ExcelHandler.cs 61 KB

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