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. //清理系统取出来的数据
  323. BaseUtil.CleanDataTableData(FirstDT);
  324. //首页参数拼接
  325. string First_OrderCode = "";
  326. string First_Prspec = "";
  327. string First_Batch = "";
  328. ArrayList<string> First_WID = new ArrayList<string>();
  329. for (int i = 0; i < rowNum; i++)
  330. {
  331. IRow row1 = sheet.CreateRow(PaintIndex);
  332. PaintIndex = PaintIndex + 1;
  333. row1.HeightInPoints = 20;
  334. //不包含的订单号
  335. if (DataTable.Columns.Contains("pd_ordercode") && !First_OrderCode.Contains(DataTable.Rows[i]["pd_ordercode"].ToString()))
  336. {
  337. First_OrderCode += DataTable.Rows[i]["pd_ordercode"].ToString() + " ";
  338. }
  339. //不包含的物料型号
  340. if (DataTable.Columns.Contains("pr_spec") && !First_Prspec.Contains(DataTable.Rows[i]["pr_spec"].ToString()))
  341. {
  342. First_Prspec += DataTable.Rows[i]["pr_spec"].ToString() + " ";
  343. }
  344. //不包含扩撒批号
  345. if (DataTable.Columns.Contains("扩散批号") && !First_Batch.Contains(DataTable.Rows[i]["扩散批号"].ToString()))
  346. {
  347. First_Batch += DataTable.Rows[i]["扩散批号"].ToString() + " ";
  348. }
  349. //不包含Wafer_id
  350. if (DataTable.Columns.Contains("Wafer_ID") &&!First_WID.Contains(DataTable.Rows[i]["Wafer_ID"].ToString()))
  351. {
  352. First_WID.Add(DataTable.Rows[i]["Wafer_ID"].ToString());
  353. }
  354. if (i / PageSize >= 1 && i % PageSize == 0)
  355. {
  356. DataRow dr = FirstDT.NewRow();
  357. dr["型号"] = DataTable.Rows[i]["型号"].ToString();
  358. dr["出货单号"] = DataTable.Rows[i]["出货单号"].ToString();
  359. dr["客户"] = DataTable.Rows[i]["客户"].ToString();
  360. dr["销售日期"] = DataTable.Rows[i]["销售日期"].ToString();
  361. dr["pd_ordercode"] = First_OrderCode;
  362. dr["pr_spec"] = First_Prspec;
  363. dr["ch_splitbatch"] = First_Batch;
  364. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID," ");
  365. dr["num"] = PageSize;
  366. dr["io_qty"] = sumCount;
  367. FirstDT.Rows.Add(dr);
  368. First_OrderCode = "";
  369. First_Prspec = "";
  370. First_Batch = "";
  371. First_WID.Clear();
  372. for (int j = 0; j < columnNum - 4; j++)
  373. {
  374. if (j == 0)
  375. {
  376. row1.CreateCell(j);
  377. row1.Cells[j].SetCellValue("小计");
  378. }
  379. else if (j == 2)
  380. {
  381. row1.CreateCell(j);
  382. row1.Cells[j].SetCellValue(sumCount);
  383. }
  384. else
  385. {
  386. row1.CreateCell(j);
  387. }
  388. row1.Cells[j].CellStyle = styleborder;
  389. }
  390. sumCount = 0;
  391. row1 = sheet.CreateRow(PaintIndex);
  392. PaintIndex = PaintIndex + 1;
  393. }
  394. //每次到了页数开始分页
  395. if (i % PageSize == 0 || i == rowNum - 1)
  396. {
  397. //第一行添加客户信息 rownum只有一行的情
  398. if (i != rowNum - 1 || rowNum == 1)
  399. {
  400. for (int j = 0; j < columnNum - 3; j++)
  401. {
  402. if (j == 0)
  403. {
  404. row1.CreateCell(j);
  405. row1.Cells[j].SetCellValue(DataTable.Rows[i]["客户"].ToString());
  406. }
  407. else if (j == columnNum - 4)
  408. {
  409. row1.CreateCell(j);
  410. row1.Cells[j].SetCellValue(DataTable.Rows[i]["出货单号"].ToString());
  411. }
  412. else
  413. {
  414. row1.CreateCell(j);
  415. }
  416. row1.GetCell(j).CellStyle = style;
  417. }
  418. row1 = sheet.CreateRow(PaintIndex);
  419. PaintIndex = PaintIndex + 1;
  420. //第二行添加型号
  421. for (int j = 0; j < columnNum - 3; j++)
  422. {
  423. if (j == 0)
  424. {
  425. row1.CreateCell(j);
  426. row1.Cells[j].SetCellValue(DataTable.Rows[i]["型号"].ToString());
  427. }
  428. else if (j == columnNum - 4)
  429. {
  430. row1.CreateCell(j);
  431. row1.Cells[j].SetCellValue(DataTable.Rows[i]["销售日期"].ToString());
  432. }
  433. else
  434. {
  435. row1.CreateCell(j);
  436. }
  437. row1.GetCell(j).CellStyle = style;
  438. }
  439. //添加列名
  440. row1 = sheet.CreateRow(PaintIndex);
  441. PaintIndex = PaintIndex + 1;
  442. for (int j = 4; j < columnNum; j++)
  443. {
  444. row1.CreateCell(j - 4);
  445. row1.Cells[j - 4].CellStyle = styleborder;
  446. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  447. {
  448. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  449. }
  450. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  451. {
  452. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  453. }
  454. else
  455. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].ColumnName);
  456. //如果chw_itemname1的值为空,则值为100和0,其中一列不显示,不显示
  457. 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"))
  458. {
  459. sheet.SetColumnHidden(j - 4, true);
  460. }
  461. }
  462. row1 = sheet.CreateRow(PaintIndex);
  463. PaintIndex = PaintIndex + 1;
  464. }
  465. }
  466. //添加数据内容
  467. for (int j = 4; j < columnNum; j++)
  468. {
  469. string Data = DataTable.Rows[i][j].ToString();
  470. row1.CreateCell(j - 4);
  471. row1.Cells[j - 4].SetCellValue(Data);
  472. row1.GetCell(j - 4).CellStyle = styleborder;
  473. if (DataTable.Columns[j].ColumnName == "数量")
  474. {
  475. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  476. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  477. }
  478. if (DataTable.Columns[j].ColumnName == "序号")
  479. {
  480. row1.Cells[j - 4].SetCellValue(i + 1);
  481. }
  482. }
  483. if (i == rowNum - 1)
  484. {
  485. DataRow dr = FirstDT.NewRow();
  486. dr["型号"] = DataTable.Rows[i]["型号"].ToString();
  487. dr["出货单号"] = DataTable.Rows[i]["出货单号"].ToString();
  488. dr["客户"] = DataTable.Rows[i]["客户"].ToString();
  489. dr["销售日期"] = DataTable.Rows[i]["销售日期"].ToString();
  490. dr["pd_ordercode"] = First_OrderCode;
  491. dr["pr_spec"] = First_Prspec;
  492. dr["ch_splitbatch"] = First_Batch;
  493. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  494. dr["num"] = (i % PageSize) + 1;
  495. dr["io_qty"] = sumCount;
  496. FirstDT.Rows.Add(dr);
  497. row1 = sheet.CreateRow(PaintIndex);
  498. PaintIndex = PaintIndex + 1;
  499. for (int j = 0; j < columnNum - 4; j++)
  500. {
  501. if (j == 0)
  502. {
  503. row1.CreateCell(j);
  504. row1.Cells[j].SetCellValue("小计");
  505. }
  506. else if (j == 2)
  507. {
  508. row1.CreateCell(j);
  509. row1.Cells[j].SetCellValue(sumCount);
  510. }
  511. else
  512. {
  513. row1.CreateCell(j);
  514. }
  515. row1.Cells[j].CellStyle = styleborder;
  516. }
  517. row1 = sheet.CreateRow(PaintIndex);
  518. PaintIndex = PaintIndex + 1;
  519. for (int j = 0; j < columnNum - 3; j++)
  520. {
  521. if (j == 0)
  522. {
  523. row1.CreateCell(j);
  524. row1.Cells[j].SetCellValue("备注");
  525. }
  526. else if (j == 2)
  527. {
  528. row1.CreateCell(j);
  529. row1.Cells[j].SetCellValue(totalCount);
  530. }
  531. //原本是j == columnNum - 5因为还有spec和order两列隐藏列,所以需要在往后移动
  532. else if (j == columnNum - 7)
  533. {
  534. row1.CreateCell(j);
  535. row1.Cells[j].SetCellValue(rowNum);
  536. }
  537. else if (j == columnNum - 4)
  538. {
  539. row1.CreateCell(j);
  540. row1.Cells[j].SetCellValue("片");
  541. }
  542. else
  543. {
  544. row1.CreateCell(j);
  545. }
  546. row1.Cells[j].CellStyle = style;
  547. }
  548. }
  549. }
  550. for (int i = 0; i < sheet.LastRowNum; i++)
  551. {
  552. if (i != 0)
  553. {
  554. sheet.AutoSizeColumn(i);
  555. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  556. }
  557. }
  558. break;
  559. case "BatchCode":
  560. string LastBatchCode = "";
  561. for (int i = 0; i < rowNum; i++)
  562. {
  563. IRow row1 = sheet.CreateRow(PaintIndex);
  564. PaintIndex = PaintIndex + 1;
  565. row1.HeightInPoints = 20;
  566. //如果批号不相等的时候
  567. if (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["扩散批号"].ToString())
  568. {
  569. for (int j = 0; j < columnNum - 4; j++)
  570. {
  571. if (j == 0)
  572. {
  573. row1.CreateCell(j);
  574. row1.Cells[j].SetCellValue("小计");
  575. }
  576. else if (j == 2)
  577. {
  578. row1.CreateCell(j);
  579. row1.Cells[j].SetCellValue(sumCount);
  580. }
  581. else
  582. {
  583. row1.CreateCell(j);
  584. }
  585. row1.Cells[j].CellStyle = styleborder;
  586. }
  587. sumCount = 0;
  588. row1 = sheet.CreateRow(PaintIndex);
  589. PaintIndex = PaintIndex + 1;
  590. }
  591. //每次到了页数开始分页
  592. if (LastBatchCode == "" || (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["扩散批号"].ToString()) || i == rowNum - 1)
  593. {
  594. LastBatchCode = DataTable.Rows[i]["扩散批号"].ToString();
  595. //第一行添加客户信息
  596. if (i != rowNum - 1)
  597. {
  598. for (int j = 0; j < columnNum - 3; j++)
  599. {
  600. if (j == 0)
  601. {
  602. row1.CreateCell(j);
  603. row1.Cells[j].SetCellValue(DataTable.Rows[i]["客户"].ToString());
  604. }
  605. else if (j == columnNum - 4)
  606. {
  607. row1.CreateCell(j);
  608. row1.Cells[j].SetCellValue(DataTable.Rows[i]["出货单号"].ToString());
  609. }
  610. else
  611. {
  612. row1.CreateCell(j);
  613. }
  614. row1.GetCell(j).CellStyle = style;
  615. }
  616. row1 = sheet.CreateRow(PaintIndex);
  617. PaintIndex = PaintIndex + 1;
  618. //第二行添加型号
  619. for (int j = 0; j < columnNum - 3; j++)
  620. {
  621. if (j == 0)
  622. {
  623. row1.CreateCell(j);
  624. row1.Cells[j].SetCellValue(DataTable.Rows[i]["型号"].ToString());
  625. }
  626. else if (j == columnNum - 4)
  627. {
  628. row1.CreateCell(j);
  629. row1.Cells[j].SetCellValue(DataTable.Rows[i]["销售日期"].ToString());
  630. }
  631. else
  632. {
  633. row1.CreateCell(j);
  634. }
  635. row1.GetCell(j).CellStyle = style;
  636. }
  637. //添加列名
  638. row1 = sheet.CreateRow(PaintIndex);
  639. PaintIndex = PaintIndex + 1;
  640. for (int j = 4; j < columnNum; j++)
  641. {
  642. row1.CreateCell(j - 4);
  643. row1.Cells[j - 4].CellStyle = styleborder;
  644. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  645. {
  646. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  647. }
  648. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  649. {
  650. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  651. }
  652. else
  653. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].ColumnName);
  654. 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"))
  655. {
  656. sheet.SetColumnHidden(j - 4, true);
  657. }
  658. }
  659. row1 = sheet.CreateRow(PaintIndex);
  660. PaintIndex = PaintIndex + 1;
  661. }
  662. }
  663. //添加数据内容
  664. for (int j = 4; j < columnNum; j++)
  665. {
  666. string Data = DataTable.Rows[i][j].ToString();
  667. row1.CreateCell(j - 4);
  668. row1.Cells[j - 4].SetCellValue(Data);
  669. row1.GetCell(j - 4).CellStyle = styleborder;
  670. if (DataTable.Columns[j].ColumnName == "数量")
  671. {
  672. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  673. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  674. }
  675. if (DataTable.Columns[j].ColumnName == "序号")
  676. {
  677. row1.Cells[j - 4].SetCellValue(i + 1);
  678. }
  679. }
  680. if (i == rowNum - 1)
  681. {
  682. row1 = sheet.CreateRow(PaintIndex);
  683. PaintIndex = PaintIndex + 1;
  684. for (int j = 0; j < columnNum - 4; j++)
  685. {
  686. if (j == 0)
  687. {
  688. row1.CreateCell(j);
  689. row1.Cells[j].SetCellValue("小计");
  690. }
  691. else if (j == 2)
  692. {
  693. row1.CreateCell(j);
  694. row1.Cells[j].SetCellValue(sumCount);
  695. }
  696. else
  697. {
  698. row1.CreateCell(j);
  699. }
  700. row1.Cells[j].CellStyle = styleborder;
  701. }
  702. row1 = sheet.CreateRow(PaintIndex);
  703. PaintIndex = PaintIndex + 1;
  704. for (int j = 0; j < columnNum - 3; j++)
  705. {
  706. if (j == 0)
  707. {
  708. row1.CreateCell(j);
  709. row1.Cells[j].SetCellValue("备注");
  710. }
  711. else if (j == 2)
  712. {
  713. row1.CreateCell(j);
  714. row1.Cells[j].SetCellValue(totalCount);
  715. }
  716. else if (j == columnNum - 7)
  717. {
  718. row1.CreateCell(j);
  719. row1.Cells[j].SetCellValue(rowNum);
  720. }
  721. else if (j == columnNum - 4)
  722. {
  723. row1.CreateCell(j);
  724. row1.Cells[j].SetCellValue("片");
  725. }
  726. else
  727. {
  728. row1.CreateCell(j);
  729. }
  730. row1.Cells[j].CellStyle = style;
  731. }
  732. }
  733. }
  734. for (int i = 0; i < sheet.LastRowNum; i++)
  735. {
  736. if (i != 0)
  737. {
  738. sheet.AutoSizeColumn(i);
  739. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  740. }
  741. }
  742. break;
  743. case "BoxCode":
  744. string LastBoxCode = "";
  745. for (int i = 0; i < rowNum; i++)
  746. {
  747. IRow row1 = sheet.CreateRow(PaintIndex);
  748. PaintIndex = PaintIndex + 1;
  749. row1.HeightInPoints = 20;
  750. //如果批号不相等的时候
  751. if (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["盒号"].ToString())
  752. {
  753. for (int j = 0; j < columnNum - 4; j++)
  754. {
  755. if (j == 0)
  756. {
  757. row1.CreateCell(j);
  758. row1.Cells[j].SetCellValue("小计");
  759. }
  760. else if (j == 2)
  761. {
  762. row1.CreateCell(j);
  763. row1.Cells[j].SetCellValue(sumCount);
  764. }
  765. else
  766. {
  767. row1.CreateCell(j);
  768. }
  769. row1.Cells[j].CellStyle = styleborder;
  770. }
  771. sumCount = 0;
  772. row1 = sheet.CreateRow(PaintIndex);
  773. PaintIndex = PaintIndex + 1;
  774. }
  775. //每次到了页数开始分页
  776. if (LastBoxCode == "" || (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["盒号"].ToString()) || i == rowNum - 1)
  777. {
  778. LastBoxCode = DataTable.Rows[i]["盒号"].ToString();
  779. //第一行添加客户信息
  780. if (i != rowNum - 1)
  781. {
  782. for (int j = 0; j < columnNum - 3; j++)
  783. {
  784. if (j == 0)
  785. {
  786. row1.CreateCell(j);
  787. row1.Cells[j].SetCellValue(DataTable.Rows[i]["客户"].ToString());
  788. }
  789. else if (j == columnNum - 4)
  790. {
  791. row1.CreateCell(j);
  792. row1.Cells[j].SetCellValue(DataTable.Rows[i]["出货单号"].ToString());
  793. }
  794. else
  795. {
  796. row1.CreateCell(j);
  797. }
  798. row1.GetCell(j).CellStyle = style;
  799. }
  800. row1 = sheet.CreateRow(PaintIndex);
  801. PaintIndex = PaintIndex + 1;
  802. //第二行添加型号
  803. for (int j = 0; j < columnNum - 3; j++)
  804. {
  805. if (j == 0)
  806. {
  807. row1.CreateCell(j);
  808. row1.Cells[j].SetCellValue(DataTable.Rows[i]["型号"].ToString());
  809. }
  810. else if (j == columnNum - 4)
  811. {
  812. row1.CreateCell(j);
  813. row1.Cells[j].SetCellValue(DataTable.Rows[i]["销售日期"].ToString());
  814. }
  815. else
  816. {
  817. row1.CreateCell(j);
  818. }
  819. row1.GetCell(j).CellStyle = style;
  820. }
  821. //添加列名
  822. row1 = sheet.CreateRow(PaintIndex);
  823. PaintIndex = PaintIndex + 1;
  824. for (int j = 4; j < columnNum; j++)
  825. {
  826. row1.CreateCell(j - 4);
  827. row1.Cells[j - 4].CellStyle = styleborder;
  828. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  829. {
  830. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  831. }
  832. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  833. {
  834. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  835. }
  836. else
  837. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].ColumnName);
  838. 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"))
  839. {
  840. sheet.SetColumnHidden(j - 4, true);
  841. }
  842. }
  843. row1 = sheet.CreateRow(PaintIndex);
  844. PaintIndex = PaintIndex + 1;
  845. }
  846. }
  847. //添加数据内容
  848. for (int j = 4; j < columnNum; j++)
  849. {
  850. string Data = DataTable.Rows[i][j].ToString();
  851. row1.CreateCell(j - 4);
  852. row1.Cells[j - 4].SetCellValue(Data);
  853. row1.GetCell(j - 4).CellStyle = styleborder;
  854. if (DataTable.Columns[j].ColumnName == "数量")
  855. {
  856. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  857. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  858. }
  859. if (DataTable.Columns[j].ColumnName == "序号")
  860. {
  861. row1.Cells[j - 4].SetCellValue(i + 1);
  862. }
  863. }
  864. if (i == rowNum - 1)
  865. {
  866. row1 = sheet.CreateRow(PaintIndex);
  867. PaintIndex = PaintIndex + 1;
  868. for (int j = 0; j < columnNum - 4; j++)
  869. {
  870. if (j == 0)
  871. {
  872. row1.CreateCell(j);
  873. row1.Cells[j].SetCellValue("小计");
  874. }
  875. else if (j == 2)
  876. {
  877. row1.CreateCell(j);
  878. row1.Cells[j].SetCellValue(sumCount);
  879. }
  880. else
  881. {
  882. row1.CreateCell(j);
  883. }
  884. row1.Cells[j].CellStyle = styleborder;
  885. }
  886. row1 = sheet.CreateRow(PaintIndex);
  887. PaintIndex = PaintIndex + 1;
  888. for (int j = 0; j < columnNum - 3; j++)
  889. {
  890. if (j == 0)
  891. {
  892. row1.CreateCell(j);
  893. row1.Cells[j].SetCellValue("备注");
  894. }
  895. else if (j == 2)
  896. {
  897. row1.CreateCell(j);
  898. row1.Cells[j].SetCellValue(totalCount);
  899. }
  900. else if (j == columnNum - 7)
  901. {
  902. row1.CreateCell(j);
  903. row1.Cells[j].SetCellValue(rowNum);
  904. }
  905. else if (j == columnNum - 4)
  906. {
  907. row1.CreateCell(j);
  908. row1.Cells[j].SetCellValue("片");
  909. }
  910. else
  911. {
  912. row1.CreateCell(j);
  913. }
  914. row1.Cells[j].CellStyle = style;
  915. }
  916. }
  917. }
  918. for (int i = 0; i < sheet.LastRowNum; i++)
  919. {
  920. if (i != 0)
  921. {
  922. sheet.AutoSizeColumn(i);
  923. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  924. }
  925. }
  926. break;
  927. default:
  928. break;
  929. }
  930. //填充首页
  931. sumCount = 0;
  932. totalCount = 0;
  933. PaintIndex = 1;
  934. ISheet sheet2 = book.CreateSheet("首页");
  935. row = sheet2.CreateRow(0);
  936. row.CreateCell(0);
  937. row.Cells[0].SetCellValue(" 深爱半导体有限公司芯片出货清单");
  938. row.GetCell(0).CellStyle = style;
  939. rowNum = FirstDT.Rows.Count;
  940. columnNum = FirstDT.Columns.Count;
  941. for (int i = 0; i < columnNum; i++)
  942. {
  943. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("pd_ordercode"))
  944. {
  945. FirstDT.Columns[i].ColumnName = "订单号";
  946. }
  947. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("pr_spec"))
  948. {
  949. FirstDT.Columns[i].ColumnName = "产品型号";
  950. }
  951. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_splitbatch"))
  952. {
  953. FirstDT.Columns[i].ColumnName = "批号";
  954. }
  955. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  956. {
  957. FirstDT.Columns[i].ColumnName = "片号";
  958. }
  959. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("num"))
  960. {
  961. FirstDT.Columns[i].ColumnName = "片数";
  962. }
  963. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("io_qty"))
  964. {
  965. FirstDT.Columns[i].ColumnName = "管芯数";
  966. }
  967. }
  968. for (int i = 0; i < rowNum; i++)
  969. {
  970. IRow row1 = sheet2.CreateRow(PaintIndex);
  971. PaintIndex = PaintIndex + 1;
  972. row1.HeightInPoints = 20;
  973. //只需要绘制一行
  974. if (i == 0)
  975. {
  976. for (int j = 0; j < columnNum - 3; j++)
  977. {
  978. if (j == 0)
  979. {
  980. row1.CreateCell(j);
  981. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["客户"].ToString());
  982. }
  983. else if (j == columnNum - 4)
  984. {
  985. row1.CreateCell(j);
  986. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["出货单号"].ToString());
  987. }
  988. else
  989. {
  990. row1.CreateCell(j);
  991. }
  992. row1.GetCell(j).CellStyle = style;
  993. }
  994. row1 = sheet2.CreateRow(PaintIndex);
  995. PaintIndex = PaintIndex + 1;
  996. //第二行添加型号
  997. for (int j = 0; j < columnNum - 3; j++)
  998. {
  999. if (j == 0)
  1000. {
  1001. row1.CreateCell(j);
  1002. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["型号"].ToString());
  1003. }
  1004. else if (j == columnNum - 4)
  1005. {
  1006. row1.CreateCell(j);
  1007. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["销售日期"].ToString());
  1008. }
  1009. else
  1010. {
  1011. row1.CreateCell(j);
  1012. }
  1013. row1.GetCell(j).CellStyle = style;
  1014. }
  1015. row1 = sheet2.CreateRow(PaintIndex);
  1016. PaintIndex = PaintIndex + 1;
  1017. //添加列名
  1018. for (int j = 4; j < columnNum; j++)
  1019. {
  1020. row1.CreateCell(j - 4);
  1021. row1.Cells[j - 4].CellStyle = styleborder;
  1022. row1.Cells[j - 4].SetCellValue(FirstDT.Columns[j].ColumnName);
  1023. }
  1024. row1 = sheet2.CreateRow(PaintIndex);
  1025. PaintIndex = PaintIndex + 1;
  1026. }
  1027. //添加数据内容
  1028. for (int j = 4; j < columnNum; j++)
  1029. {
  1030. string Data = FirstDT.Rows[i][j].ToString();
  1031. row1.CreateCell(j - 4);
  1032. row1.Cells[j - 4].SetCellValue(Data);
  1033. row1.GetCell(j - 4).CellStyle = styleborder;
  1034. if (FirstDT.Columns[j].ColumnName == "片数")
  1035. {
  1036. sumCount += int.Parse(Data);
  1037. }
  1038. if (FirstDT.Columns[j].ColumnName == "管芯数")
  1039. {
  1040. totalCount += int.Parse(Data);
  1041. }
  1042. }
  1043. //添加总计行
  1044. if (i == rowNum - 1)
  1045. {
  1046. row1 = sheet2.CreateRow(PaintIndex);
  1047. PaintIndex = PaintIndex + 1;
  1048. for (int j = 0; j < columnNum - 4; j++)
  1049. {
  1050. if (j == 0)
  1051. {
  1052. row1.CreateCell(j);
  1053. row1.Cells[j].CellStyle = styleborder;
  1054. row1.Cells[j].SetCellValue("总计");
  1055. }
  1056. else if (j == columnNum - 6)
  1057. {
  1058. row1.CreateCell(j);
  1059. row1.Cells[j].CellStyle = styleborder;
  1060. row1.Cells[j].SetCellValue(sumCount);
  1061. }
  1062. else if (j == columnNum - 5)
  1063. {
  1064. row1.CreateCell(j);
  1065. row1.Cells[j].CellStyle = styleborder;
  1066. row1.Cells[j].SetCellValue(totalCount);
  1067. }
  1068. else
  1069. {
  1070. row1.CreateCell(j);
  1071. row1.Cells[j].CellStyle = styleborder;
  1072. }
  1073. }
  1074. }
  1075. }
  1076. for (int i = 0; i < sheet2.LastRowNum; i++)
  1077. {
  1078. if (i != 0)
  1079. {
  1080. sheet2.AutoSizeColumn(i);
  1081. sheet2.SetColumnWidth(i, sheet2.GetColumnWidth(i) + 1000);
  1082. }
  1083. }
  1084. //将book的内容写入内存流中返回
  1085. book.Write(ms);
  1086. return ms;
  1087. }
  1088. }
  1089. }