ExcelHandler.cs 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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());
  461. }
  462. else if (j == 4)
  463. {
  464. row1.CreateCell(j);
  465. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  466. }
  467. else if (columnNum > 5 && j == columnNum - 5)
  468. {
  469. row1.CreateCell(j);
  470. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  471. }
  472. else
  473. {
  474. row1.CreateCell(j);
  475. }
  476. row1.GetCell(j).CellStyle = style;
  477. }
  478. row1 = sheet.CreateRow(PaintIndex);
  479. PaintIndex = PaintIndex + 1;
  480. //第二行添加型号
  481. for (int j = 0; j < columnNum - 3; j++)
  482. {
  483. if (j == 0)
  484. {
  485. row1.CreateCell(j);
  486. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_orispeccode"].ToString() + DataTable.Rows[i]["me_desc"].ToString() + DataTable.Rows[i]["pr_size"].ToString());
  487. }
  488. else if (j == 4)
  489. {
  490. row1.CreateCell(j);
  491. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  492. }
  493. else if (columnNum <= 5 && j == columnNum - 4)
  494. {
  495. row1.CreateCell(j);
  496. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  497. }
  498. else
  499. {
  500. row1.CreateCell(j);
  501. }
  502. row1.GetCell(j).CellStyle = style;
  503. }
  504. //添加列名
  505. row1 = sheet.CreateRow(PaintIndex);
  506. PaintIndex = PaintIndex + 1;
  507. //计数列所在的索引
  508. for (int j = 4; j < columnNum; j++)
  509. {
  510. row1.CreateCell(j - 4);
  511. row1.Cells[j - 4].CellStyle = styleborder;
  512. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  513. {
  514. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  515. }
  516. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  517. {
  518. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  519. }
  520. else
  521. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  522. if (DataTable.Columns[j].ColumnName.ToString() == "io_qty")
  523. {
  524. NumIndex = j;
  525. }
  526. //如果chw_itemname1的值为空,则值为100和0,其中一列不显示,不显示
  527. if (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")))
  528. {
  529. sheet.SetColumnHidden(j - 4, true);
  530. }
  531. }
  532. row1 = sheet.CreateRow(PaintIndex);
  533. PaintIndex = PaintIndex + 1;
  534. }
  535. }
  536. //添加数据内容
  537. for (int j = 4; j < columnNum; j++)
  538. {
  539. string Data = DataTable.Rows[i][j].ToString();
  540. row1.CreateCell(j - 4);
  541. row1.Cells[j - 4].SetCellValue(Data);
  542. row1.GetCell(j - 4).CellStyle = styleborder;
  543. if (DataTable.Columns[j].ColumnName == "io_qty")
  544. {
  545. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  546. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  547. }
  548. if (DataTable.Columns[j].ColumnName == "rownum")
  549. {
  550. row1.Cells[j - 4].SetCellValue(i + 1);
  551. }
  552. }
  553. //固定行号分组的时候自动拼接新的DataTable
  554. if (i == rowNum - 1)
  555. {
  556. DataRow dr = FirstDT.NewRow();
  557. dr["pr_orispeccode"] = DataTable.Rows[i]["pr_orispeccode"].ToString();
  558. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  559. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  560. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  561. dr["pd_ordercode"] = First_OrderCode;
  562. dr["pr_orispeccode1"] = First_Prspec;
  563. dr["ch_splitbatch"] = First_Batch;
  564. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  565. dr["num"] = (i % PageSize) + 1;
  566. dr["io_qty"] = sumCount;
  567. FirstDT.Rows.Add(dr);
  568. row1 = sheet.CreateRow(PaintIndex);
  569. PaintIndex = PaintIndex + 1;
  570. for (int j = 0; j < columnNum - 4; j++)
  571. {
  572. row1.CreateCell(j);
  573. if (j == 0)
  574. {
  575. row1.Cells[j].SetCellValue("小计");
  576. }
  577. else if (DataTable.Columns[j].ColumnName == "io_qty")
  578. {
  579. row1.Cells[j - 4].SetCellValue(sumCount);
  580. }
  581. row1.Cells[j].CellStyle = styleborder;
  582. }
  583. row1 = sheet.CreateRow(PaintIndex);
  584. for (int j = 0; j < columnNum - 3; j++)
  585. {
  586. if (j == 0)
  587. {
  588. row1.CreateCell(j);
  589. row1.Cells[j].SetCellValue("备注");
  590. }
  591. else if (j == 2)
  592. {
  593. row1.CreateCell(j);
  594. row1.Cells[j].SetCellValue(totalCount);
  595. }
  596. //原本是j == columnNum - 5因为还有spec和order两列隐藏列,所以需要在往后移动
  597. else if (j == columnNum - 6)
  598. {
  599. row1.CreateCell(j);
  600. row1.Cells[j].SetCellValue(rowNum);
  601. }
  602. else if (j > 5 && j == columnNum - 5)
  603. {
  604. row1.CreateCell(j);
  605. row1.Cells[j].SetCellValue("片");
  606. }
  607. else if (columnNum > 5 && j == columnNum - 5)
  608. {
  609. row1.CreateCell(j);
  610. row1.Cells[j].SetCellValue("片");
  611. }
  612. else
  613. {
  614. row1.CreateCell(j);
  615. }
  616. row1.Cells[j].CellStyle = style;
  617. }
  618. sheet.SetRowBreak(PaintIndex);
  619. sheet.Footer.Center = "第&P页,共&N页";
  620. PaintIndex = PaintIndex + 1;
  621. }
  622. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  623. pib_outboxcode1.Add(BoxCode.ToString());
  624. }
  625. for (int i = 0; i < sheet.LastRowNum; i++)
  626. {
  627. if (i != 0)
  628. {
  629. sheet.AutoSizeColumn(i);
  630. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  631. }
  632. }
  633. break;
  634. case "BatchCode":
  635. string LastBatchCode = "";
  636. for (int i = 0; i < rowNum; i++)
  637. {
  638. IRow row1 = sheet.CreateRow(PaintIndex);
  639. PaintIndex = PaintIndex + 1;
  640. row1.HeightInPoints = RowHeight;
  641. //如果批号不相等的时候
  642. if (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString())
  643. {
  644. BoxCode = BoxCode + 1;
  645. for (int j = 0; j < columnNum - 4; j++)
  646. {
  647. row1.CreateCell(j);
  648. if (j == 0)
  649. {
  650. row1.Cells[j].SetCellValue("小计");
  651. }
  652. else if (DataTable.Columns[j].ColumnName == "io_qty")
  653. {
  654. row1.Cells[j - 4].SetCellValue(sumCount);
  655. }
  656. row1.Cells[j].CellStyle = styleborder;
  657. }
  658. sumCount = 0;
  659. row1 = sheet.CreateRow(PaintIndex);
  660. sheet.SetRowBreak(PaintIndex - 1);
  661. sheet.Footer.Center = "第&P页,共&N页";
  662. PaintIndex = PaintIndex + 1;
  663. }
  664. //每次到了页数开始分页
  665. if (LastBatchCode == "" || (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString()) || i == rowNum - 1)
  666. {
  667. LastBatchCode = DataTable.Rows[i]["ch_splitbatch"].ToString();
  668. //第一行添加客户信息
  669. if (i != rowNum - 1)
  670. {
  671. for (int j = 0; j < columnNum - 3; j++)
  672. {
  673. if (j == 0)
  674. {
  675. row1.CreateCell(j);
  676. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString());
  677. }
  678. else if (j == columnNum - 4)
  679. {
  680. row1.CreateCell(j);
  681. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  682. }
  683. else
  684. {
  685. row1.CreateCell(j);
  686. }
  687. row1.GetCell(j).CellStyle = style;
  688. }
  689. row1 = sheet.CreateRow(PaintIndex);
  690. PaintIndex = PaintIndex + 1;
  691. //第二行添加型号
  692. for (int j = 0; j < columnNum - 3; j++)
  693. {
  694. if (j == 0)
  695. {
  696. row1.CreateCell(j);
  697. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_orispeccode"].ToString() + DataTable.Rows[i]["me_desc"].ToString() + DataTable.Rows[i]["pr_size"].ToString());
  698. }
  699. else if (j == columnNum - 4)
  700. {
  701. row1.CreateCell(j);
  702. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  703. }
  704. else
  705. {
  706. row1.CreateCell(j);
  707. }
  708. row1.GetCell(j).CellStyle = style;
  709. }
  710. //添加列名
  711. row1 = sheet.CreateRow(PaintIndex);
  712. PaintIndex = PaintIndex + 1;
  713. for (int j = 4; j < columnNum; j++)
  714. {
  715. row1.CreateCell(j - 4);
  716. row1.Cells[j - 4].CellStyle = styleborder;
  717. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  718. {
  719. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  720. }
  721. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  722. {
  723. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  724. }
  725. else
  726. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  727. if (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")))
  728. {
  729. sheet.SetColumnHidden(j - 4, true);
  730. }
  731. }
  732. row1 = sheet.CreateRow(PaintIndex);
  733. PaintIndex = PaintIndex + 1;
  734. }
  735. }
  736. //添加数据内容
  737. for (int j = 4; j < columnNum; j++)
  738. {
  739. string Data = DataTable.Rows[i][j].ToString();
  740. row1.CreateCell(j - 4);
  741. row1.Cells[j - 4].SetCellValue(Data);
  742. row1.GetCell(j - 4).CellStyle = styleborder;
  743. if (DataTable.Columns[j].ColumnName == "io_qty")
  744. {
  745. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  746. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  747. }
  748. if (DataTable.Columns[j].ColumnName == "rownum")
  749. {
  750. row1.Cells[j - 4].SetCellValue(i + 1);
  751. }
  752. }
  753. if (i == rowNum - 1)
  754. {
  755. row1 = sheet.CreateRow(PaintIndex);
  756. PaintIndex = PaintIndex + 1;
  757. for (int j = 0; j < columnNum - 4; j++)
  758. {
  759. row1.CreateCell(j);
  760. if (j == 0)
  761. {
  762. row1.Cells[j].SetCellValue("小计");
  763. }
  764. else if (DataTable.Columns[j].ColumnName == "io_qty")
  765. {
  766. row1.Cells[j - 4].SetCellValue(sumCount);
  767. }
  768. row1.Cells[j].CellStyle = styleborder;
  769. }
  770. //创建备注内容
  771. row1 = sheet.CreateRow(PaintIndex);
  772. for (int j = 0; j < columnNum - 3; j++)
  773. {
  774. if (j == 0)
  775. {
  776. row1.CreateCell(j);
  777. row1.Cells[j].SetCellValue("备注");
  778. }
  779. else if (j == 2)
  780. {
  781. row1.CreateCell(j);
  782. row1.Cells[j].SetCellValue(totalCount);
  783. }
  784. else if (j == columnNum - 6)
  785. {
  786. row1.CreateCell(j);
  787. row1.Cells[j].SetCellValue(rowNum);
  788. }
  789. else if (j > 5 && j == columnNum - 5)
  790. {
  791. row1.CreateCell(j);
  792. row1.Cells[j].SetCellValue("片");
  793. }
  794. else if (columnNum > 5 && j == columnNum - 5)
  795. {
  796. row1.CreateCell(j);
  797. row1.Cells[j].SetCellValue("片");
  798. }
  799. else
  800. {
  801. row1.CreateCell(j);
  802. }
  803. row1.Cells[j].CellStyle = style;
  804. }
  805. sheet.SetRowBreak(PaintIndex);
  806. sheet.Footer.Center = "第&P页,共&N页";
  807. PaintIndex = PaintIndex + 1;
  808. }
  809. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  810. pib_outboxcode1.Add(BoxCode.ToString());
  811. }
  812. for (int i = 0; i < sheet.LastRowNum; i++)
  813. {
  814. if (i != 0)
  815. {
  816. sheet.AutoSizeColumn(i);
  817. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  818. }
  819. }
  820. break;
  821. case "BoxCode":
  822. string LastBoxCode = "";
  823. for (int i = 0; i < rowNum; i++)
  824. {
  825. IRow row1 = sheet.CreateRow(PaintIndex);
  826. PaintIndex = PaintIndex + 1;
  827. row1.HeightInPoints = RowHeight;
  828. //如果批号不相等的时候
  829. if (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["CH_PBCODE"].ToString())
  830. {
  831. BoxCode = BoxCode + 1;
  832. for (int j = 0; j < columnNum - 4; j++)
  833. {
  834. row1.CreateCell(j);
  835. if (j == 0)
  836. {
  837. row1.Cells[j].SetCellValue("小计");
  838. }
  839. else if (DataTable.Columns[j].ColumnName == "io_qty")
  840. {
  841. row1.Cells[j - 4].SetCellValue(sumCount);
  842. }
  843. row1.Cells[j].CellStyle = styleborder;
  844. }
  845. sumCount = 0;
  846. row1 = sheet.CreateRow(PaintIndex);
  847. sheet.SetRowBreak(PaintIndex - 1);
  848. sheet.Footer.Center = "第&P页,共&N页";
  849. PaintIndex = PaintIndex + 1;
  850. }
  851. //每次到了页数开始分页
  852. if (LastBoxCode == "" || (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["ch_pbcode"].ToString()) || i == rowNum - 1)
  853. {
  854. LastBoxCode = DataTable.Rows[i]["CH_PBCODE"].ToString();
  855. //第一行添加客户信息
  856. if (i != rowNum - 1)
  857. {
  858. for (int j = 0; j < columnNum - 3; j++)
  859. {
  860. if (j == 0)
  861. {
  862. row1.CreateCell(j);
  863. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString());
  864. }
  865. else if (j > 5 && j == columnNum - 5)
  866. {
  867. row1.CreateCell(j);
  868. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  869. }
  870. else if (columnNum > 5 && j == columnNum - 5)
  871. {
  872. row1.CreateCell(j);
  873. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  874. }
  875. else
  876. {
  877. row1.CreateCell(j);
  878. }
  879. row1.GetCell(j).CellStyle = style;
  880. }
  881. row1 = sheet.CreateRow(PaintIndex);
  882. PaintIndex = PaintIndex + 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]["pr_orispeccode"].ToString() + DataTable.Rows[i]["me_desc"].ToString() + DataTable.Rows[i]["pr_size"].ToString());
  890. }
  891. else if (j > 5 && j == columnNum - 5)
  892. {
  893. row1.CreateCell(j);
  894. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  895. }
  896. else if (columnNum > 5 && j == columnNum - 5)
  897. {
  898. row1.CreateCell(j);
  899. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  900. }
  901. else
  902. {
  903. row1.CreateCell(j);
  904. }
  905. row1.GetCell(j).CellStyle = style;
  906. }
  907. //添加列名
  908. row1 = sheet.CreateRow(PaintIndex);
  909. PaintIndex = PaintIndex + 1;
  910. for (int j = 4; j < columnNum; j++)
  911. {
  912. //设定固定的列名
  913. row1.CreateCell(j - 4);
  914. row1.Cells[j - 4].CellStyle = styleborder;
  915. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  916. {
  917. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  918. }
  919. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  920. {
  921. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  922. }
  923. else
  924. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  925. if (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")))
  926. {
  927. sheet.SetColumnHidden(j - 4, true);
  928. }
  929. }
  930. row1 = sheet.CreateRow(PaintIndex);
  931. PaintIndex = PaintIndex + 1;
  932. }
  933. }
  934. //添加数据内容
  935. for (int j = 4; j < columnNum; j++)
  936. {
  937. string Data = DataTable.Rows[i][j].ToString();
  938. row1.CreateCell(j - 4);
  939. row1.Cells[j - 4].SetCellValue(Data);
  940. row1.GetCell(j - 4).CellStyle = styleborder;
  941. if (DataTable.Columns[j].ColumnName == "io_qty")
  942. {
  943. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  944. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  945. }
  946. if (DataTable.Columns[j].ColumnName == "rownum")
  947. {
  948. row1.Cells[j - 4].SetCellValue(i + 1);
  949. }
  950. }
  951. if (i == rowNum - 1)
  952. {
  953. row1 = sheet.CreateRow(PaintIndex);
  954. PaintIndex = PaintIndex + 1;
  955. for (int j = 0; j < columnNum - 4; j++)
  956. {
  957. row1.CreateCell(j);
  958. if (j == 0)
  959. {
  960. row1.Cells[j].SetCellValue("小计");
  961. }
  962. else if (DataTable.Columns[j].ColumnName == "io_qty")
  963. {
  964. row1.Cells[j - 4].SetCellValue(sumCount);
  965. }
  966. row1.Cells[j].CellStyle = styleborder;
  967. }
  968. row1 = sheet.CreateRow(PaintIndex);
  969. for (int j = 0; j < columnNum - 3; j++)
  970. {
  971. if (j == 0)
  972. {
  973. row1.CreateCell(j);
  974. row1.Cells[j].SetCellValue("备注");
  975. }
  976. else if (j == 2)
  977. {
  978. row1.CreateCell(j);
  979. row1.Cells[j].SetCellValue(totalCount);
  980. }
  981. else if (j == columnNum - 6)
  982. {
  983. row1.CreateCell(j);
  984. row1.Cells[j].SetCellValue(rowNum);
  985. }
  986. else if (j > 5 && j == columnNum - 5)
  987. {
  988. row1.CreateCell(j);
  989. row1.Cells[j].SetCellValue("片");
  990. }
  991. else if (columnNum > 5 && j == columnNum - 5)
  992. {
  993. row1.CreateCell(j);
  994. row1.Cells[j].SetCellValue("片");
  995. }
  996. else
  997. {
  998. row1.CreateCell(j);
  999. }
  1000. row1.Cells[j].CellStyle = style;
  1001. }
  1002. sheet.SetRowBreak(PaintIndex);
  1003. sheet.Footer.Center = "第&P页,共&N页";
  1004. PaintIndex = PaintIndex + 1;
  1005. }
  1006. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  1007. pib_outboxcode1.Add(BoxCode.ToString());
  1008. }
  1009. for (int i = 0; i < sheet.LastRowNum; i++)
  1010. {
  1011. if (i != 0)
  1012. {
  1013. sheet.AutoSizeColumn(i);
  1014. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  1015. }
  1016. }
  1017. break;
  1018. default:
  1019. break;
  1020. }
  1021. 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());
  1022. //删除下载链接再重新插入
  1023. HttpHandler.GenDownLoadLinK(Inoutno);
  1024. //填充首页
  1025. sumCount = 0;
  1026. totalCount = 0;
  1027. PaintIndex = 1;
  1028. ISheet sheet2 = book.CreateSheet("首页");
  1029. row = sheet2.CreateRow(0);
  1030. row.CreateCell(0);
  1031. row.Cells[0].SetCellValue(" 深爱半导体股份有限公司芯片出货清单");
  1032. row.GetCell(0).CellStyle = style;
  1033. rowNum = FirstDT.Rows.Count;
  1034. //不需要显示的列移除
  1035. for (int i = FirstDT.Columns.Count - 1; i > 0; i--)
  1036. {
  1037. for (int j = 0; j < box.Length; j++)
  1038. {
  1039. if (box[j].Name == "FirstPage_WID" && !box[j].Checked)
  1040. {
  1041. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  1042. {
  1043. FirstDT.Columns.RemoveAt(i);
  1044. }
  1045. }
  1046. if (box[j].Name == "FirstPage_YIELD" && !box[j].Checked)
  1047. {
  1048. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_yeild"))
  1049. {
  1050. FirstDT.Columns.RemoveAt(i);
  1051. }
  1052. }
  1053. if (box[j].Name == "FirstPage_REMARK" && !box[j].Checked)
  1054. {
  1055. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  1056. {
  1057. FirstDT.Columns.RemoveAt(i);
  1058. }
  1059. }
  1060. }
  1061. }
  1062. columnNum = FirstDT.Columns.Count;
  1063. for (int i = 0; i < rowNum; i++)
  1064. {
  1065. IRow row1 = sheet2.CreateRow(PaintIndex);
  1066. PaintIndex = PaintIndex + 1;
  1067. row1.HeightInPoints = RowHeight;
  1068. //只需要绘制一行
  1069. if (i == 0)
  1070. {
  1071. for (int j = 0; j < columnNum - 3; j++)
  1072. {
  1073. if (j == 0)
  1074. {
  1075. row1.CreateCell(j);
  1076. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["pi_title"].ToString());
  1077. }
  1078. else if (j > 5 && j == columnNum - 5)
  1079. {
  1080. row1.CreateCell(j);
  1081. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  1082. }
  1083. else if (columnNum > 5 && j == columnNum - 5)
  1084. {
  1085. row1.CreateCell(j);
  1086. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  1087. }
  1088. else
  1089. {
  1090. row1.CreateCell(j);
  1091. }
  1092. row1.GetCell(j).CellStyle = style;
  1093. }
  1094. row1 = sheet2.CreateRow(PaintIndex);
  1095. PaintIndex = PaintIndex + 1;
  1096. //第二行添加型号
  1097. for (int j = 0; j < columnNum - 3; j++)
  1098. {
  1099. if (j == 0)
  1100. {
  1101. row1.CreateCell(j);
  1102. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["pr_orispeccode"].ToString());
  1103. }
  1104. else if (j > 5 && j == columnNum - 5)
  1105. {
  1106. row1.CreateCell(j);
  1107. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  1108. }
  1109. else if (columnNum > 5 && j == columnNum - 5)
  1110. {
  1111. row1.CreateCell(j);
  1112. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  1113. }
  1114. else
  1115. {
  1116. row1.CreateCell(j);
  1117. }
  1118. row1.GetCell(j).CellStyle = style;
  1119. }
  1120. row1 = sheet2.CreateRow(PaintIndex);
  1121. PaintIndex = PaintIndex + 1;
  1122. //添加列名
  1123. for (int j = 4; j < columnNum; j++)
  1124. {
  1125. row1.CreateCell(j - 4);
  1126. row1.Cells[j - 4].CellStyle = styleborder;
  1127. row1.Cells[j - 4].SetCellValue(FirstDT.Columns[j].Caption);
  1128. }
  1129. row1 = sheet2.CreateRow(PaintIndex);
  1130. PaintIndex = PaintIndex + 1;
  1131. }
  1132. //添加数据内容
  1133. for (int j = 4; j < columnNum; j++)
  1134. {
  1135. string Data = FirstDT.Rows[i][j].ToString();
  1136. row1.CreateCell(j - 4);
  1137. row1.Cells[j - 4].SetCellValue(Data);
  1138. row1.GetCell(j - 4).CellStyle = styleborder;
  1139. if (FirstDT.Columns[j].ColumnName == "num")
  1140. {
  1141. sumCount += int.Parse(Data);
  1142. }
  1143. if (FirstDT.Columns[j].ColumnName == "io_qty")
  1144. {
  1145. totalCount += int.Parse(Data);
  1146. }
  1147. }
  1148. //添加总计行
  1149. if (i == rowNum - 1)
  1150. {
  1151. row1 = sheet2.CreateRow(PaintIndex);
  1152. PaintIndex = PaintIndex + 1;
  1153. for (int j = 0; j < columnNum - 4; j++)
  1154. {
  1155. if (j == 0)
  1156. {
  1157. row1.CreateCell(j);
  1158. row1.Cells[j].CellStyle = styleborder;
  1159. row1.Cells[j].SetCellValue("总计");
  1160. }
  1161. else if (j == columnNum - 6)
  1162. {
  1163. row1.CreateCell(j);
  1164. row1.Cells[j].CellStyle = styleborder;
  1165. row1.Cells[j].SetCellValue(sumCount);
  1166. }
  1167. else if (j == columnNum - 5)
  1168. {
  1169. row1.CreateCell(j);
  1170. row1.Cells[j].CellStyle = styleborder;
  1171. row1.Cells[j].SetCellValue(totalCount);
  1172. }
  1173. else
  1174. {
  1175. row1.CreateCell(j);
  1176. row1.Cells[j].CellStyle = styleborder;
  1177. }
  1178. }
  1179. }
  1180. }
  1181. for (int i = 0; i < sheet2.LastRowNum; i++)
  1182. {
  1183. if (i != 0)
  1184. {
  1185. sheet2.AutoSizeColumn(i);
  1186. sheet2.SetColumnWidth(i, sheet2.GetColumnWidth(i) + 1000);
  1187. }
  1188. }
  1189. //将book的内容写入内存流中返回
  1190. book.Write(ms);
  1191. return ms;
  1192. }
  1193. }
  1194. }