ExcelHandler.cs 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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 = 11;
  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. //更新箱号
  274. List<string> ch_code = new List<string>();
  275. //系统打印箱号
  276. List<string> pib_outboxcode1 = new List<string>();
  277. int BoxCode = 1;
  278. ICellStyle style = book.CreateCellStyle();
  279. style.VerticalAlignment = VerticalAlignment.Center;
  280. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  281. ICellStyle styleborder = book.CreateCellStyle();
  282. styleborder.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  283. styleborder.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  284. styleborder.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  285. styleborder.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  286. styleborder.VerticalAlignment = VerticalAlignment.Center;
  287. styleborder.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  288. string pi_inoutno = "";
  289. //设置列的宽度,根据首行的列的内容的长度来设置
  290. for (int i = DataTable.Columns.Count - 1; i > 0; i--)
  291. {
  292. for (int j = 0; j < box.Length; j++)
  293. {
  294. if (box[j].Name.ToLower() == "ch_bluefilm" && !box[j].Checked)
  295. {
  296. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_bluefilm"))
  297. {
  298. DataTable.Columns.RemoveAt(i);
  299. break;
  300. }
  301. }
  302. if (box[j].Name.ToLower() == "ch_code" && !box[j].Checked)
  303. {
  304. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_code"))
  305. {
  306. DataTable.Columns.RemoveAt(i);
  307. break;
  308. }
  309. }
  310. if (box[j].Name.ToLower() == "ch_splitbatch" && !box[j].Checked)
  311. {
  312. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_splitbatch"))
  313. {
  314. DataTable.Columns.RemoveAt(i);
  315. break;
  316. }
  317. }
  318. if (box[j].Name.ToLower() == "ch_waterid" && !box[j].Checked)
  319. {
  320. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  321. {
  322. DataTable.Columns.RemoveAt(i);
  323. break;
  324. }
  325. }
  326. if (box[j].Name.ToLower() == "ch_pbcode" && !box[j].Checked)
  327. {
  328. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_pbcode"))
  329. {
  330. DataTable.Columns.RemoveAt(i);
  331. break;
  332. }
  333. }
  334. if (box[j].Name.ToLower() == "ch_remark" && !box[j].Checked)
  335. {
  336. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  337. {
  338. DataTable.Columns.RemoveAt(i);
  339. break;
  340. }
  341. }
  342. if (box[j].Name.ToLower() == "percent" && !box[j].Checked)
  343. {
  344. if (DataTable.Columns[i].ColumnName.ToLower().Contains("chw_percent1") || DataTable.Columns[i].ColumnName.ToLower().Contains("chw_percent2") || DataTable.Columns[i].ColumnName.ToLower().Contains("chw_itemname1") || DataTable.Columns[i].ColumnName.ToLower().Contains("chw_itemname2"))
  345. {
  346. DataTable.Columns.RemoveAt(i);
  347. break;
  348. }
  349. }
  350. if (box[j].Name.ToLower() == "ts" && !box[j].Checked)
  351. {
  352. if (DataTable.Columns[i].ColumnName.ToLower() == ("ts1") || DataTable.Columns[i].ColumnName.ToLower() == ("ts2") || DataTable.Columns[i].ColumnName.ToLower() == ("ts3"))
  353. {
  354. DataTable.Columns.RemoveAt(i);
  355. break;
  356. }
  357. }
  358. if (box[j].Name.ToLower() == "bvceo" && !box[j].Checked)
  359. {
  360. if (DataTable.Columns[i].ColumnName.ToLower() == "bvceo1" || DataTable.Columns[i].ColumnName.ToLower() == ("bvceo2") || DataTable.Columns[i].ColumnName.ToLower() == ("bvceo3"))
  361. {
  362. DataTable.Columns.RemoveAt(i);
  363. break;
  364. }
  365. }
  366. }
  367. }
  368. //获取行数量和列数量
  369. int rowNum = DataTable.Rows.Count;
  370. int columnNum = DataTable.Columns.Count;
  371. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  372. IRow row = sheet.CreateRow(0);
  373. //冻结第一行
  374. sheet.CreateFreezePane(0, 1, 0, 1);
  375. row.HeightInPoints = RowHeight;
  376. //固定第一行
  377. //row.RowStyle.IsLocked=true;
  378. //给第一行的标签赋值样式和值
  379. row.CreateCell(0);
  380. row.Cells[0].SetCellValue(" 深爱半导体有限公司芯片出货清单");
  381. row.GetCell(0).CellStyle = style;
  382. //开始绘制的Index
  383. int PaintIndex = 1;
  384. int sumCount = 0;
  385. int totalCount = 0;
  386. switch (Type)
  387. {
  388. case "FixRow":
  389. //清理系统取出来的数据
  390. BaseUtil.CleanDataTableData(FirstDT);
  391. //首页参数拼接
  392. string First_OrderCode = "";
  393. string First_Prspec = "";
  394. string First_Batch = "";
  395. ArrayList<string> First_WID = new ArrayList<string>();
  396. for (int i = 0; i < rowNum; i++)
  397. {
  398. IRow row1 = sheet.CreateRow(PaintIndex);
  399. PaintIndex = PaintIndex + 1;
  400. row1.HeightInPoints = RowHeight;
  401. //不包含的订单号
  402. if (DataTable.Columns.Contains("pd_ordercode") && !First_OrderCode.Contains(DataTable.Rows[i]["pd_ordercode"].ToString()))
  403. {
  404. First_OrderCode += DataTable.Rows[i]["pd_ordercode"].ToString() + " ";
  405. }
  406. //不包含的物料型号
  407. if (DataTable.Columns.Contains("pr_spec1") && !First_Prspec.Contains(DataTable.Rows[i]["pr_spec1"].ToString()))
  408. {
  409. First_Prspec += DataTable.Rows[i]["pr_spec1"].ToString() + " ";
  410. }
  411. //不包含扩撒批号
  412. if (DataTable.Columns.Contains("ch_splitbatch") && !First_Batch.Contains(DataTable.Rows[i]["ch_splitbatch"].ToString()))
  413. {
  414. First_Batch += DataTable.Rows[i]["ch_splitbatch"].ToString() + " ";
  415. }
  416. //不包含Wafer_id
  417. if (DataTable.Columns.Contains("Wafer_ID") && !First_WID.Contains(DataTable.Rows[i]["Wafer_ID"].ToString()))
  418. {
  419. First_WID.Add(DataTable.Rows[i]["Wafer_ID"].ToString());
  420. }
  421. if (i / PageSize >= 1 && i % PageSize == 0)
  422. {
  423. DataRow dr = FirstDT.NewRow();
  424. dr["pr_spec"] = DataTable.Rows[i]["pr_spec"].ToString();
  425. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  426. pi_inoutno = DataTable.Rows[i]["pi_inoutno"].ToString();
  427. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  428. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  429. dr["pd_ordercode"] = First_OrderCode;
  430. dr["pr_spec1"] = First_Prspec;
  431. dr["ch_splitbatch"] = First_Batch;
  432. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  433. dr["num"] = PageSize;
  434. dr["io_qty"] = sumCount;
  435. FirstDT.Rows.Add(dr);
  436. First_OrderCode = "";
  437. First_Prspec = "";
  438. First_Batch = "";
  439. First_WID.Clear();
  440. BoxCode = BoxCode + 1;
  441. for (int j = 0; j < columnNum - 4; j++)
  442. {
  443. if (j == 0)
  444. {
  445. row1.CreateCell(j);
  446. row1.Cells[j].SetCellValue("小计");
  447. }
  448. else if (j == 2)
  449. {
  450. row1.CreateCell(j);
  451. row1.Cells[j].SetCellValue(sumCount);
  452. }
  453. else
  454. {
  455. row1.CreateCell(j);
  456. }
  457. row1.Cells[j].CellStyle = styleborder;
  458. }
  459. sumCount = 0;
  460. row1 = sheet.CreateRow(PaintIndex);
  461. sheet.SetRowBreak(PaintIndex-1);
  462. PaintIndex = PaintIndex + 1;
  463. }
  464. //每次到了页数开始分页
  465. if (i % PageSize == 0 || i == rowNum - 1)
  466. {
  467. //第一行添加客户信息 rownum只有一行的情
  468. if (i != rowNum - 1 || rowNum == 1)
  469. {
  470. for (int j = 0; j < columnNum - 3; j++)
  471. {
  472. if (j == 0)
  473. {
  474. row1.CreateCell(j);
  475. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString());
  476. }
  477. else if (j > 5 && j == columnNum - 5)
  478. {
  479. row1.CreateCell(j);
  480. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  481. }
  482. else if (columnNum > 5 && j == columnNum - 5)
  483. {
  484. row1.CreateCell(j);
  485. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  486. }
  487. else
  488. {
  489. row1.CreateCell(j);
  490. }
  491. row1.GetCell(j).CellStyle = style;
  492. }
  493. row1 = sheet.CreateRow(PaintIndex);
  494. PaintIndex = PaintIndex + 1;
  495. //第二行添加型号
  496. for (int j = 0; j < columnNum - 3; j++)
  497. {
  498. if (j == 0)
  499. {
  500. row1.CreateCell(j);
  501. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_spec"].ToString());
  502. }
  503. else if (columnNum > 5 && j == columnNum - 5)
  504. {
  505. row1.CreateCell(j);
  506. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  507. }
  508. else if (columnNum <= 5 && j == columnNum - 4)
  509. {
  510. row1.CreateCell(j);
  511. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  512. }
  513. else
  514. {
  515. row1.CreateCell(j);
  516. }
  517. row1.GetCell(j).CellStyle = style;
  518. }
  519. //添加列名
  520. row1 = sheet.CreateRow(PaintIndex);
  521. PaintIndex = PaintIndex + 1;
  522. for (int j = 4; j < columnNum; j++)
  523. {
  524. row1.CreateCell(j - 4);
  525. row1.Cells[j - 4].CellStyle = styleborder;
  526. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  527. {
  528. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  529. }
  530. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  531. {
  532. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  533. }
  534. else
  535. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  536. //如果chw_itemname1的值为空,则值为100和0,其中一列不显示,不显示
  537. 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"))
  538. {
  539. sheet.SetColumnHidden(j - 4, true);
  540. }
  541. }
  542. row1 = sheet.CreateRow(PaintIndex);
  543. PaintIndex = PaintIndex + 1;
  544. }
  545. }
  546. //添加数据内容
  547. for (int j = 4; j < columnNum; j++)
  548. {
  549. string Data = DataTable.Rows[i][j].ToString();
  550. row1.CreateCell(j - 4);
  551. row1.Cells[j - 4].SetCellValue(Data);
  552. row1.GetCell(j - 4).CellStyle = styleborder;
  553. if (DataTable.Columns[j].ColumnName == "io_qty")
  554. {
  555. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  556. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  557. }
  558. if (DataTable.Columns[j].ColumnName == "rownum")
  559. {
  560. row1.Cells[j - 4].SetCellValue(i + 1);
  561. }
  562. }
  563. //固定行号分组的时候自动拼接新的DataTable
  564. if (i == rowNum - 1)
  565. {
  566. DataRow dr = FirstDT.NewRow();
  567. dr["pr_spec"] = DataTable.Rows[i]["pr_spec"].ToString();
  568. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  569. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  570. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  571. dr["pd_ordercode"] = First_OrderCode;
  572. dr["pr_spec1"] = First_Prspec;
  573. dr["ch_splitbatch"] = First_Batch;
  574. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  575. dr["num"] = (i % PageSize) + 1;
  576. dr["io_qty"] = sumCount;
  577. FirstDT.Rows.Add(dr);
  578. row1 = sheet.CreateRow(PaintIndex);
  579. PaintIndex = PaintIndex + 1;
  580. for (int j = 0; j < columnNum - 4; j++)
  581. {
  582. if (j == 0)
  583. {
  584. row1.CreateCell(j);
  585. row1.Cells[j].SetCellValue("小计");
  586. }
  587. else if (j == 2)
  588. {
  589. row1.CreateCell(j);
  590. row1.Cells[j].SetCellValue(sumCount);
  591. }
  592. else
  593. {
  594. row1.CreateCell(j);
  595. }
  596. row1.Cells[j].CellStyle = styleborder;
  597. }
  598. row1 = sheet.CreateRow(PaintIndex);
  599. for (int j = 0; j < columnNum - 3; j++)
  600. {
  601. if (j == 0)
  602. {
  603. row1.CreateCell(j);
  604. row1.Cells[j].SetCellValue("备注");
  605. }
  606. else if (j == 2)
  607. {
  608. row1.CreateCell(j);
  609. row1.Cells[j].SetCellValue(totalCount);
  610. }
  611. //原本是j == columnNum - 5因为还有spec和order两列隐藏列,所以需要在往后移动
  612. else if (j == columnNum - 6)
  613. {
  614. row1.CreateCell(j);
  615. row1.Cells[j].SetCellValue(rowNum);
  616. }
  617. else if (j > 5 && j == columnNum - 5)
  618. {
  619. row1.CreateCell(j);
  620. row1.Cells[j].SetCellValue("片");
  621. }
  622. else if (columnNum > 5 && j == columnNum - 5)
  623. {
  624. row1.CreateCell(j);
  625. row1.Cells[j].SetCellValue("片");
  626. }
  627. else
  628. {
  629. row1.CreateCell(j);
  630. }
  631. row1.Cells[j].CellStyle = style;
  632. }
  633. sheet.SetRowBreak(PaintIndex);
  634. PaintIndex = PaintIndex + 1;
  635. }
  636. ch_code.Add(DataTable.Rows[i]["ch_code"].ToString());
  637. pib_outboxcode1.Add(BoxCode.ToString());
  638. }
  639. for (int i = 0; i < sheet.LastRowNum; i++)
  640. {
  641. if (i != 0)
  642. {
  643. sheet.AutoSizeColumn(i);
  644. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  645. }
  646. }
  647. break;
  648. case "BatchCode":
  649. string LastBatchCode = "";
  650. for (int i = 0; i < rowNum; i++)
  651. {
  652. IRow row1 = sheet.CreateRow(PaintIndex);
  653. PaintIndex = PaintIndex + 1;
  654. row1.HeightInPoints = RowHeight;
  655. //如果批号不相等的时候
  656. if (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString())
  657. {
  658. BoxCode = BoxCode + 1;
  659. for (int j = 0; j < columnNum - 4; j++)
  660. {
  661. if (j == 0)
  662. {
  663. row1.CreateCell(j);
  664. row1.Cells[j].SetCellValue("小计");
  665. }
  666. else if (j == 2)
  667. {
  668. row1.CreateCell(j);
  669. row1.Cells[j].SetCellValue(sumCount);
  670. }
  671. else
  672. {
  673. row1.CreateCell(j);
  674. }
  675. row1.Cells[j].CellStyle = styleborder;
  676. }
  677. sumCount = 0;
  678. row1 = sheet.CreateRow(PaintIndex);
  679. sheet.SetRowBreak(PaintIndex - 1);
  680. PaintIndex = PaintIndex + 1;
  681. }
  682. //每次到了页数开始分页
  683. if (LastBatchCode == "" || (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString()) || i == rowNum - 1)
  684. {
  685. LastBatchCode = DataTable.Rows[i]["ch_splitbatch"].ToString();
  686. //第一行添加客户信息
  687. if (i != rowNum - 1)
  688. {
  689. for (int j = 0; j < columnNum - 3; j++)
  690. {
  691. if (j == 0)
  692. {
  693. row1.CreateCell(j);
  694. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString());
  695. }
  696. else if (j == columnNum - 4)
  697. {
  698. row1.CreateCell(j);
  699. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  700. }
  701. else
  702. {
  703. row1.CreateCell(j);
  704. }
  705. row1.GetCell(j).CellStyle = style;
  706. }
  707. row1 = sheet.CreateRow(PaintIndex);
  708. PaintIndex = PaintIndex + 1;
  709. //第二行添加型号
  710. for (int j = 0; j < columnNum - 3; j++)
  711. {
  712. if (j == 0)
  713. {
  714. row1.CreateCell(j);
  715. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_spec"].ToString());
  716. }
  717. else if (j == columnNum - 4)
  718. {
  719. row1.CreateCell(j);
  720. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  721. }
  722. else
  723. {
  724. row1.CreateCell(j);
  725. }
  726. row1.GetCell(j).CellStyle = style;
  727. }
  728. //添加列名
  729. row1 = sheet.CreateRow(PaintIndex);
  730. PaintIndex = PaintIndex + 1;
  731. for (int j = 4; j < columnNum; j++)
  732. {
  733. row1.CreateCell(j - 4);
  734. row1.Cells[j - 4].CellStyle = styleborder;
  735. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  736. {
  737. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  738. }
  739. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  740. {
  741. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  742. }
  743. else
  744. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  745. 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"))
  746. {
  747. sheet.SetColumnHidden(j - 4, true);
  748. }
  749. }
  750. row1 = sheet.CreateRow(PaintIndex);
  751. PaintIndex = PaintIndex + 1;
  752. }
  753. }
  754. //添加数据内容
  755. for (int j = 4; j < columnNum; j++)
  756. {
  757. string Data = DataTable.Rows[i][j].ToString();
  758. row1.CreateCell(j - 4);
  759. row1.Cells[j - 4].SetCellValue(Data);
  760. row1.GetCell(j - 4).CellStyle = styleborder;
  761. if (DataTable.Columns[j].ColumnName == "io_qty")
  762. {
  763. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  764. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  765. }
  766. if (DataTable.Columns[j].ColumnName == "rownum")
  767. {
  768. row1.Cells[j - 4].SetCellValue(i + 1);
  769. }
  770. }
  771. if (i == rowNum - 1)
  772. {
  773. row1 = sheet.CreateRow(PaintIndex);
  774. PaintIndex = PaintIndex + 1;
  775. for (int j = 0; j < columnNum - 4; j++)
  776. {
  777. if (j == 0)
  778. {
  779. row1.CreateCell(j);
  780. row1.Cells[j].SetCellValue("小计");
  781. }
  782. else if (j == 2)
  783. {
  784. row1.CreateCell(j);
  785. row1.Cells[j].SetCellValue(sumCount);
  786. }
  787. else
  788. {
  789. row1.CreateCell(j);
  790. }
  791. row1.Cells[j].CellStyle = styleborder;
  792. }
  793. //创建备注内容
  794. row1 = sheet.CreateRow(PaintIndex);
  795. for (int j = 0; j < columnNum - 3; j++)
  796. {
  797. if (j == 0)
  798. {
  799. row1.CreateCell(j);
  800. row1.Cells[j].SetCellValue("备注");
  801. }
  802. else if (j == 2)
  803. {
  804. row1.CreateCell(j);
  805. row1.Cells[j].SetCellValue(totalCount);
  806. }
  807. else if (j == columnNum - 6)
  808. {
  809. row1.CreateCell(j);
  810. row1.Cells[j].SetCellValue(rowNum);
  811. }
  812. else if (j > 5 && j == columnNum - 5)
  813. {
  814. row1.CreateCell(j);
  815. row1.Cells[j].SetCellValue("片");
  816. }
  817. else if (columnNum > 5 && j == columnNum - 5)
  818. {
  819. row1.CreateCell(j);
  820. row1.Cells[j].SetCellValue("片");
  821. }
  822. else
  823. {
  824. row1.CreateCell(j);
  825. }
  826. row1.Cells[j].CellStyle = style;
  827. }
  828. sheet.SetRowBreak(PaintIndex);
  829. PaintIndex = PaintIndex + 1;
  830. }
  831. ch_code.Add(DataTable.Rows[i]["ch_code"].ToString());
  832. pib_outboxcode1.Add(BoxCode.ToString());
  833. }
  834. for (int i = 0; i < sheet.LastRowNum; i++)
  835. {
  836. if (i != 0)
  837. {
  838. sheet.AutoSizeColumn(i);
  839. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  840. }
  841. }
  842. break;
  843. case "BoxCode":
  844. string LastBoxCode = "";
  845. for (int i = 0; i < rowNum; i++)
  846. {
  847. IRow row1 = sheet.CreateRow(PaintIndex);
  848. PaintIndex = PaintIndex + 1;
  849. row1.HeightInPoints = RowHeight;
  850. //如果批号不相等的时候
  851. if (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["CH_PBCODE"].ToString())
  852. {
  853. BoxCode = BoxCode + 1;
  854. for (int j = 0; j < columnNum - 4; j++)
  855. {
  856. if (j == 0)
  857. {
  858. row1.CreateCell(j);
  859. row1.Cells[j].SetCellValue("小计");
  860. }
  861. else if (j == 2)
  862. {
  863. row1.CreateCell(j);
  864. row1.Cells[j].SetCellValue(sumCount);
  865. }
  866. else
  867. {
  868. row1.CreateCell(j);
  869. }
  870. row1.Cells[j].CellStyle = styleborder;
  871. }
  872. sumCount = 0;
  873. row1 = sheet.CreateRow(PaintIndex);
  874. sheet.SetRowBreak(PaintIndex - 1);
  875. PaintIndex = PaintIndex + 1;
  876. }
  877. //每次到了页数开始分页
  878. if (LastBoxCode == "" || (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["ch_pbcode"].ToString()) || i == rowNum - 1)
  879. {
  880. LastBoxCode = DataTable.Rows[i]["CH_PBCODE"].ToString();
  881. //第一行添加客户信息
  882. if (i != rowNum - 1)
  883. {
  884. for (int j = 0; j < columnNum - 3; j++)
  885. {
  886. if (j == 0)
  887. {
  888. row1.CreateCell(j);
  889. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString());
  890. }
  891. else if (j > 5 && j == columnNum - 5)
  892. {
  893. row1.CreateCell(j);
  894. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  895. }
  896. else if (columnNum > 5 && j == columnNum - 5)
  897. {
  898. row1.CreateCell(j);
  899. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  900. }
  901. else
  902. {
  903. row1.CreateCell(j);
  904. }
  905. row1.GetCell(j).CellStyle = style;
  906. }
  907. row1 = sheet.CreateRow(PaintIndex);
  908. PaintIndex = PaintIndex + 1;
  909. //第二行添加型号
  910. for (int j = 0; j < columnNum - 3; j++)
  911. {
  912. if (j == 0)
  913. {
  914. row1.CreateCell(j);
  915. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_spec"].ToString());
  916. }
  917. else if (j > 5 && j == columnNum - 5)
  918. {
  919. row1.CreateCell(j);
  920. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  921. }
  922. else if (columnNum > 5 && j == columnNum - 5)
  923. {
  924. row1.CreateCell(j);
  925. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  926. }
  927. else
  928. {
  929. row1.CreateCell(j);
  930. }
  931. row1.GetCell(j).CellStyle = style;
  932. }
  933. //添加列名
  934. row1 = sheet.CreateRow(PaintIndex);
  935. PaintIndex = PaintIndex + 1;
  936. for (int j = 4; j < columnNum; j++)
  937. {
  938. //设定固定的列名
  939. row1.CreateCell(j - 4);
  940. row1.Cells[j - 4].CellStyle = styleborder;
  941. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  942. {
  943. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  944. }
  945. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  946. {
  947. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  948. }
  949. else
  950. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  951. 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"))
  952. {
  953. sheet.SetColumnHidden(j - 4, true);
  954. }
  955. }
  956. row1 = sheet.CreateRow(PaintIndex);
  957. PaintIndex = PaintIndex + 1;
  958. }
  959. }
  960. //添加数据内容
  961. for (int j = 4; j < columnNum; j++)
  962. {
  963. string Data = DataTable.Rows[i][j].ToString();
  964. row1.CreateCell(j - 4);
  965. row1.Cells[j - 4].SetCellValue(Data);
  966. row1.GetCell(j - 4).CellStyle = styleborder;
  967. if (DataTable.Columns[j].ColumnName == "io_qty")
  968. {
  969. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  970. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  971. }
  972. if (DataTable.Columns[j].ColumnName == "rownum")
  973. {
  974. row1.Cells[j - 4].SetCellValue(i + 1);
  975. }
  976. }
  977. if (i == rowNum - 1)
  978. {
  979. row1 = sheet.CreateRow(PaintIndex);
  980. PaintIndex = PaintIndex + 1;
  981. for (int j = 0; j < columnNum - 4; j++)
  982. {
  983. if (j == 0)
  984. {
  985. row1.CreateCell(j);
  986. row1.Cells[j].SetCellValue("小计");
  987. }
  988. else if (j == 2)
  989. {
  990. row1.CreateCell(j);
  991. row1.Cells[j].SetCellValue(sumCount);
  992. }
  993. else
  994. {
  995. row1.CreateCell(j);
  996. }
  997. row1.Cells[j].CellStyle = styleborder;
  998. }
  999. row1 = sheet.CreateRow(PaintIndex);
  1000. for (int j = 0; j < columnNum - 3; j++)
  1001. {
  1002. if (j == 0)
  1003. {
  1004. row1.CreateCell(j);
  1005. row1.Cells[j].SetCellValue("备注");
  1006. }
  1007. else if (j == 2)
  1008. {
  1009. row1.CreateCell(j);
  1010. row1.Cells[j].SetCellValue(totalCount);
  1011. }
  1012. else if (j == columnNum - 6)
  1013. {
  1014. row1.CreateCell(j);
  1015. row1.Cells[j].SetCellValue(rowNum);
  1016. }
  1017. else if (j > 5 && j == columnNum - 5)
  1018. {
  1019. row1.CreateCell(j);
  1020. row1.Cells[j].SetCellValue("片");
  1021. }
  1022. else if (columnNum > 5 && j == columnNum - 5)
  1023. {
  1024. row1.CreateCell(j);
  1025. row1.Cells[j].SetCellValue("片");
  1026. }
  1027. else
  1028. {
  1029. row1.CreateCell(j);
  1030. }
  1031. row1.Cells[j].CellStyle = style;
  1032. }
  1033. sheet.SetRowBreak(PaintIndex);
  1034. PaintIndex = PaintIndex + 1;
  1035. }
  1036. ch_code.Add(DataTable.Rows[i]["ch_code"].ToString());
  1037. pib_outboxcode1.Add(BoxCode.ToString());
  1038. }
  1039. for (int i = 0; i < sheet.LastRowNum; i++)
  1040. {
  1041. if (i != 0)
  1042. {
  1043. sheet.AutoSizeColumn(i);
  1044. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  1045. }
  1046. }
  1047. break;
  1048. default:
  1049. break;
  1050. }
  1051. dh.BatchInsert("update prodiobarcode set pib_outboxcode1=:pib_outboxcode1 where pib_inoutno='"+ Inoutno + "' and pib_custbarcode=:pib_custbarcode",new string[] { "pib_outboxcode1", "pib_custbarcode" },pib_outboxcode1.ToArray(), ch_code.ToArray());
  1052. //填充首页
  1053. sumCount = 0;
  1054. totalCount = 0;
  1055. PaintIndex = 1;
  1056. ISheet sheet2 = book.CreateSheet("首页");
  1057. row = sheet2.CreateRow(0);
  1058. row.CreateCell(0);
  1059. row.Cells[0].SetCellValue(" 深爱半导体有限公司芯片出货清单");
  1060. row.GetCell(0).CellStyle = style;
  1061. rowNum = FirstDT.Rows.Count;
  1062. //不需要显示的列移除
  1063. for (int i = FirstDT.Columns.Count - 1; i > 0; i--)
  1064. {
  1065. for (int j = 0; j < box.Length; j++)
  1066. {
  1067. if (box[j].Name == "FirstPage_WID" && !box[j].Checked)
  1068. {
  1069. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  1070. {
  1071. FirstDT.Columns.RemoveAt(i);
  1072. }
  1073. }
  1074. if (box[j].Name == "FirstPage_YIELD" && !box[j].Checked)
  1075. {
  1076. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_yeild"))
  1077. {
  1078. FirstDT.Columns.RemoveAt(i);
  1079. }
  1080. }
  1081. if (box[j].Name == "FirstPage_REMARK" && !box[j].Checked)
  1082. {
  1083. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  1084. {
  1085. FirstDT.Columns.RemoveAt(i);
  1086. }
  1087. }
  1088. }
  1089. }
  1090. columnNum = FirstDT.Columns.Count;
  1091. for (int i = 0; i < rowNum; i++)
  1092. {
  1093. IRow row1 = sheet2.CreateRow(PaintIndex);
  1094. PaintIndex = PaintIndex + 1;
  1095. row1.HeightInPoints = RowHeight;
  1096. //只需要绘制一行
  1097. if (i == 0)
  1098. {
  1099. for (int j = 0; j < columnNum - 3; j++)
  1100. {
  1101. if (j == 0)
  1102. {
  1103. row1.CreateCell(j);
  1104. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["pi_title"].ToString());
  1105. }
  1106. else if (j > 5 && j == columnNum - 5)
  1107. {
  1108. row1.CreateCell(j);
  1109. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  1110. }
  1111. else if (columnNum > 5 && j == columnNum - 5)
  1112. {
  1113. row1.CreateCell(j);
  1114. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  1115. }
  1116. else
  1117. {
  1118. row1.CreateCell(j);
  1119. }
  1120. row1.GetCell(j).CellStyle = style;
  1121. }
  1122. row1 = sheet2.CreateRow(PaintIndex);
  1123. PaintIndex = PaintIndex + 1;
  1124. //第二行添加型号
  1125. for (int j = 0; j < columnNum - 3; j++)
  1126. {
  1127. if (j == 0)
  1128. {
  1129. row1.CreateCell(j);
  1130. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["pr_spec"].ToString());
  1131. }
  1132. else if (j > 5 && j == columnNum - 5)
  1133. {
  1134. row1.CreateCell(j);
  1135. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  1136. }
  1137. else if (columnNum > 5 && j == columnNum - 5)
  1138. {
  1139. row1.CreateCell(j);
  1140. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  1141. }
  1142. else
  1143. {
  1144. row1.CreateCell(j);
  1145. }
  1146. row1.GetCell(j).CellStyle = style;
  1147. }
  1148. row1 = sheet2.CreateRow(PaintIndex);
  1149. PaintIndex = PaintIndex + 1;
  1150. //添加列名
  1151. for (int j = 4; j < columnNum; j++)
  1152. {
  1153. row1.CreateCell(j - 4);
  1154. row1.Cells[j - 4].CellStyle = styleborder;
  1155. row1.Cells[j - 4].SetCellValue(FirstDT.Columns[j].Caption);
  1156. }
  1157. row1 = sheet2.CreateRow(PaintIndex);
  1158. PaintIndex = PaintIndex + 1;
  1159. }
  1160. //添加数据内容
  1161. for (int j = 4; j < columnNum; j++)
  1162. {
  1163. string Data = FirstDT.Rows[i][j].ToString();
  1164. row1.CreateCell(j - 4);
  1165. row1.Cells[j - 4].SetCellValue(Data);
  1166. row1.GetCell(j - 4).CellStyle = styleborder;
  1167. if (FirstDT.Columns[j].ColumnName == "num")
  1168. {
  1169. sumCount += int.Parse(Data);
  1170. }
  1171. if (FirstDT.Columns[j].ColumnName == "io_qty")
  1172. {
  1173. totalCount += int.Parse(Data);
  1174. }
  1175. }
  1176. //添加总计行
  1177. if (i == rowNum - 1)
  1178. {
  1179. row1 = sheet2.CreateRow(PaintIndex);
  1180. PaintIndex = PaintIndex + 1;
  1181. for (int j = 0; j < columnNum - 4; j++)
  1182. {
  1183. if (j == 0)
  1184. {
  1185. row1.CreateCell(j);
  1186. row1.Cells[j].CellStyle = styleborder;
  1187. row1.Cells[j].SetCellValue("总计");
  1188. }
  1189. else if (j == columnNum - 6)
  1190. {
  1191. row1.CreateCell(j);
  1192. row1.Cells[j].CellStyle = styleborder;
  1193. row1.Cells[j].SetCellValue(sumCount);
  1194. }
  1195. else if (j == columnNum - 5)
  1196. {
  1197. row1.CreateCell(j);
  1198. row1.Cells[j].CellStyle = styleborder;
  1199. row1.Cells[j].SetCellValue(totalCount);
  1200. }
  1201. else
  1202. {
  1203. row1.CreateCell(j);
  1204. row1.Cells[j].CellStyle = styleborder;
  1205. }
  1206. }
  1207. }
  1208. }
  1209. for (int i = 0; i < sheet2.LastRowNum; i++)
  1210. {
  1211. if (i != 0)
  1212. {
  1213. sheet2.AutoSizeColumn(i);
  1214. sheet2.SetColumnWidth(i, sheet2.GetColumnWidth(i) + 1000);
  1215. }
  1216. }
  1217. //将book的内容写入内存流中返回
  1218. book.Write(ms);
  1219. return ms;
  1220. }
  1221. }
  1222. }