ExcelHandler.cs 64 KB

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