ExcelHandler.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. using System.IO;
  2. using System.Data;
  3. using NPOI.HSSF.UserModel;
  4. using NPOI.SS.UserModel;
  5. using NPOI.HSSF.Util;
  6. using NPOI.XSSF.UserModel;
  7. using System;
  8. using UAS_LabelMachine.Entity;
  9. using UAS_LabelMachine.PublicMethod;
  10. namespace UAS_LabelMachine
  11. {
  12. class ExcelHandler
  13. {
  14. DataHelper dh = SystemInf.dh;
  15. /// <summary>
  16. /// 导出Excel,返回文件在客户端的路径
  17. /// </summary>
  18. public string ExportExcel(DataTable firstsdt, DataTable dt, string FolderPath, string FileName, string Type, int PageSize, bool PrintWID)
  19. {
  20. //创建一个内存流,用来接收转换成Excel的内容
  21. MemoryStream ms;
  22. ms = DataTableToExcel1(firstsdt, dt, Type, PageSize, PrintWID);
  23. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  24. string filePath = @FolderPath + "\\" + FileName + ".xls";
  25. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  26. byte[] data = ms.ToArray();
  27. fs.Write(data, 0, data.Length);
  28. fs.Flush();
  29. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  30. ms.Dispose();
  31. fs.Dispose();
  32. return filePath;
  33. }
  34. /// <summary>
  35. /// 导入Excel
  36. /// </summary>
  37. public void ImportExcel(DataTable DataTable, string TableName)
  38. {
  39. int columnNum = DataTable.Columns.Count;
  40. int rowNum = DataTable.Columns.Count;
  41. string[] field = new string[columnNum];
  42. for (int i = 0; i < columnNum; i++)
  43. {
  44. field[i] = DataTable.Rows[0][i].ToString();
  45. }
  46. }
  47. public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
  48. {
  49. DataTable dataTable = null;
  50. FileStream fs = null;
  51. DataColumn column = null;
  52. DataRow dataRow = null;
  53. IWorkbook workbook = null;
  54. ISheet sheet = null;
  55. IRow row = null;
  56. ICell cell = null;
  57. int startRow = 0;
  58. try
  59. {
  60. using (fs = File.OpenRead(filePath))
  61. {
  62. // 2007版本
  63. if (filePath.IndexOf(".xlsx") > 0)
  64. {
  65. workbook = new XSSFWorkbook(fs);
  66. }
  67. // 2003版本
  68. else if (filePath.IndexOf(".xls") > 0)
  69. {
  70. workbook = new HSSFWorkbook(fs);
  71. }
  72. if (workbook != null)
  73. {
  74. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  75. dataTable = new DataTable();
  76. if (sheet != null)
  77. {
  78. int rowCount = sheet.LastRowNum;//总行数
  79. if (rowCount > 0)
  80. {
  81. IRow firstRow = sheet.GetRow(0);//第一行
  82. int cellCount = firstRow.LastCellNum;//列数
  83. //构建datatable的列
  84. if (isColumnName)
  85. {
  86. startRow = 1;//如果第一行是列名,则从第二行开始读取
  87. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  88. {
  89. cell = firstRow.GetCell(i);
  90. if (cell != null)
  91. {
  92. if (cell.StringCellValue != null)
  93. {
  94. column = new DataColumn(cell.StringCellValue);
  95. dataTable.Columns.Add(column);
  96. }
  97. }
  98. }
  99. }
  100. else
  101. {
  102. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  103. {
  104. column = new DataColumn("column" + (i + 1));
  105. dataTable.Columns.Add(column);
  106. }
  107. }
  108. //填充行
  109. for (int i = startRow; i <= rowCount; ++i)
  110. {
  111. row = sheet.GetRow(i);
  112. if (row == null) continue;
  113. dataRow = dataTable.NewRow();
  114. for (int j = row.FirstCellNum; j < cellCount; ++j)
  115. {
  116. cell = row.GetCell(j);
  117. if (cell == null)
  118. {
  119. dataRow[j] = "";
  120. }
  121. else
  122. {
  123. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  124. switch (cell.CellType)
  125. {
  126. case CellType.Blank:
  127. dataRow[j] = "";
  128. break;
  129. case CellType.Numeric:
  130. short format = cell.CellStyle.DataFormat;
  131. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  132. if (format == 14 || format == 31 || format == 57 || format == 58)
  133. dataRow[j] = cell.DateCellValue;
  134. else
  135. dataRow[j] = cell.NumericCellValue;
  136. break;
  137. case CellType.String:
  138. dataRow[j] = cell.StringCellValue;
  139. break;
  140. }
  141. }
  142. }
  143. dataTable.Rows.Add(dataRow);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. return dataTable;
  150. }
  151. catch (Exception)
  152. {
  153. if (fs != null)
  154. {
  155. fs.Close();
  156. }
  157. return null;
  158. }
  159. }
  160. /// <summary>
  161. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  162. /// </summary>
  163. /// <param name="DataTable"></param>
  164. /// <returns></returns>
  165. public MemoryStream DataTableToExcel(DataTable DataTable)
  166. {
  167. //创建内存流
  168. MemoryStream ms = new MemoryStream();
  169. //创建一个Book,相当于一个Excel文件
  170. HSSFWorkbook book = new HSSFWorkbook();
  171. //Excel中的Sheet
  172. ISheet sheet = book.CreateSheet("sheet1");
  173. //获取行数量和列数量
  174. int rowNum = DataTable.Rows.Count;
  175. int columnNum = DataTable.Columns.Count;
  176. //设置列的宽度,根据首行的列的内容的长度来设置
  177. for (int i = 0; i < columnNum; i++)
  178. {
  179. int dataLength;
  180. //如果内容比标题短则取标题长度
  181. if (DataTable.Rows[0][i].ToString().Length < DataTable.Columns[i].ColumnName.Length)
  182. {
  183. dataLength = DataTable.Columns[i].ColumnName.Length;
  184. dataLength = dataLength * 300;
  185. }
  186. else
  187. {
  188. dataLength = DataTable.Rows[0][i].ToString().Length;
  189. dataLength = dataLength * 300;
  190. }
  191. sheet.SetColumnWidth(i, dataLength);
  192. }
  193. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  194. IRow row = sheet.CreateRow(0);
  195. //冻结第一行
  196. sheet.CreateFreezePane(0, 1, 0, 1);
  197. ICellStyle style = book.CreateCellStyle();
  198. style.FillForegroundColor = HSSFColor.PaleBlue.Index;
  199. style.FillPattern = FillPattern.BigSpots;
  200. style.FillBackgroundColor = HSSFColor.LightGreen.Index;
  201. //设置边框
  202. style.BorderBottom = BorderStyle.Thick;
  203. style.BorderLeft = BorderStyle.Thick;
  204. style.BorderRight = BorderStyle.Thick;
  205. style.BorderTop = BorderStyle.Thick;
  206. row.HeightInPoints = 20;
  207. //固定第一行
  208. //row.RowStyle.IsLocked=true;
  209. //给第一行的标签赋值样式和值
  210. for (int j = 0; j < columnNum; j++)
  211. {
  212. row.CreateCell(j);
  213. row.Cells[j].CellStyle = style;
  214. row.Cells[j].CellStyle.VerticalAlignment = VerticalAlignment.Center;
  215. row.Cells[j].CellStyle.Alignment = HorizontalAlignment.Center;
  216. row.Cells[j].SetCellValue(DataTable.Columns[j].ColumnName);
  217. }
  218. //将DataTable的值循环赋值给book,Aligment设置居中
  219. //之前已经画了带颜色的第一行,所以从i=1开始画
  220. for (int i = 0; i < rowNum; i++)
  221. {
  222. IRow row1 = sheet.CreateRow(i + 1);
  223. row1.HeightInPoints = 20;
  224. for (int j = 0; j < columnNum; j++)
  225. {
  226. row1.CreateCell(j);
  227. row1.Cells[j].SetCellValue(DataTable.Rows[i][j].ToString());
  228. row1.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.Center;
  229. }
  230. }
  231. //将book的内容写入内存流中返回
  232. book.Write(ms);
  233. return ms;
  234. }
  235. /// <summary>
  236. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  237. /// </summary>
  238. /// <param name="DataTable"></param>
  239. /// <returns></returns>
  240. public MemoryStream DataTableToExcel1(DataTable FirstDT, DataTable DataTable, string Type, int PageSize, bool PrintWID)
  241. {
  242. //创建内存流
  243. MemoryStream ms = new MemoryStream();
  244. //创建一个Book,相当于一个Excel文件
  245. HSSFWorkbook book = new HSSFWorkbook();
  246. //Excel中的Sheet
  247. ISheet sheet = book.CreateSheet("分页");
  248. //获取行数量和列数量
  249. int rowNum = DataTable.Rows.Count;
  250. int columnNum = DataTable.Columns.Count;
  251. ICellStyle style = book.CreateCellStyle();
  252. style.VerticalAlignment = VerticalAlignment.Center;
  253. style.Alignment = HorizontalAlignment.Left;
  254. ICellStyle styleborder = book.CreateCellStyle();
  255. styleborder.BorderBottom = BorderStyle.Thick;
  256. styleborder.BorderTop = BorderStyle.Thick;
  257. styleborder.BorderLeft = BorderStyle.Thick;
  258. styleborder.BorderRight = BorderStyle.Thick;
  259. styleborder.VerticalAlignment = VerticalAlignment.Center;
  260. styleborder.Alignment = HorizontalAlignment.Left;
  261. //设置列的宽度,根据首行的列的内容的长度来设置
  262. for (int i = 0; i < columnNum; i++)
  263. {
  264. if (DataTable.Columns[i].ColumnName.Contains("客户"))
  265. {
  266. DataTable.Columns[i].ColumnName = "客户";
  267. }
  268. if (DataTable.Columns[i].ColumnName.Contains("出货单号"))
  269. {
  270. DataTable.Columns[i].ColumnName = "出货单号";
  271. }
  272. if (DataTable.Columns[i].ColumnName.Contains("型号"))
  273. {
  274. DataTable.Columns[i].ColumnName = "型号";
  275. }
  276. if (DataTable.Columns[i].ColumnName.Contains("销售日期"))
  277. {
  278. DataTable.Columns[i].ColumnName = "销售日期";
  279. }
  280. if (DataTable.Columns[i].ColumnName.ToLower().Contains("rownum"))
  281. {
  282. DataTable.Columns[i].ColumnName = "序号";
  283. }
  284. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_code"))
  285. {
  286. DataTable.Columns[i].ColumnName = "芯片号";
  287. }
  288. if (DataTable.Columns[i].ColumnName.ToLower().Contains("pib_lotno"))
  289. {
  290. DataTable.Columns[i].ColumnName = "LotNo";
  291. }
  292. if (DataTable.Columns[i].ColumnName.ToLower().Contains("io_qty"))
  293. {
  294. DataTable.Columns[i].ColumnName = "数量";
  295. }
  296. }
  297. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  298. IRow row = sheet.CreateRow(0);
  299. //冻结第一行
  300. sheet.CreateFreezePane(0, 1, 0, 1);
  301. //style.FillPattern = FillPattern.BigSpots;
  302. //style.FillBackgroundColor = HSSFColor.LightGreen.Index;
  303. //设置边框
  304. //style.BorderBottom = BorderStyle.Thick;
  305. //style.BorderLeft = BorderStyle.Thick;
  306. //style.BorderRight = BorderStyle.Thick;
  307. //style.BorderTop = BorderStyle.Thick;
  308. row.HeightInPoints = 20;
  309. //固定第一行
  310. //row.RowStyle.IsLocked=true;
  311. //给第一行的标签赋值样式和值
  312. row.CreateCell(0);
  313. row.Cells[0].SetCellValue(" 深爱半导体有限公司芯片出货清单");
  314. row.GetCell(0).CellStyle = style;
  315. //开始绘制的Index
  316. int PaintIndex = 1;
  317. int sumCount = 0;
  318. int totalCount = 0;
  319. switch (Type)
  320. {
  321. case "FixRow":
  322. BaseUtil.CleanDataTableData(FirstDT);
  323. //首页参数拼接
  324. string First_OrderCode = "";
  325. string First_Prspec = "";
  326. string First_Batch = "";
  327. string First_WID = "";
  328. for (int i = 0; i < rowNum; i++)
  329. {
  330. IRow row1 = sheet.CreateRow(PaintIndex);
  331. PaintIndex = PaintIndex + 1;
  332. row1.HeightInPoints = 20;
  333. //不包含的订单号
  334. if (!First_OrderCode.Contains(DataTable.Rows[i]["pd_ordercode"].ToString()))
  335. {
  336. First_OrderCode += DataTable.Rows[i]["pd_ordercode"].ToString() + " ";
  337. }
  338. //不包含的物料型号
  339. if (!First_Prspec.Contains(DataTable.Rows[i]["pr_spec"].ToString()))
  340. {
  341. First_Prspec += DataTable.Rows[i]["pr_spec"].ToString() + " ";
  342. }
  343. //不包含扩撒批号
  344. if (!First_Batch.Contains(DataTable.Rows[i]["扩散批号"].ToString()))
  345. {
  346. First_Batch += DataTable.Rows[i]["扩散批号"].ToString() + " ";
  347. }
  348. //不包含Wafer_id
  349. if (!First_WID.Contains(DataTable.Rows[i]["Wafer_ID"].ToString()))
  350. {
  351. First_WID += DataTable.Rows[i]["Wafer_ID"].ToString() + " ";
  352. }
  353. if (i / PageSize >= 1 && i % PageSize == 0)
  354. {
  355. DataRow dr = FirstDT.NewRow();
  356. dr["型号"] = DataTable.Rows[i]["型号"].ToString();
  357. dr["出货单号"] = DataTable.Rows[i]["出货单号"].ToString();
  358. dr["客户"] = DataTable.Rows[i]["客户"].ToString();
  359. dr["销售日期"] = DataTable.Rows[i]["销售日期"].ToString();
  360. dr["pd_ordercode"] = First_OrderCode;
  361. dr["pr_spec"] = First_Prspec;
  362. dr["ch_splitbatch"] = First_Batch;
  363. dr["ch_waterid"] = First_WID;
  364. dr["num"] = PageSize;
  365. dr["io_qty"] = sumCount;
  366. FirstDT.Rows.Add(dr);
  367. First_OrderCode = "";
  368. First_Prspec = "";
  369. First_Batch = "";
  370. First_WID = "";
  371. for (int j = 0; j < columnNum - 4; j++)
  372. {
  373. if (j == 0)
  374. {
  375. row1.CreateCell(j);
  376. row1.Cells[j].SetCellValue("小计");
  377. }
  378. else if (j == 2)
  379. {
  380. row1.CreateCell(j);
  381. row1.Cells[j].SetCellValue(sumCount);
  382. }
  383. else
  384. {
  385. row1.CreateCell(j);
  386. }
  387. row1.Cells[j].CellStyle = styleborder;
  388. }
  389. sumCount = 0;
  390. row1 = sheet.CreateRow(PaintIndex);
  391. PaintIndex = PaintIndex + 1;
  392. }
  393. //每次到了页数开始分页
  394. if (i % PageSize == 0 || i == rowNum - 1)
  395. {
  396. //第一行添加客户信息
  397. if (i != rowNum - 1)
  398. {
  399. for (int j = 0; j < columnNum - 3; j++)
  400. {
  401. if (j == 0)
  402. {
  403. row1.CreateCell(j);
  404. row1.Cells[j].SetCellValue(DataTable.Rows[i]["客户"].ToString());
  405. }
  406. else if (j == columnNum - 4)
  407. {
  408. row1.CreateCell(j);
  409. row1.Cells[j].SetCellValue(DataTable.Rows[i]["出货单号"].ToString());
  410. }
  411. else
  412. {
  413. row1.CreateCell(j);
  414. }
  415. row1.GetCell(j).CellStyle = style;
  416. }
  417. row1 = sheet.CreateRow(PaintIndex);
  418. PaintIndex = PaintIndex + 1;
  419. //第二行添加型号
  420. for (int j = 0; j < columnNum - 3; j++)
  421. {
  422. if (j == 0)
  423. {
  424. row1.CreateCell(j);
  425. row1.Cells[j].SetCellValue(DataTable.Rows[i]["型号"].ToString());
  426. }
  427. else if (j == columnNum - 4)
  428. {
  429. row1.CreateCell(j);
  430. row1.Cells[j].SetCellValue(DataTable.Rows[i]["销售日期"].ToString());
  431. }
  432. else
  433. {
  434. row1.CreateCell(j);
  435. }
  436. row1.GetCell(j).CellStyle = style;
  437. }
  438. //添加列名
  439. row1 = sheet.CreateRow(PaintIndex);
  440. PaintIndex = PaintIndex + 1;
  441. for (int j = 4; j < columnNum; j++)
  442. {
  443. row1.CreateCell(j - 4);
  444. row1.Cells[j - 4].CellStyle = styleborder;
  445. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  446. {
  447. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  448. }
  449. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  450. {
  451. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  452. }
  453. else
  454. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].ColumnName);
  455. //如果chw_itemname1的值为空,则值为100和0,其中一列不显示,不显示
  456. if (DataTable.Columns[j].ColumnName.ToLower().Contains("chw_itemname") || DataTable.Columns[j].ColumnName.ToLower().Contains("pr_spec") || DataTable.Columns[j].ColumnName.ToLower().Contains("pd_ordercode"))
  457. {
  458. sheet.SetColumnHidden(j - 4, true);
  459. }
  460. }
  461. row1 = sheet.CreateRow(PaintIndex);
  462. PaintIndex = PaintIndex + 1;
  463. }
  464. }
  465. //添加数据内容
  466. for (int j = 4; j < columnNum; j++)
  467. {
  468. string Data = DataTable.Rows[i][j].ToString();
  469. row1.CreateCell(j - 4);
  470. row1.Cells[j - 4].SetCellValue(Data);
  471. row1.GetCell(j - 4).CellStyle = styleborder;
  472. if (DataTable.Columns[j].ColumnName == "数量")
  473. {
  474. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  475. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  476. }
  477. if (DataTable.Columns[j].ColumnName == "序号")
  478. {
  479. row1.Cells[j - 4].SetCellValue(i + 1);
  480. }
  481. }
  482. if (i == rowNum - 1)
  483. {
  484. DataRow dr = FirstDT.NewRow();
  485. dr["型号"] = DataTable.Rows[i]["型号"].ToString();
  486. dr["出货单号"] = DataTable.Rows[i]["出货单号"].ToString();
  487. dr["客户"] = DataTable.Rows[i]["客户"].ToString();
  488. dr["销售日期"] = DataTable.Rows[i]["销售日期"].ToString();
  489. dr["pd_ordercode"] = First_OrderCode;
  490. dr["pr_spec"] = First_Prspec;
  491. dr["ch_splitbatch"] = First_Batch;
  492. dr["ch_waterid"] = First_WID;
  493. dr["num"] =(i% PageSize)+1;
  494. dr["io_qty"] = sumCount;
  495. FirstDT.Rows.Add(dr);
  496. row1 = sheet.CreateRow(PaintIndex);
  497. PaintIndex = PaintIndex + 1;
  498. for (int j = 0; j < columnNum - 4; j++)
  499. {
  500. if (j == 0)
  501. {
  502. row1.CreateCell(j);
  503. row1.Cells[j].SetCellValue("小计");
  504. }
  505. else if (j == 2)
  506. {
  507. row1.CreateCell(j);
  508. row1.Cells[j].SetCellValue(sumCount);
  509. }
  510. else
  511. {
  512. row1.CreateCell(j);
  513. }
  514. row1.Cells[j].CellStyle = styleborder;
  515. }
  516. row1 = sheet.CreateRow(PaintIndex);
  517. PaintIndex = PaintIndex + 1;
  518. for (int j = 0; j < columnNum - 3; j++)
  519. {
  520. if (j == 0)
  521. {
  522. row1.CreateCell(j);
  523. row1.Cells[j].SetCellValue("备注");
  524. }
  525. else if (j == 2)
  526. {
  527. row1.CreateCell(j);
  528. row1.Cells[j].SetCellValue(totalCount);
  529. }
  530. else if (j == columnNum - 5)
  531. {
  532. row1.CreateCell(j);
  533. row1.Cells[j].SetCellValue(rowNum);
  534. }
  535. else if (j == columnNum - 4)
  536. {
  537. row1.CreateCell(j);
  538. row1.Cells[j].SetCellValue("片");
  539. }
  540. else
  541. {
  542. row1.CreateCell(j);
  543. }
  544. row1.Cells[j].CellStyle = style;
  545. }
  546. }
  547. }
  548. for (int i = 0; i < sheet.LastRowNum; i++)
  549. {
  550. if (i != 0)
  551. {
  552. sheet.AutoSizeColumn(i);
  553. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  554. }
  555. }
  556. break;
  557. case "BatchCode":
  558. string LastBatchCode = "";
  559. for (int i = 0; i < rowNum; i++)
  560. {
  561. IRow row1 = sheet.CreateRow(PaintIndex);
  562. PaintIndex = PaintIndex + 1;
  563. row1.HeightInPoints = 20;
  564. //如果批号不相等的时候
  565. if (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["扩散批号"].ToString())
  566. {
  567. for (int j = 0; j < columnNum - 4; j++)
  568. {
  569. if (j == 0)
  570. {
  571. row1.CreateCell(j);
  572. row1.Cells[j].SetCellValue("小计");
  573. }
  574. else if (j == 2)
  575. {
  576. row1.CreateCell(j);
  577. row1.Cells[j].SetCellValue(sumCount);
  578. }
  579. else
  580. {
  581. row1.CreateCell(j);
  582. }
  583. row1.Cells[j].CellStyle = styleborder;
  584. }
  585. sumCount = 0;
  586. row1 = sheet.CreateRow(PaintIndex);
  587. PaintIndex = PaintIndex + 1;
  588. }
  589. //每次到了页数开始分页
  590. if (LastBatchCode == "" || (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["扩散批号"].ToString()) || i == rowNum - 1)
  591. {
  592. LastBatchCode = DataTable.Rows[i]["扩散批号"].ToString();
  593. //第一行添加客户信息
  594. if (i != rowNum - 1)
  595. {
  596. for (int j = 0; j < columnNum - 3; j++)
  597. {
  598. if (j == 0)
  599. {
  600. row1.CreateCell(j);
  601. row1.Cells[j].SetCellValue(DataTable.Rows[i]["客户"].ToString());
  602. }
  603. else if (j == columnNum - 4)
  604. {
  605. row1.CreateCell(j);
  606. row1.Cells[j].SetCellValue(DataTable.Rows[i]["出货单号"].ToString());
  607. }
  608. else
  609. {
  610. row1.CreateCell(j);
  611. }
  612. row1.GetCell(j).CellStyle = style;
  613. }
  614. row1 = sheet.CreateRow(PaintIndex);
  615. PaintIndex = PaintIndex + 1;
  616. //第二行添加型号
  617. for (int j = 0; j < columnNum - 3; j++)
  618. {
  619. if (j == 0)
  620. {
  621. row1.CreateCell(j);
  622. row1.Cells[j].SetCellValue(DataTable.Rows[i]["型号"].ToString());
  623. }
  624. else if (j == columnNum - 4)
  625. {
  626. row1.CreateCell(j);
  627. row1.Cells[j].SetCellValue(DataTable.Rows[i]["销售日期"].ToString());
  628. }
  629. else
  630. {
  631. row1.CreateCell(j);
  632. }
  633. row1.GetCell(j).CellStyle = style;
  634. }
  635. //添加列名
  636. row1 = sheet.CreateRow(PaintIndex);
  637. PaintIndex = PaintIndex + 1;
  638. for (int j = 4; j < columnNum; j++)
  639. {
  640. row1.CreateCell(j - 4);
  641. row1.Cells[j - 4].CellStyle = styleborder;
  642. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  643. {
  644. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  645. }
  646. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  647. {
  648. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  649. }
  650. else
  651. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].ColumnName);
  652. if (DataTable.Columns[j].ColumnName.ToLower().Contains("chw_itemname") || DataTable.Columns[j].ColumnName.ToLower().Contains("pr_spec") || DataTable.Columns[j].ColumnName.ToLower().Contains("pd_ordercode"))
  653. {
  654. sheet.SetColumnHidden(j - 4, true);
  655. }
  656. }
  657. row1 = sheet.CreateRow(PaintIndex);
  658. PaintIndex = PaintIndex + 1;
  659. }
  660. }
  661. //添加数据内容
  662. for (int j = 4; j < columnNum; j++)
  663. {
  664. string Data = DataTable.Rows[i][j].ToString();
  665. row1.CreateCell(j - 4);
  666. row1.Cells[j - 4].SetCellValue(Data);
  667. row1.GetCell(j - 4).CellStyle = styleborder;
  668. if (DataTable.Columns[j].ColumnName == "数量")
  669. {
  670. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  671. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  672. }
  673. if (DataTable.Columns[j].ColumnName == "序号")
  674. {
  675. row1.Cells[j - 4].SetCellValue(i + 1);
  676. }
  677. }
  678. if (i == rowNum - 1)
  679. {
  680. row1 = sheet.CreateRow(PaintIndex);
  681. PaintIndex = PaintIndex + 1;
  682. for (int j = 0; j < columnNum - 4; j++)
  683. {
  684. if (j == 0)
  685. {
  686. row1.CreateCell(j);
  687. row1.Cells[j].SetCellValue("小计");
  688. }
  689. else if (j == 2)
  690. {
  691. row1.CreateCell(j);
  692. row1.Cells[j].SetCellValue(sumCount);
  693. }
  694. else
  695. {
  696. row1.CreateCell(j);
  697. }
  698. row1.Cells[j].CellStyle = styleborder;
  699. }
  700. row1 = sheet.CreateRow(PaintIndex);
  701. PaintIndex = PaintIndex + 1;
  702. for (int j = 0; j < columnNum - 3; j++)
  703. {
  704. if (j == 0)
  705. {
  706. row1.CreateCell(j);
  707. row1.Cells[j].SetCellValue("备注");
  708. }
  709. else if (j == 2)
  710. {
  711. row1.CreateCell(j);
  712. row1.Cells[j].SetCellValue(totalCount);
  713. }
  714. else if (j == columnNum - 5)
  715. {
  716. row1.CreateCell(j);
  717. row1.Cells[j].SetCellValue(rowNum);
  718. }
  719. else if (j == columnNum - 4)
  720. {
  721. row1.CreateCell(j);
  722. row1.Cells[j].SetCellValue("片");
  723. }
  724. else
  725. {
  726. row1.CreateCell(j);
  727. }
  728. row1.Cells[j].CellStyle = style;
  729. }
  730. }
  731. }
  732. for (int i = 0; i < sheet.LastRowNum; i++)
  733. {
  734. if (i != 0)
  735. {
  736. sheet.AutoSizeColumn(i);
  737. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  738. }
  739. }
  740. break;
  741. case "BoxCode":
  742. string LastBoxCode = "";
  743. for (int i = 0; i < rowNum; i++)
  744. {
  745. IRow row1 = sheet.CreateRow(PaintIndex);
  746. PaintIndex = PaintIndex + 1;
  747. row1.HeightInPoints = 20;
  748. //如果批号不相等的时候
  749. if (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["盒号"].ToString())
  750. {
  751. for (int j = 0; j < columnNum - 4; j++)
  752. {
  753. if (j == 0)
  754. {
  755. row1.CreateCell(j);
  756. row1.Cells[j].SetCellValue("小计");
  757. }
  758. else if (j == 2)
  759. {
  760. row1.CreateCell(j);
  761. row1.Cells[j].SetCellValue(sumCount);
  762. }
  763. else
  764. {
  765. row1.CreateCell(j);
  766. }
  767. row1.Cells[j].CellStyle = styleborder;
  768. }
  769. sumCount = 0;
  770. row1 = sheet.CreateRow(PaintIndex);
  771. PaintIndex = PaintIndex + 1;
  772. }
  773. //每次到了页数开始分页
  774. if (LastBoxCode == "" || (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["盒号"].ToString()) || i == rowNum - 1)
  775. {
  776. LastBoxCode = DataTable.Rows[i]["盒号"].ToString();
  777. //第一行添加客户信息
  778. if (i != rowNum - 1)
  779. {
  780. for (int j = 0; j < columnNum - 3; j++)
  781. {
  782. if (j == 0)
  783. {
  784. row1.CreateCell(j);
  785. row1.Cells[j].SetCellValue(DataTable.Rows[i]["客户"].ToString());
  786. }
  787. else if (j == columnNum - 4)
  788. {
  789. row1.CreateCell(j);
  790. row1.Cells[j].SetCellValue(DataTable.Rows[i]["出货单号"].ToString());
  791. }
  792. else
  793. {
  794. row1.CreateCell(j);
  795. }
  796. row1.GetCell(j).CellStyle = style;
  797. }
  798. row1 = sheet.CreateRow(PaintIndex);
  799. PaintIndex = PaintIndex + 1;
  800. //第二行添加型号
  801. for (int j = 0; j < columnNum - 3; j++)
  802. {
  803. if (j == 0)
  804. {
  805. row1.CreateCell(j);
  806. row1.Cells[j].SetCellValue(DataTable.Rows[i]["型号"].ToString());
  807. }
  808. else if (j == columnNum - 4)
  809. {
  810. row1.CreateCell(j);
  811. row1.Cells[j].SetCellValue(DataTable.Rows[i]["销售日期"].ToString());
  812. }
  813. else
  814. {
  815. row1.CreateCell(j);
  816. }
  817. row1.GetCell(j).CellStyle = style;
  818. }
  819. //添加列名
  820. row1 = sheet.CreateRow(PaintIndex);
  821. PaintIndex = PaintIndex + 1;
  822. for (int j = 4; j < columnNum; j++)
  823. {
  824. row1.CreateCell(j - 4);
  825. row1.Cells[j - 4].CellStyle = styleborder;
  826. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  827. {
  828. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  829. }
  830. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  831. {
  832. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  833. }
  834. else
  835. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].ColumnName);
  836. if (DataTable.Columns[j].ColumnName.ToLower().Contains("chw_itemname") || DataTable.Columns[j].ColumnName.ToLower().Contains("pr_spec") || DataTable.Columns[j].ColumnName.ToLower().Contains("pd_ordercode"))
  837. {
  838. sheet.SetColumnHidden(j - 4, true);
  839. }
  840. }
  841. row1 = sheet.CreateRow(PaintIndex);
  842. PaintIndex = PaintIndex + 1;
  843. }
  844. }
  845. //添加数据内容
  846. for (int j = 4; j < columnNum; j++)
  847. {
  848. string Data = DataTable.Rows[i][j].ToString();
  849. row1.CreateCell(j - 4);
  850. row1.Cells[j - 4].SetCellValue(Data);
  851. row1.GetCell(j - 4).CellStyle = styleborder;
  852. if (DataTable.Columns[j].ColumnName == "数量")
  853. {
  854. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  855. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  856. }
  857. if (DataTable.Columns[j].ColumnName == "序号")
  858. {
  859. row1.Cells[j - 4].SetCellValue(i + 1);
  860. }
  861. }
  862. if (i == rowNum - 1)
  863. {
  864. row1 = sheet.CreateRow(PaintIndex);
  865. PaintIndex = PaintIndex + 1;
  866. for (int j = 0; j < columnNum - 4; j++)
  867. {
  868. if (j == 0)
  869. {
  870. row1.CreateCell(j);
  871. row1.Cells[j].SetCellValue("小计");
  872. }
  873. else if (j == 2)
  874. {
  875. row1.CreateCell(j);
  876. row1.Cells[j].SetCellValue(sumCount);
  877. }
  878. else
  879. {
  880. row1.CreateCell(j);
  881. }
  882. row1.Cells[j].CellStyle = styleborder;
  883. }
  884. row1 = sheet.CreateRow(PaintIndex);
  885. PaintIndex = PaintIndex + 1;
  886. for (int j = 0; j < columnNum - 3; j++)
  887. {
  888. if (j == 0)
  889. {
  890. row1.CreateCell(j);
  891. row1.Cells[j].SetCellValue("备注");
  892. }
  893. else if (j == 2)
  894. {
  895. row1.CreateCell(j);
  896. row1.Cells[j].SetCellValue(totalCount);
  897. }
  898. else if (j == columnNum - 5)
  899. {
  900. row1.CreateCell(j);
  901. row1.Cells[j].SetCellValue(rowNum);
  902. }
  903. else if (j == columnNum - 4)
  904. {
  905. row1.CreateCell(j);
  906. row1.Cells[j].SetCellValue("片");
  907. }
  908. else
  909. {
  910. row1.CreateCell(j);
  911. }
  912. row1.Cells[j].CellStyle = style;
  913. }
  914. }
  915. }
  916. for (int i = 0; i < sheet.LastRowNum; i++)
  917. {
  918. if (i != 0)
  919. {
  920. sheet.AutoSizeColumn(i);
  921. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  922. }
  923. }
  924. break;
  925. default:
  926. break;
  927. }
  928. //填充首页
  929. sumCount = 0;
  930. totalCount = 0;
  931. PaintIndex = 1;
  932. ISheet sheet2 = book.CreateSheet("首页");
  933. row = sheet2.CreateRow(0);
  934. row.CreateCell(0);
  935. row.Cells[0].SetCellValue(" 深爱半导体有限公司芯片出货清单");
  936. row.GetCell(0).CellStyle = style;
  937. rowNum = FirstDT.Rows.Count;
  938. columnNum = FirstDT.Columns.Count;
  939. for (int i = 0; i < columnNum; i++)
  940. {
  941. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("pd_ordercode"))
  942. {
  943. FirstDT.Columns[i].ColumnName = "订单号";
  944. }
  945. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("pr_spec"))
  946. {
  947. FirstDT.Columns[i].ColumnName = "产品型号";
  948. }
  949. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_splitbatch"))
  950. {
  951. FirstDT.Columns[i].ColumnName = "批号";
  952. }
  953. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  954. {
  955. FirstDT.Columns[i].ColumnName = "片号";
  956. }
  957. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("num"))
  958. {
  959. FirstDT.Columns[i].ColumnName = "片数";
  960. }
  961. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("io_qty"))
  962. {
  963. FirstDT.Columns[i].ColumnName = "管芯数";
  964. }
  965. }
  966. for (int i = 0; i < rowNum; i++)
  967. {
  968. IRow row1 = sheet2.CreateRow(PaintIndex);
  969. PaintIndex = PaintIndex + 1;
  970. row1.HeightInPoints = 20;
  971. //只需要绘制一行
  972. if (i == 0)
  973. {
  974. for (int j = 0; j < columnNum - 3; j++)
  975. {
  976. if (j == 0)
  977. {
  978. row1.CreateCell(j);
  979. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["客户"].ToString());
  980. }
  981. else if (j == columnNum - 4)
  982. {
  983. row1.CreateCell(j);
  984. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["出货单号"].ToString());
  985. }
  986. else
  987. {
  988. row1.CreateCell(j);
  989. }
  990. row1.GetCell(j).CellStyle = style;
  991. }
  992. row1 = sheet2.CreateRow(PaintIndex);
  993. PaintIndex = PaintIndex + 1;
  994. //第二行添加型号
  995. for (int j = 0; j < columnNum - 3; j++)
  996. {
  997. if (j == 0)
  998. {
  999. row1.CreateCell(j);
  1000. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["型号"].ToString());
  1001. }
  1002. else if (j == columnNum - 4)
  1003. {
  1004. row1.CreateCell(j);
  1005. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["销售日期"].ToString());
  1006. }
  1007. else
  1008. {
  1009. row1.CreateCell(j);
  1010. }
  1011. row1.GetCell(j).CellStyle = style;
  1012. }
  1013. row1 = sheet2.CreateRow(PaintIndex);
  1014. PaintIndex = PaintIndex + 1;
  1015. //添加列名
  1016. for (int j = 4; j < columnNum; j++)
  1017. {
  1018. row1.CreateCell(j - 4);
  1019. row1.Cells[j - 4].CellStyle = styleborder;
  1020. row1.Cells[j - 4].SetCellValue(FirstDT.Columns[j].ColumnName);
  1021. }
  1022. row1 = sheet2.CreateRow(PaintIndex);
  1023. PaintIndex = PaintIndex + 1;
  1024. }
  1025. //添加数据内容
  1026. for (int j = 4; j < columnNum; j++)
  1027. {
  1028. string Data = FirstDT.Rows[i][j].ToString();
  1029. row1.CreateCell(j - 4);
  1030. row1.Cells[j - 4].SetCellValue(Data);
  1031. row1.GetCell(j - 4).CellStyle = styleborder;
  1032. if (FirstDT.Columns[j].ColumnName == "片数")
  1033. {
  1034. sumCount += int.Parse(Data);
  1035. }
  1036. if (FirstDT.Columns[j].ColumnName == "管芯数")
  1037. {
  1038. totalCount += int.Parse(Data);
  1039. }
  1040. }
  1041. //添加总计行
  1042. if (i == rowNum - 1)
  1043. {
  1044. row1 = sheet2.CreateRow(PaintIndex);
  1045. PaintIndex = PaintIndex + 1;
  1046. for (int j = 0; j < columnNum - 4; j++)
  1047. {
  1048. if (j == 0)
  1049. {
  1050. row1.CreateCell(j);
  1051. row1.Cells[j].CellStyle = styleborder;
  1052. row1.Cells[j].SetCellValue("总计");
  1053. }
  1054. else if (j == columnNum - 6)
  1055. {
  1056. row1.CreateCell(j);
  1057. row1.Cells[j].CellStyle = styleborder;
  1058. row1.Cells[j].SetCellValue(sumCount);
  1059. }
  1060. else if (j == columnNum - 5)
  1061. {
  1062. row1.CreateCell(j);
  1063. row1.Cells[j].CellStyle = styleborder;
  1064. row1.Cells[j].SetCellValue(totalCount);
  1065. }
  1066. else
  1067. {
  1068. row1.CreateCell(j);
  1069. row1.Cells[j].CellStyle = styleborder;
  1070. }
  1071. }
  1072. }
  1073. }
  1074. for (int i = 0; i < sheet2.LastRowNum; i++)
  1075. {
  1076. if (i != 0)
  1077. {
  1078. sheet2.AutoSizeColumn(i);
  1079. sheet2.SetColumnWidth(i, sheet2.GetColumnWidth(i) + 1000);
  1080. }
  1081. }
  1082. //将book的内容写入内存流中返回
  1083. book.Write(ms);
  1084. return ms;
  1085. }
  1086. }
  1087. }