ExcelHandler.cs 61 KB

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