ExcelHandler.cs 61 KB

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