ExcelHandler.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. using System.IO;
  2. using System.Data;
  3. using System;
  4. using NPOI.HSSF.UserModel;
  5. using NPOI.SS.UserModel;
  6. using NPOI.HSSF.Util;
  7. using NPOI.SS.Formula.Eval;
  8. using System.Text;
  9. using NPOI.SS.Util;
  10. using System.Drawing;
  11. using System.Collections.Generic;
  12. using Seagull.BarTender.Print;
  13. using DevExpress.XtraPrinting.Native.LayoutAdjustment;
  14. using DevExpress.XtraExport.Implementation;
  15. namespace UAS_MES_NEW.DataOperate
  16. {
  17. class ExcelHandler
  18. {
  19. DataHelper dh = new DataHelper();
  20. /// <summary>
  21. /// 导出Excel,返回文件在客户端的路径
  22. /// </summary>
  23. public string ExportExcel(DataTable dt, string FolderPath)
  24. {
  25. //创建一个内存流,用来接收转换成Excel的内容
  26. MemoryStream ms;
  27. ms = DataTableToExcel(dt);
  28. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  29. string filePath = FolderPath;
  30. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  31. byte[] data = ms.ToArray();
  32. fs.Write(data, 0, data.Length);
  33. fs.Flush();
  34. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  35. ms.Dispose();
  36. fs.Dispose();
  37. return filePath;
  38. }
  39. public string ExportExcel_LIANGAN(DataTable dt, string FolderPath)
  40. {
  41. //创建一个内存流,用来接收转换成Excel的内容
  42. MemoryStream ms;
  43. ms = DataTableToExcel_LIANGAN(dt, FolderPath);
  44. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  45. string filePath = FolderPath;
  46. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  47. byte[] data = ms.ToArray();
  48. fs.Write(data, 0, data.Length);
  49. fs.Flush();
  50. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  51. ms.Dispose();
  52. fs.Dispose();
  53. return filePath;
  54. }
  55. public MemoryStream DataTableToExcel_LIANGAN(DataTable DataTable, string FolderPath)
  56. {
  57. //创建内存流
  58. MemoryStream ms = new MemoryStream();
  59. //创建一个Book,相当于一个Excel文件
  60. HSSFWorkbook book = new HSSFWorkbook();
  61. //Excel中的Sheet
  62. //获取行数量和列数量
  63. int rowNum = DataTable.Rows.Count;
  64. int columnNum = DataTable.Columns.Count;
  65. //设置列的宽度,根据首行的列的内容的长度来设置
  66. string SheetName = "";
  67. //表格数据游标
  68. int DataRowCount = 8;
  69. for (int i = 0; i < DataTable.Rows.Count; i++)
  70. {
  71. if (DataTable.Rows[i]["STF_MACHINE"].ToString() != "" && i != DataTable.Rows.Count - 1)
  72. {
  73. ISheet sheet1 = book.CreateSheet(DataTable.Rows[i]["STF_MACHINE"].ToString() + "-" + DataTable.Rows[i + 1]["STF_MACHINE"].ToString().Substring(0, 1));
  74. SheetName = DataTable.Rows[i]["STF_MACHINE"].ToString() + "-" + DataTable.Rows[i + 1]["STF_MACHINE"].ToString().Substring(0, 1);
  75. IRow row1 = sheet1.CreateRow(0);
  76. row1.CreateCell(0); row1.Cells[0].SetCellValue(" SMT程式料表 ");
  77. //客户抬头
  78. IRow row2 = sheet1.CreateRow(1);
  79. CellRangeAddress cellRangeAddress = new CellRangeAddress(1, 1, 0, 1);
  80. CellRangeAddress cellRangeAddress1 = new CellRangeAddress(1, 1, 2, 3);
  81. CellRangeAddress cellRangeAddress2 = new CellRangeAddress(1, 1, 4, 5);
  82. CellRangeAddress cellRangeAddress3 = new CellRangeAddress(1, 1, 6, 7);
  83. sheet1.AddMergedRegion(cellRangeAddress);
  84. sheet1.AddMergedRegion(cellRangeAddress1);
  85. sheet1.AddMergedRegion(cellRangeAddress2);
  86. sheet1.AddMergedRegion(cellRangeAddress3);
  87. row2.CreateCell(0); row2.CreateCell(1); row2.Cells[0].SetCellValue("客户名称:");
  88. row2.CreateCell(2); row2.CreateCell(3); row2.Cells[2].SetCellValue("CY");
  89. row2.CreateCell(4); row2.CreateCell(5); row2.Cells[4].SetCellValue("文件编号://");
  90. row2.CreateCell(6); row2.CreateCell(7); row2.Cells[6].SetCellValue("发行日期:2024-06-14");
  91. //产品型号
  92. IRow row3 = sheet1.CreateRow(2);
  93. CellRangeAddress cellRangeAddress4 = new CellRangeAddress(2, 2, 0, 1);
  94. CellRangeAddress cellRangeAddress5 = new CellRangeAddress(2, 2, 2, 3);
  95. CellRangeAddress cellRangeAddress6 = new CellRangeAddress(2, 2, 4, 5);
  96. CellRangeAddress cellRangeAddress7 = new CellRangeAddress(2, 2, 6, 7);
  97. sheet1.AddMergedRegion(cellRangeAddress4);
  98. sheet1.AddMergedRegion(cellRangeAddress5);
  99. sheet1.AddMergedRegion(cellRangeAddress6);
  100. sheet1.AddMergedRegion(cellRangeAddress7);
  101. row3.CreateCell(0); row3.CreateCell(1); row3.Cells[0].SetCellValue("产品型号:");
  102. row3.CreateCell(2); row3.CreateCell(3); row3.Cells[2].SetCellValue("it9220-LS2404-B1-V2.0-MB");
  103. row3.CreateCell(4); row3.CreateCell(5); row3.Cells[4].SetCellValue("BOM编号:EN-CY-BOM-143(A0)");
  104. row3.CreateCell(6); row3.CreateCell(7); row3.Cells[6].SetCellValue("生效日期:2024-06-14");
  105. //程序名称
  106. IRow row4 = sheet1.CreateRow(3);
  107. CellRangeAddress cellRangeAddress8 = new CellRangeAddress(3, 3, 0, 1);
  108. CellRangeAddress cellRangeAddress9 = new CellRangeAddress(3, 3, 2, 3);
  109. CellRangeAddress cellRangeAddress10 = new CellRangeAddress(3, 3, 4, 5);
  110. sheet1.AddMergedRegion(cellRangeAddress8);
  111. sheet1.AddMergedRegion(cellRangeAddress9);
  112. sheet1.AddMergedRegion(cellRangeAddress10);
  113. row4.CreateCell(0); row4.CreateCell(1); row4.Cells[0].SetCellValue("程序名称\t\r\n");
  114. row4.CreateCell(2); row4.CreateCell(3); row4.Cells[2].SetCellValue("CY-it9220-LS2404-B1-V2.0-MB\t\r\n");
  115. row4.CreateCell(4); row4.CreateCell(5); row4.Cells[4].SetCellValue("面别: AB面\t\r\n");
  116. //机器名称
  117. IRow row5 = sheet1.CreateRow(4);
  118. CellRangeAddress cellRangeAddress14 = new CellRangeAddress(5, 5, 0, 1);
  119. CellRangeAddress cellRangeAddress15 = new CellRangeAddress(5, 5, 2, 3);
  120. CellRangeAddress cellRangeAddress16 = new CellRangeAddress(5, 5, 4, 5);
  121. CellRangeAddress cellRangeAddress20 = new CellRangeAddress(5, 6, 6, 7);
  122. sheet1.AddMergedRegion(cellRangeAddress20);
  123. sheet1.AddMergedRegion(cellRangeAddress14);
  124. sheet1.AddMergedRegion(cellRangeAddress15);
  125. sheet1.AddMergedRegion(cellRangeAddress16);
  126. sheet1.AddMergedRegion(cellRangeAddress20);
  127. row5.CreateCell(0); row5.CreateCell(1); row5.Cells[0].SetCellValue("机器名称\t\r\n");
  128. row5.CreateCell(2); row5.CreateCell(3); row5.Cells[2].SetCellValue("TX2I+TX2I+TX2I+TX2+SX1\t\r\n");
  129. row5.CreateCell(4); row5.CreateCell(5); row5.Cells[4].SetCellValue("QA确认:\t\r\n");
  130. row5.CreateCell(6); row5.CreateCell(7); row5.Cells[6].SetCellValue("文控签章:");
  131. //制作
  132. IRow row6 = sheet1.CreateRow(5);
  133. CellRangeAddress cellRangeAddress17 = new CellRangeAddress(6, 6, 0, 1);
  134. CellRangeAddress cellRangeAddress18 = new CellRangeAddress(6, 6, 2, 3);
  135. CellRangeAddress cellRangeAddress19 = new CellRangeAddress(6, 6, 4, 5);
  136. sheet1.AddMergedRegion(cellRangeAddress17);
  137. sheet1.AddMergedRegion(cellRangeAddress18);
  138. sheet1.AddMergedRegion(cellRangeAddress19);
  139. row6.CreateCell(0); row6.CreateCell(1); row6.Cells[0].SetCellValue("制作:");
  140. row6.CreateCell(2); row6.CreateCell(3); row6.Cells[2].SetCellValue("贺瑞华");
  141. row6.CreateCell(4); row6.CreateCell(5); row6.Cells[4].SetCellValue("QA审核:");
  142. //制作
  143. IRow row7 = sheet1.CreateRow(6);
  144. CellRangeAddress cellRangeAddress21 = new CellRangeAddress(6, 6, 0, 7);
  145. sheet1.AddMergedRegion(cellRangeAddress21);
  146. row7.CreateCell(0); row7.CreateCell(1); row7.Cells[0].SetCellValue(DataTable.Rows[i]["STF_MACHINE"].ToString() + "-" + DataTable.Rows[i + 1]["STF_MACHINE"].ToString().Substring(0, 1));
  147. IRow row8 = sheet1.CreateRow(7);
  148. row8.CreateCell(0); row8.Cells[0].SetCellValue("站位");
  149. row8.CreateCell(1); row8.Cells[1].SetCellValue("Feeder类型");
  150. row8.CreateCell(2); row8.Cells[2].SetCellValue("位置");
  151. row8.CreateCell(3); row8.Cells[3].SetCellValue("物料编码");
  152. row8.CreateCell(4); row8.Cells[4].SetCellValue("物料描述");
  153. row8.CreateCell(5); row8.Cells[5].SetCellValue("用量");
  154. row8.CreateCell(6); row8.Cells[6].SetCellValue("位号");
  155. row8.CreateCell(7); row8.Cells[7].SetCellValue("备注");
  156. i = i + 1;
  157. DataRowCount = 8;
  158. sheet1.SetColumnWidth(4,sheet1.GetColumnWidth(4)+3500);
  159. }
  160. else
  161. {
  162. if (SheetName != "")
  163. {
  164. ISheet sheet = book.GetSheet(SheetName);
  165. IRow row = sheet.CreateRow(DataRowCount);
  166. row.CreateCell(0);
  167. row.CreateCell(1);
  168. row.CreateCell(2);
  169. row.CreateCell(3);
  170. row.CreateCell(4);
  171. row.CreateCell(5);
  172. row.CreateCell(6);
  173. //位置编号
  174. CellRangeAddress cellRangeAddress = new CellRangeAddress(DataRowCount, DataRowCount + 1, 0, 0);
  175. sheet.AddMergedRegion(cellRangeAddress);
  176. //飞达规格
  177. CellRangeAddress cellRangeAddress1 = new CellRangeAddress(DataRowCount, DataRowCount + 1, 1, 1);
  178. sheet.AddMergedRegion(cellRangeAddress1);
  179. row.Cells[0].SetCellValue(DataTable.Rows[i]["STF_MODEL"].ToString());
  180. row.Cells[1].SetCellValue(DataTable.Rows[i]["STF_FEEDER"].ToString());
  181. //物料编号
  182. row.Cells[2].SetCellValue(DataTable.Rows[i]["STF_FEEDERNO"].ToString());
  183. row.Cells[3].SetCellValue(DataTable.Rows[i]["STF_LOCATION"].ToString());
  184. row.Cells[4].SetCellValue(DataTable.Rows[i]["pr_orispeccode"].ToString());
  185. row.Cells[4].CellStyle.WrapText = true;
  186. row.Cells[5].SetCellValue(DataTable.Rows[i]["bd_baseqty"].ToString());
  187. row.Cells[6].SetCellValue(DataTable.Rows[i]["bd_soncode"].ToString());
  188. row.Cells[4].CellStyle.ShrinkToFit = true;
  189. DataRowCount = DataRowCount + 1;
  190. }
  191. }
  192. }
  193. //将book的内容写入内存流中返回
  194. book.Write(ms);
  195. return ms;
  196. }
  197. /// <summary>
  198. /// 导出Excel,返回文件在客户端的路径
  199. /// </summary>
  200. public string ExportExcel_BAIDU(DataTable dt, DateTime begindate, int DateNum, string FolderPath)
  201. {
  202. //创建一个内存流,用来接收转换成Excel的内容
  203. MemoryStream ms;
  204. ms = DataTableToExcel_BAIDU(dt, begindate, DateNum);
  205. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  206. string filePath = @FolderPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + "各工序直通率.xls";
  207. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  208. byte[] data = ms.ToArray();
  209. fs.Write(data, 0, data.Length);
  210. fs.Flush();
  211. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  212. ms.Dispose();
  213. fs.Dispose();
  214. return filePath;
  215. }
  216. /// <summary>
  217. /// 导入Excel
  218. /// </summary>
  219. public DataTable ImportExcel(string FolderPath, string TableName)
  220. {
  221. DataTable dt = new DataTable();
  222. dt.TableName = TableName;
  223. if (FolderPath.Contains(".xls") || FolderPath.Contains(".xlsx"))
  224. {
  225. using (FileStream file = new FileStream(FolderPath, FileMode.Open, FileAccess.Read))
  226. {
  227. //获取文件流
  228. HSSFWorkbook workbook = new HSSFWorkbook(file);
  229. ISheet sheet = workbook.GetSheetAt(0);
  230. //获取所有的列名
  231. foreach (ICell item in sheet.GetRow(sheet.FirstRowNum).Cells)
  232. {
  233. dt.Columns.Add(item.ToString(), typeof(string));
  234. }
  235. System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  236. while (rows.MoveNext())
  237. {
  238. IRow row = (HSSFRow)rows.Current;
  239. if (row.RowNum == sheet.FirstRowNum)
  240. {
  241. continue;
  242. }
  243. DataRow dr = dt.NewRow();
  244. foreach (ICell item in row.Cells)
  245. {
  246. switch (item.CellType)
  247. {
  248. case CellType.BOOLEAN:
  249. dr[item.ColumnIndex] = item.BooleanCellValue;
  250. break;
  251. case CellType.ERROR:
  252. dr[item.ColumnIndex] = ErrorEval.GetText(item.ErrorCellValue);
  253. break;
  254. case CellType.FORMULA:
  255. switch (item.CachedFormulaResultType)
  256. {
  257. case CellType.BOOLEAN:
  258. dr[item.ColumnIndex] = item.BooleanCellValue;
  259. break;
  260. case CellType.ERROR:
  261. dr[item.ColumnIndex] = ErrorEval.GetText(item.ErrorCellValue);
  262. break;
  263. case CellType.NUMERIC:
  264. if (DateUtil.IsCellDateFormatted(item))
  265. {
  266. dr[item.ColumnIndex] = item.DateCellValue.ToString("yyyy-MM-dd hh:MM:ss");
  267. }
  268. else
  269. {
  270. dr[item.ColumnIndex] = item.NumericCellValue;
  271. }
  272. break;
  273. case CellType.STRING:
  274. string str = item.StringCellValue;
  275. if (!string.IsNullOrEmpty(str))
  276. {
  277. dr[item.ColumnIndex] = str.ToString();
  278. }
  279. else
  280. {
  281. dr[item.ColumnIndex] = null;
  282. }
  283. break;
  284. case CellType.Unknown:
  285. case CellType.BLANK:
  286. default:
  287. dr[item.ColumnIndex] = string.Empty;
  288. break;
  289. }
  290. break;
  291. case CellType.NUMERIC:
  292. if (DateUtil.IsCellDateFormatted(item))
  293. {
  294. dr[item.ColumnIndex] = item.DateCellValue.ToString("yyyy-MM-dd hh:MM:ss");
  295. }
  296. else
  297. {
  298. dr[item.ColumnIndex] = item.NumericCellValue;
  299. }
  300. break;
  301. case CellType.STRING:
  302. string strValue = item.StringCellValue;
  303. if (string.IsNullOrEmpty(strValue))
  304. {
  305. dr[item.ColumnIndex] = strValue.ToString();
  306. }
  307. else
  308. {
  309. dr[item.ColumnIndex] = null;
  310. }
  311. break;
  312. case CellType.Unknown:
  313. case CellType.BLANK:
  314. default:
  315. dr[item.ColumnIndex] = string.Empty;
  316. break;
  317. }
  318. }
  319. dt.Rows.Add(dr);
  320. }
  321. }
  322. }
  323. return dt;
  324. }
  325. public void WriteTxt(DataTable dt, string FolderPath, string FileName)
  326. {
  327. StreamWriter sr;
  328. string filePath = @FolderPath;
  329. if (File.Exists(filePath + "\\" + FileName)) //如果文件存在,则创建File.AppendText对象
  330. {
  331. File.Delete(filePath + "\\" + FileName);
  332. }
  333. sr = File.CreateText(filePath + "\\" + FileName);
  334. StringBuilder sb = new StringBuilder();
  335. string Title = "";
  336. foreach (DataColumn dc in dt.Columns)
  337. {
  338. Title += string.Format("{0,10}", dc.ColumnName.ToString());
  339. }
  340. sr.WriteLine(Title + "\r");
  341. foreach (DataRow dr in dt.Rows)
  342. {
  343. string text = "";
  344. for (int i = 0; i < dt.Columns.Count; i++)
  345. {
  346. text += String.Format("{0,10}", dr[i].ToString());
  347. }
  348. sr.WriteLine(text + "\r");
  349. }
  350. sr.Close();
  351. }
  352. /// <summary>
  353. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  354. /// </summary>
  355. /// <param name="DataTable"></param>
  356. /// <returns></returns>
  357. public MemoryStream DataTableToExcel(DataTable DataTable)
  358. {
  359. //创建内存流
  360. MemoryStream ms = new MemoryStream();
  361. //创建一个Book,相当于一个Excel文件
  362. HSSFWorkbook book = new HSSFWorkbook();
  363. //Excel中的Sheet
  364. ISheet sheet = book.CreateSheet("sheet1");
  365. //获取行数量和列数量
  366. int rowNum = DataTable.Rows.Count;
  367. int columnNum = DataTable.Columns.Count;
  368. //设置列的宽度,根据首行的列的内容的长度来设置
  369. for (int i = 0; i < columnNum; i++)
  370. {
  371. int dataLength = DataTable.Columns[i].ColumnName.Length;
  372. sheet.SetColumnWidth(i, dataLength * 700);
  373. }
  374. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  375. IRow row = sheet.CreateRow(0);
  376. //冻结第一行
  377. sheet.CreateFreezePane(0, 1, 0, 1);
  378. ICellStyle style = book.CreateCellStyle();
  379. style.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  380. style.FillPattern = FillPatternType.BIG_SPOTS;
  381. style.FillBackgroundColor = HSSFColor.LIGHT_GREEN.index;
  382. //设置边框
  383. style.BorderBottom = BorderStyle.THICK;
  384. style.BorderLeft = BorderStyle.THICK;
  385. style.BorderRight = BorderStyle.THICK;
  386. style.BorderTop = BorderStyle.THICK;
  387. row.HeightInPoints = 20;
  388. //固定第一行
  389. //row.RowStyle.IsLocked=true;
  390. //给第一行的标签赋值样式和值
  391. for (int j = 0; j < columnNum; j++)
  392. {
  393. row.CreateCell(j);
  394. row.Cells[j].CellStyle = style;
  395. row.Cells[j].CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  396. row.Cells[j].CellStyle.Alignment = HorizontalAlignment.CENTER;
  397. row.Cells[j].SetCellValue(DataTable.Columns[j].ColumnName);
  398. }
  399. //将DataTable的值循环赋值给book,Aligment设置居中
  400. //之前已经画了带颜色的第一行,所以从i=1开始画
  401. for (int i = 0; i < rowNum; i++)
  402. {
  403. IRow row1 = sheet.CreateRow(i + 1);
  404. row1.HeightInPoints = 20;
  405. for (int j = 0; j < columnNum; j++)
  406. {
  407. row1.CreateCell(j);
  408. row1.Cells[j].SetCellValue(DataTable.Rows[i][j].ToString());
  409. row1.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  410. }
  411. }
  412. //将book的内容写入内存流中返回
  413. book.Write(ms);
  414. return ms;
  415. }
  416. /// <summary>
  417. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  418. /// </summary>
  419. /// <param name="DataTable"></param>
  420. /// <returns></returns>
  421. public MemoryStream DataTableToExcel_BAIDU(DataTable DataTable, DateTime begindate, int DateNum)
  422. {
  423. string[] Step = new[] { "1-MT1", "2-MT2", "3-MMI", "4-RSA(耦合)", "5-AUD(曲线)", "6-SCW(写号)", "7-SCK(验号)" };
  424. string[] StepCode = new[] { "B_MT1", "B_MT2", "B_MMI", "B_RSA", "B_AUD", "B_WRITE", "B_SN", "B_OUTLOOK" };
  425. string[] Kind = new[] { "测试数", "通过数", "不良数", "误测通过数", "误测数", "FPY", "RPY" };
  426. string[] TotalKind = new[] { "总投入数", "总不良数", "FPY", "RPY" };
  427. string[] OutLook = new[] { "测试数", "不良数", "FPY" };
  428. //每行的内容
  429. int ContentRow = 7;
  430. //外观的展示的行
  431. int OutLookRow = 55;
  432. MemoryStream ms = new MemoryStream();
  433. //创建一个Book,相当于一个Excel文件
  434. HSSFWorkbook book = new HSSFWorkbook();
  435. ICellStyle NONE = book.CreateCellStyle();
  436. NONE.VerticalAlignment = VerticalAlignment.CENTER;
  437. NONE.Alignment = HorizontalAlignment.CENTER;
  438. NONE.BorderBottom = BorderStyle.THIN;
  439. NONE.BorderLeft = BorderStyle.THIN;
  440. NONE.BorderRight = BorderStyle.THIN;
  441. NONE.BorderTop = BorderStyle.THIN;
  442. ICellStyle TAN = book.CreateCellStyle();
  443. TAN.VerticalAlignment = VerticalAlignment.CENTER;
  444. TAN.Alignment = HorizontalAlignment.CENTER;
  445. TAN.FillForegroundColor = HSSFColor.TAN.index;
  446. TAN.FillPattern = FillPatternType.SOLID_FOREGROUND;
  447. TAN.BorderBottom = BorderStyle.THIN;
  448. TAN.BorderLeft = BorderStyle.THIN;
  449. TAN.BorderRight = BorderStyle.THIN;
  450. TAN.BorderTop = BorderStyle.THIN;
  451. ICellStyle PALE_BLUE = book.CreateCellStyle();
  452. PALE_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  453. PALE_BLUE.Alignment = HorizontalAlignment.CENTER;
  454. PALE_BLUE.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  455. PALE_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  456. PALE_BLUE.BorderBottom = BorderStyle.THIN;
  457. PALE_BLUE.BorderLeft = BorderStyle.THIN;
  458. PALE_BLUE.BorderRight = BorderStyle.THIN;
  459. PALE_BLUE.BorderTop = BorderStyle.THIN;
  460. ICellStyle LIME = book.CreateCellStyle();
  461. LIME.VerticalAlignment = VerticalAlignment.CENTER;
  462. LIME.Alignment = HorizontalAlignment.CENTER;
  463. LIME.FillForegroundColor = HSSFColor.LIME.index;
  464. LIME.FillPattern = FillPatternType.SOLID_FOREGROUND;
  465. LIME.BorderBottom = BorderStyle.THIN;
  466. LIME.BorderLeft = BorderStyle.THIN;
  467. LIME.BorderRight = BorderStyle.THIN;
  468. LIME.BorderTop = BorderStyle.THIN;
  469. ICellStyle LEMON_CHIFFON = book.CreateCellStyle();
  470. LEMON_CHIFFON.VerticalAlignment = VerticalAlignment.CENTER;
  471. LEMON_CHIFFON.Alignment = HorizontalAlignment.CENTER;
  472. LEMON_CHIFFON.FillForegroundColor = HSSFColor.LEMON_CHIFFON.index;
  473. LEMON_CHIFFON.FillPattern = FillPatternType.SOLID_FOREGROUND;
  474. LEMON_CHIFFON.BorderBottom = BorderStyle.THIN;
  475. LEMON_CHIFFON.BorderLeft = BorderStyle.THIN;
  476. LEMON_CHIFFON.BorderRight = BorderStyle.THIN;
  477. LEMON_CHIFFON.BorderTop = BorderStyle.THIN;
  478. LEMON_CHIFFON.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  479. ICellStyle GOLD = book.CreateCellStyle();
  480. GOLD.VerticalAlignment = VerticalAlignment.CENTER;
  481. GOLD.Alignment = HorizontalAlignment.CENTER;
  482. GOLD.FillForegroundColor = HSSFColor.GOLD.index;
  483. GOLD.FillPattern = FillPatternType.SOLID_FOREGROUND;
  484. GOLD.BorderBottom = BorderStyle.THIN;
  485. GOLD.BorderLeft = BorderStyle.THIN;
  486. GOLD.BorderRight = BorderStyle.THIN;
  487. GOLD.BorderTop = BorderStyle.THIN;
  488. ICellStyle LIGHT_GREEN = book.CreateCellStyle();
  489. LIGHT_GREEN.VerticalAlignment = VerticalAlignment.CENTER;
  490. LIGHT_GREEN.Alignment = HorizontalAlignment.CENTER;
  491. LIGHT_GREEN.FillForegroundColor = HSSFColor.LIGHT_GREEN.index;
  492. LIGHT_GREEN.FillPattern = FillPatternType.SOLID_FOREGROUND;
  493. LIGHT_GREEN.BorderBottom = BorderStyle.THIN;
  494. LIGHT_GREEN.BorderLeft = BorderStyle.THIN;
  495. LIGHT_GREEN.BorderRight = BorderStyle.THIN;
  496. LIGHT_GREEN.BorderTop = BorderStyle.THIN;
  497. //LIGHT_GREEN.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  498. ICellStyle DARK_BLUE = book.CreateCellStyle();
  499. DARK_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  500. DARK_BLUE.Alignment = HorizontalAlignment.CENTER;
  501. DARK_BLUE.FillForegroundColor = HSSFColor.LIGHT_BLUE.index;
  502. DARK_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  503. DARK_BLUE.BorderBottom = BorderStyle.THIN;
  504. DARK_BLUE.BorderLeft = BorderStyle.THIN;
  505. DARK_BLUE.BorderRight = BorderStyle.THIN;
  506. DARK_BLUE.BorderTop = BorderStyle.THIN;
  507. DARK_BLUE.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  508. ICellStyle LIGHT_CORNFLOWER_BLUE = book.CreateCellStyle();
  509. LIGHT_CORNFLOWER_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  510. LIGHT_CORNFLOWER_BLUE.Alignment = HorizontalAlignment.CENTER;
  511. LIGHT_CORNFLOWER_BLUE.FillForegroundColor = HSSFColor.LIGHT_CORNFLOWER_BLUE.index;
  512. LIGHT_CORNFLOWER_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  513. LIGHT_CORNFLOWER_BLUE.BorderBottom = BorderStyle.THIN;
  514. LIGHT_CORNFLOWER_BLUE.BorderLeft = BorderStyle.THIN;
  515. LIGHT_CORNFLOWER_BLUE.BorderRight = BorderStyle.THIN;
  516. LIGHT_CORNFLOWER_BLUE.BorderTop = BorderStyle.THIN;
  517. LIGHT_CORNFLOWER_BLUE.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  518. ICellStyle GREY_25_PERCENT = book.CreateCellStyle();
  519. GREY_25_PERCENT.VerticalAlignment = VerticalAlignment.CENTER;
  520. GREY_25_PERCENT.Alignment = HorizontalAlignment.CENTER;
  521. GREY_25_PERCENT.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
  522. GREY_25_PERCENT.FillPattern = FillPatternType.SOLID_FOREGROUND;
  523. GREY_25_PERCENT.BorderBottom = BorderStyle.THIN;
  524. GREY_25_PERCENT.BorderLeft = BorderStyle.THIN;
  525. GREY_25_PERCENT.BorderRight = BorderStyle.THIN;
  526. GREY_25_PERCENT.BorderTop = BorderStyle.THIN;
  527. ICellStyle PINK = book.CreateCellStyle();
  528. PINK.VerticalAlignment = VerticalAlignment.CENTER;
  529. PINK.Alignment = HorizontalAlignment.CENTER;
  530. PINK.FillForegroundColor = HSSFColor.LIGHT_ORANGE.index;
  531. PINK.FillPattern = FillPatternType.SOLID_FOREGROUND;
  532. PINK.BorderBottom = BorderStyle.THIN;
  533. PINK.BorderLeft = BorderStyle.THIN;
  534. PINK.BorderRight = BorderStyle.THIN;
  535. PINK.BorderTop = BorderStyle.THIN;
  536. //Excel中的Sheet
  537. ISheet sheet = book.CreateSheet("sheet1");
  538. IRow row = sheet.CreateRow(0);
  539. ICell cell = row.CreateCell(0);
  540. //画第一行的抬头
  541. cell.SetCellValue("组装制程品质数据");
  542. cell.CellStyle = NONE;
  543. CellRangeAddress region = new CellRangeAddress(0, 0, 0, DateNum + 1);
  544. sheet.AddMergedRegion(region);
  545. //第一行的日期标题
  546. row = sheet.CreateRow(1);
  547. cell = row.CreateCell(0);
  548. cell.CellStyle = NONE;
  549. cell.SetCellValue("站点");
  550. cell = row.CreateCell(1);
  551. cell.SetCellValue("类别");
  552. cell.CellStyle = NONE;
  553. for (int i = 0; i < DateNum; i++)
  554. {
  555. cell = row.CreateCell(i + 2);
  556. cell.SetCellValue(begindate.AddDays(i).ToString("MM/dd"));
  557. cell.CellStyle = NONE;
  558. }
  559. //画第一列的工序名称和第二列的类别
  560. //总良率数据
  561. row = sheet.CreateRow(2);
  562. cell = row.CreateCell(0);
  563. cell.SetCellValue("总良率");
  564. cell.CellStyle = LEMON_CHIFFON;
  565. region = new CellRangeAddress(2, 5, 0, 0);
  566. sheet.AddMergedRegion(region);
  567. //总良率的统计数据
  568. for (int i = 0; i < TotalKind.Length; i++)
  569. {
  570. row = sheet.GetRow(i + 2);
  571. if (row == null)
  572. {
  573. row = sheet.CreateRow(i + 2);
  574. }
  575. cell = row.CreateCell(1);
  576. cell.SetCellValue(TotalKind[i]);
  577. cell.CellStyle = LEMON_CHIFFON;
  578. switch (i)
  579. {
  580. case 0:
  581. cell.CellStyle = LIME;
  582. break;
  583. case 1:
  584. cell.CellStyle = TAN;
  585. break;
  586. case 2:
  587. cell.CellStyle = DARK_BLUE;
  588. break;
  589. case 3:
  590. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  591. break;
  592. default:
  593. break;
  594. }
  595. }
  596. //中间的设备测试工序
  597. for (int i = 0; i < Step.Length; i++)
  598. {
  599. //除去前面6行
  600. int rowindex = 6 + i * ContentRow;
  601. row = sheet.CreateRow(rowindex);
  602. cell = row.CreateCell(0);
  603. cell.SetCellValue(Step[i]);
  604. cell.CellStyle = PALE_BLUE;
  605. region = new CellRangeAddress(rowindex, rowindex + ContentRow - 1, 0, 0);
  606. sheet.AddMergedRegion(region);
  607. for (int j = rowindex; j < rowindex + ContentRow; j++)
  608. {
  609. row = sheet.GetRow(j);
  610. if (row == null)
  611. {
  612. row = sheet.CreateRow(j);
  613. }
  614. cell = row.CreateCell(1);
  615. cell.SetCellValue(Kind[j - rowindex]);
  616. switch (j - rowindex)
  617. {
  618. case 0:
  619. cell.CellStyle = GREY_25_PERCENT;
  620. break;
  621. case 1:
  622. cell.CellStyle = PINK;
  623. break;
  624. case 2:
  625. cell.CellStyle = TAN;
  626. break;
  627. case 3:
  628. cell.CellStyle = GOLD;
  629. break;
  630. case 4:
  631. cell.CellStyle = LIGHT_GREEN;
  632. break;
  633. case 5:
  634. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  635. break;
  636. case 6:
  637. cell.CellStyle = LEMON_CHIFFON;
  638. break;
  639. default:
  640. break;
  641. }
  642. }
  643. }
  644. //最后一行外观数据
  645. row = sheet.CreateRow(OutLookRow);
  646. cell = row.CreateCell(0);
  647. cell.SetCellValue("8-外观");
  648. cell.CellStyle = PALE_BLUE;
  649. region = new CellRangeAddress(OutLookRow, OutLookRow + 2, 0, 0);
  650. sheet.AddMergedRegion(region);
  651. //外观的统计数据
  652. for (int i = 0; i < OutLook.Length; i++)
  653. {
  654. row = sheet.GetRow(OutLookRow + i);
  655. if (row == null)
  656. {
  657. row = sheet.CreateRow(OutLookRow + i);
  658. }
  659. cell = row.CreateCell(1);
  660. cell.SetCellValue(OutLook[i]);
  661. cell.CellStyle = PALE_BLUE;
  662. switch (i)
  663. {
  664. case 0:
  665. cell.CellStyle = GREY_25_PERCENT;
  666. break;
  667. case 1:
  668. cell.CellStyle = TAN;
  669. break;
  670. case 2:
  671. cell.CellStyle = LIGHT_GREEN;
  672. break;
  673. default:
  674. break;
  675. }
  676. }
  677. sheet.SetColumnWidth(0, 3700);
  678. for (int i = 0; i < DateNum; i++)
  679. {
  680. double TotalFPY = -1;
  681. double TotalRPY = -1;
  682. double TotalNG = 0;
  683. double TotalIN = 0;
  684. for (int j = 0; j < StepCode.Length; j++)
  685. {
  686. int rowindex = 6 + j * ContentRow;
  687. DataTable dt = PublicMethod.BaseUtil.filterDataTable(DataTable, "sp_date='" + begindate.AddDays(i).ToString("yyyy-MM-dd") + "' and 工序编号='" + StepCode[j] + "'");
  688. if (StepCode[j] != "B_OUTLOOK")
  689. {
  690. for (int k = rowindex; k < rowindex + ContentRow; k++)
  691. {
  692. row = sheet.GetRow(k);
  693. if (row == null)
  694. {
  695. row = sheet.CreateRow(k);
  696. }
  697. cell = row.CreateCell(i + 2);
  698. switch (k - rowindex)
  699. {
  700. case 0:
  701. double 测试数;
  702. if (dt.Rows.Count > 0)
  703. {
  704. if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
  705. {
  706. cell.SetCellValue(测试数);
  707. }
  708. }
  709. cell.CellStyle = GREY_25_PERCENT;
  710. break;
  711. case 1:
  712. double 通过数;
  713. if (dt.Rows.Count > 0)
  714. {
  715. if (double.TryParse(dt.Rows[0]["通过总数"].ToString(), out 通过数))
  716. {
  717. cell.SetCellValue(通过数);
  718. }
  719. }
  720. cell.CellStyle = PINK;
  721. break;
  722. case 2:
  723. double 不良数;
  724. if (dt.Rows.Count > 0)
  725. {
  726. if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
  727. {
  728. cell.SetCellValue(不良数);
  729. TotalNG = TotalNG + 不良数;
  730. }
  731. }
  732. cell.CellStyle = TAN;
  733. break;
  734. case 3:
  735. double 误测通过数;
  736. if (dt.Rows.Count > 0)
  737. {
  738. if (double.TryParse(dt.Rows[0]["误测通过数"].ToString(), out 误测通过数))
  739. {
  740. cell.SetCellValue(误测通过数);
  741. }
  742. }
  743. cell.CellStyle = GOLD;
  744. break;
  745. case 4:
  746. double 误测数;
  747. if (dt.Rows.Count > 0)
  748. {
  749. if (double.TryParse(dt.Rows[0]["误测数"].ToString(), out 误测数))
  750. {
  751. cell.SetCellValue(误测数);
  752. }
  753. }
  754. cell.CellStyle = LIGHT_GREEN;
  755. break;
  756. case 5:
  757. double FPY;
  758. if (dt.Rows.Count > 0)
  759. {
  760. if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
  761. {
  762. cell.SetCellValue(FPY);
  763. //累计所有FPY
  764. if (TotalFPY == -1)
  765. {
  766. TotalFPY = FPY;
  767. }
  768. else
  769. {
  770. TotalFPY = TotalFPY * FPY;
  771. }
  772. }
  773. }
  774. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  775. break;
  776. case 6:
  777. double RPY;
  778. if (dt.Rows.Count > 0)
  779. {
  780. if (double.TryParse(dt.Rows[0]["RPY"].ToString(), out RPY))
  781. {
  782. cell.SetCellValue(RPY);
  783. //累计所有RPY
  784. if (TotalRPY == -1)
  785. {
  786. TotalRPY = RPY;
  787. }
  788. else
  789. {
  790. TotalRPY = TotalRPY * RPY;
  791. }
  792. }
  793. }
  794. cell.CellStyle = LEMON_CHIFFON;
  795. break;
  796. default:
  797. break;
  798. }
  799. }
  800. }
  801. else
  802. {
  803. for (int k = rowindex; k < rowindex + 3; k++)
  804. {
  805. row = sheet.GetRow(k);
  806. if (row == null)
  807. {
  808. row = sheet.CreateRow(k);
  809. }
  810. cell = row.CreateCell(i + 2);
  811. switch (k - rowindex)
  812. {
  813. case 0:
  814. double 测试数;
  815. if (dt.Rows.Count > 0)
  816. {
  817. if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
  818. {
  819. cell.SetCellValue(测试数);
  820. }
  821. }
  822. cell.CellStyle = GREY_25_PERCENT;
  823. break;
  824. case 1:
  825. double 不良数;
  826. if (dt.Rows.Count > 0)
  827. {
  828. if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
  829. {
  830. cell.SetCellValue(不良数);
  831. TotalNG = TotalNG + 不良数;
  832. }
  833. }
  834. cell.CellStyle = TAN;
  835. break;
  836. case 2:
  837. double FPY;
  838. if (dt.Rows.Count > 0)
  839. {
  840. if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
  841. {
  842. cell.SetCellValue(FPY);
  843. //累计所有FPY
  844. if (TotalFPY == -1)
  845. {
  846. TotalFPY = FPY;
  847. }
  848. else
  849. {
  850. TotalFPY = TotalFPY * FPY;
  851. }
  852. }
  853. }
  854. cell.CellStyle = LIGHT_GREEN;
  855. break;
  856. default:
  857. break;
  858. }
  859. }
  860. }
  861. }
  862. DataTable dt1 = PublicMethod.BaseUtil.filterDataTable(DataTable, "sp_date='" + begindate.AddDays(i).ToString("yyyy-MM-dd") + "' and 工序编号='B_LCDBA1'");
  863. double 投入数;
  864. if (dt1.Rows.Count > 0)
  865. {
  866. if (double.TryParse(dt1.Rows[0]["测试数"].ToString(), out 投入数))
  867. {
  868. TotalIN = 投入数;
  869. }
  870. }
  871. //设置最上方的总计数量
  872. row = sheet.GetRow(2);
  873. cell = row.CreateCell(i + 2);
  874. cell.CellStyle = LIME;
  875. cell.SetCellValue(TotalIN);
  876. row = sheet.GetRow(3);
  877. cell = row.CreateCell(i + 2);
  878. cell.CellStyle = TAN;
  879. cell.SetCellValue(TotalNG);
  880. row = sheet.GetRow(4);
  881. cell = row.CreateCell(i + 2);
  882. cell.CellStyle = DARK_BLUE;
  883. cell.SetCellValue(TotalFPY == -1 ? 0 : TotalFPY);
  884. row = sheet.GetRow(5);
  885. cell = row.CreateCell(i + 2);
  886. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  887. cell.SetCellValue(TotalRPY == -1 ? 0 : TotalRPY);
  888. }
  889. for (int i = 0; i < sheet.PhysicalNumberOfRows; i++)
  890. {
  891. sheet.GetRow(i).Height = 300;
  892. }
  893. //将book的内容写入内存流中返回
  894. book.Write(ms);
  895. return ms;
  896. }
  897. }
  898. }