ExcelHandler.cs 54 KB

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