ExcelHandler.cs 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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. namespace UAS_LabelMachine
  13. {
  14. class ExcelHandler
  15. {
  16. DataHelper dh = SystemInf.dh;
  17. /// <summary>
  18. /// 导出Excel,返回文件在客户端的路径
  19. /// </summary>
  20. public string ExportExcel(DataTable dt, string FolderPath, string FileName)
  21. {
  22. //创建一个内存流,用来接收转换成Excel的内容
  23. MemoryStream ms;
  24. ms = DataTableToExcel(dt);
  25. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  26. string filePath = @FolderPath + "\\" + FileName + ".xls";
  27. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  28. byte[] data = ms.ToArray();
  29. fs.Write(data, 0, data.Length);
  30. fs.Flush();
  31. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  32. ms.Dispose();
  33. fs.Dispose();
  34. return filePath;
  35. }
  36. /// <summary>
  37. /// 导出Excel,返回文件在客户端的路径
  38. /// </summary>
  39. public string ExportExcel(DataTable firstsdt, DataTable dt, string FolderPath, string FileName, string Type, int PageSize, List<CheckBox> conditionbox)
  40. {
  41. //创建一个内存流,用来接收转换成Excel的内容
  42. MemoryStream ms;
  43. ms = DataTableToExcel1(firstsdt, dt, Type, PageSize, conditionbox);
  44. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  45. string filePath = @FolderPath + "\\" + FileName + ".xls";
  46. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  47. byte[] data = ms.ToArray();
  48. fs.Write(data, 0, data.Length);
  49. fs.Flush();
  50. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  51. ms.Dispose();
  52. fs.Dispose();
  53. return filePath;
  54. }
  55. /// <summary>
  56. /// 导入Excel
  57. /// </summary>
  58. public void ImportExcel(DataTable DataTable, string TableName)
  59. {
  60. int columnNum = DataTable.Columns.Count;
  61. int rowNum = DataTable.Columns.Count;
  62. string[] field = new string[columnNum];
  63. for (int i = 0; i < columnNum; i++)
  64. {
  65. field[i] = DataTable.Rows[0][i].ToString();
  66. }
  67. }
  68. public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
  69. {
  70. DataTable dataTable = null;
  71. FileStream fs = null;
  72. DataColumn column = null;
  73. DataRow dataRow = null;
  74. IWorkbook workbook = null;
  75. ISheet sheet = null;
  76. IRow row = null;
  77. ICell cell = null;
  78. int startRow = 0;
  79. try
  80. {
  81. using (fs = File.OpenRead(filePath))
  82. {
  83. // 2007版本
  84. if (filePath.IndexOf(".xlsx") > 0)
  85. {
  86. workbook = new XSSFWorkbook(fs);
  87. }
  88. // 2003版本
  89. else if (filePath.IndexOf(".xls") > 0)
  90. {
  91. workbook = new HSSFWorkbook(fs);
  92. }
  93. if (workbook != null)
  94. {
  95. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  96. dataTable = new DataTable();
  97. if (sheet != null)
  98. {
  99. int rowCount = sheet.LastRowNum;//总行数
  100. if (rowCount > 0)
  101. {
  102. IRow firstRow = sheet.GetRow(0);//第一行
  103. int cellCount = firstRow.LastCellNum;//列数
  104. //构建datatable的列
  105. if (isColumnName)
  106. {
  107. startRow = 1;//如果第一行是列名,则从第二行开始读取
  108. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  109. {
  110. cell = firstRow.GetCell(i);
  111. if (cell != null)
  112. {
  113. if (cell.StringCellValue != null)
  114. {
  115. column = new DataColumn(cell.StringCellValue);
  116. dataTable.Columns.Add(column);
  117. }
  118. }
  119. }
  120. }
  121. else
  122. {
  123. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  124. {
  125. column = new DataColumn("column" + (i + 1));
  126. dataTable.Columns.Add(column);
  127. }
  128. }
  129. //填充行
  130. for (int i = startRow; i <= rowCount; ++i)
  131. {
  132. row = sheet.GetRow(i);
  133. if (row == null) continue;
  134. dataRow = dataTable.NewRow();
  135. for (int j = row.FirstCellNum; j < cellCount; ++j)
  136. {
  137. cell = row.GetCell(j);
  138. if (cell == null)
  139. {
  140. dataRow[j] = "";
  141. }
  142. else
  143. {
  144. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  145. switch (cell.CellType)
  146. {
  147. case CellType.Blank:
  148. dataRow[j] = "";
  149. break;
  150. case CellType.Numeric:
  151. short format = cell.CellStyle.DataFormat;
  152. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  153. if (format == 14 || format == 31 || format == 57 || format == 58)
  154. dataRow[j] = cell.DateCellValue;
  155. else
  156. dataRow[j] = cell.NumericCellValue;
  157. break;
  158. case CellType.String:
  159. dataRow[j] = cell.StringCellValue;
  160. break;
  161. }
  162. }
  163. }
  164. dataTable.Rows.Add(dataRow);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. return dataTable;
  171. }
  172. catch (Exception)
  173. {
  174. if (fs != null)
  175. {
  176. fs.Close();
  177. }
  178. return null;
  179. }
  180. }
  181. /// <summary>
  182. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  183. /// </summary>
  184. /// <param name="DataTable"></param>
  185. /// <returns></returns>
  186. public MemoryStream DataTableToExcel(DataTable DataTable)
  187. {
  188. //创建内存流
  189. MemoryStream ms = new MemoryStream();
  190. //创建一个Book,相当于一个Excel文件
  191. HSSFWorkbook book = new HSSFWorkbook();
  192. //Excel中的Sheet
  193. ISheet sheet = book.CreateSheet("sheet1");
  194. //获取行数量和列数量
  195. int rowNum = DataTable.Rows.Count;
  196. int columnNum = DataTable.Columns.Count;
  197. //设置列的宽度,根据首行的列的内容的长度来设置
  198. for (int i = 0; i < columnNum; i++)
  199. {
  200. int dataLength;
  201. //如果内容比标题短则取标题长度
  202. if (DataTable.Rows[0][i].ToString().Length < DataTable.Columns[i].ColumnName.Length)
  203. {
  204. dataLength = DataTable.Columns[i].ColumnName.Length;
  205. dataLength = dataLength * 300;
  206. }
  207. else
  208. {
  209. dataLength = DataTable.Rows[0][i].ToString().Length;
  210. dataLength = dataLength * 300;
  211. }
  212. sheet.SetColumnWidth(i, dataLength);
  213. }
  214. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  215. IRow row = sheet.CreateRow(0);
  216. //冻结第一行
  217. sheet.CreateFreezePane(0, 1, 0, 1);
  218. ICellStyle style = book.CreateCellStyle();
  219. style.FillForegroundColor = HSSFColor.PaleBlue.Index;
  220. style.FillPattern = FillPattern.BigSpots;
  221. style.FillBackgroundColor = HSSFColor.LightGreen.Index;
  222. //设置边框
  223. style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thick;
  224. style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thick;
  225. style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thick;
  226. style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thick;
  227. row.HeightInPoints = 20;
  228. //固定第一行
  229. //row.RowStyle.IsLocked=true;
  230. //给第一行的标签赋值样式和值
  231. for (int j = 0; j < columnNum; j++)
  232. {
  233. row.CreateCell(j);
  234. row.Cells[j].CellStyle = style;
  235. row.Cells[j].CellStyle.VerticalAlignment = VerticalAlignment.Center;
  236. row.Cells[j].CellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  237. row.Cells[j].SetCellValue(DataTable.Columns[j].Caption);
  238. }
  239. //将DataTable的值循环赋值给book,Aligment设置居中
  240. //之前已经画了带颜色的第一行,所以从i=1开始画
  241. for (int i = 0; i < rowNum; i++)
  242. {
  243. IRow row1 = sheet.CreateRow(i + 1);
  244. row1.HeightInPoints = 20;
  245. for (int j = 0; j < columnNum; j++)
  246. {
  247. row1.CreateCell(j);
  248. row1.Cells[j].SetCellValue(DataTable.Rows[i][j].ToString());
  249. row1.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.Center;
  250. }
  251. }
  252. //将book的内容写入内存流中返回
  253. book.Write(ms);
  254. return ms;
  255. }
  256. /// <summary>
  257. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  258. /// </summary>
  259. /// <param name="DataTable"></param>
  260. /// <returns></returns>
  261. public MemoryStream DataTableToExcel1(DataTable FirstDT, DataTable DataTable, string Type, int PageSize, List<CheckBox> conditionbox)
  262. {
  263. //转换为序列
  264. CheckBox[] box = conditionbox.ToArray();
  265. //创建内存流
  266. MemoryStream ms = new MemoryStream();
  267. //创建一个Book,相当于一个Excel文件
  268. HSSFWorkbook book = new HSSFWorkbook();
  269. //Excel中的Sheet
  270. ISheet sheet = book.CreateSheet("分页");
  271. ICellStyle style = book.CreateCellStyle();
  272. style.VerticalAlignment = VerticalAlignment.Center;
  273. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  274. ICellStyle styleborder = book.CreateCellStyle();
  275. styleborder.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thick;
  276. styleborder.BorderTop = NPOI.SS.UserModel.BorderStyle.Thick;
  277. styleborder.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thick;
  278. styleborder.BorderRight = NPOI.SS.UserModel.BorderStyle.Thick;
  279. styleborder.VerticalAlignment = VerticalAlignment.Center;
  280. styleborder.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  281. //设置列的宽度,根据首行的列的内容的长度来设置
  282. for (int i = DataTable.Columns.Count - 1; i > 0; i--)
  283. {
  284. for (int j = 0; j < box.Length; j++)
  285. {
  286. if (box[j].Name.ToLower() == "ch_bluefilm" && !box[j].Checked)
  287. {
  288. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_bluefilm"))
  289. {
  290. DataTable.Columns.RemoveAt(i);
  291. break;
  292. }
  293. }
  294. if (box[j].Name.ToLower() == "ch_code" && !box[j].Checked)
  295. {
  296. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_code"))
  297. {
  298. DataTable.Columns.RemoveAt(i);
  299. break;
  300. }
  301. }
  302. if (box[j].Name.ToLower() == "ch_splitbatch" && !box[j].Checked)
  303. {
  304. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_splitbatch"))
  305. {
  306. DataTable.Columns.RemoveAt(i);
  307. break;
  308. }
  309. }
  310. if (box[j].Name.ToLower() == "ch_waterid" && !box[j].Checked)
  311. {
  312. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  313. {
  314. DataTable.Columns.RemoveAt(i);
  315. break;
  316. }
  317. }
  318. if (box[j].Name.ToLower() == "ch_pbcode" && !box[j].Checked)
  319. {
  320. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_pbcode"))
  321. {
  322. DataTable.Columns.RemoveAt(i);
  323. break;
  324. }
  325. }
  326. if (box[j].Name.ToLower() == "ch_remark" && !box[j].Checked)
  327. {
  328. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  329. {
  330. DataTable.Columns.RemoveAt(i);
  331. break;
  332. }
  333. }
  334. if (box[j].Name.ToLower() == "percent" && !box[j].Checked)
  335. {
  336. 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"))
  337. {
  338. DataTable.Columns.RemoveAt(i);
  339. break;
  340. }
  341. }
  342. if (box[j].Name.ToLower() == "ts" && !box[j].Checked)
  343. {
  344. if (DataTable.Columns[i].ColumnName.ToLower() == ("ts1") || DataTable.Columns[i].ColumnName.ToLower() == ("ts2") || DataTable.Columns[i].ColumnName.ToLower() == ("ts3"))
  345. {
  346. DataTable.Columns.RemoveAt(i);
  347. break;
  348. }
  349. }
  350. if (box[j].Name.ToLower() == "bvceo" && !box[j].Checked)
  351. {
  352. if (DataTable.Columns[i].ColumnName.ToLower() == "bvceo1" || DataTable.Columns[i].ColumnName.ToLower() == ("bvceo2") || DataTable.Columns[i].ColumnName.ToLower() == ("bvceo3"))
  353. {
  354. DataTable.Columns.RemoveAt(i);
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. //获取行数量和列数量
  361. int rowNum = DataTable.Rows.Count;
  362. int columnNum = DataTable.Columns.Count;
  363. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  364. IRow row = sheet.CreateRow(0);
  365. //冻结第一行
  366. sheet.CreateFreezePane(0, 1, 0, 1);
  367. //style.FillPattern = FillPattern.BigSpots;
  368. //style.FillBackgroundColor = HSSFColor.LightGreen.Index;
  369. //设置边框
  370. //style.BorderBottom = BorderStyle.Thick;
  371. //style.BorderLeft = BorderStyle.Thick;
  372. //style.BorderRight = BorderStyle.Thick;
  373. //style.BorderTop = BorderStyle.Thick;
  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_spec") && !First_Prspec.Contains(DataTable.Rows[i]["pr_spec"].ToString()))
  407. {
  408. First_Prspec += DataTable.Rows[i]["pr_spec"].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. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  426. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  427. dr["pd_ordercode"] = First_OrderCode;
  428. dr["pr_spec"] = First_Prspec;
  429. dr["ch_splitbatch"] = First_Batch;
  430. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  431. dr["num"] = PageSize;
  432. dr["io_qty"] = sumCount;
  433. FirstDT.Rows.Add(dr);
  434. First_OrderCode = "";
  435. First_Prspec = "";
  436. First_Batch = "";
  437. First_WID.Clear();
  438. for (int j = 0; j < columnNum - 4; j++)
  439. {
  440. if (j == 0)
  441. {
  442. row1.CreateCell(j);
  443. row1.Cells[j].SetCellValue("小计");
  444. }
  445. else if (j == 2)
  446. {
  447. row1.CreateCell(j);
  448. row1.Cells[j].SetCellValue(sumCount);
  449. }
  450. else
  451. {
  452. row1.CreateCell(j);
  453. }
  454. row1.Cells[j].CellStyle = styleborder;
  455. }
  456. sumCount = 0;
  457. row1 = sheet.CreateRow(PaintIndex);
  458. PaintIndex = PaintIndex + 1;
  459. }
  460. //每次到了页数开始分页
  461. if (i % PageSize == 0 || i == rowNum - 1)
  462. {
  463. //第一行添加客户信息 rownum只有一行的情
  464. if (i != rowNum - 1 || rowNum == 1)
  465. {
  466. for (int j = 0; j < columnNum - 3; j++)
  467. {
  468. if (j == 0)
  469. {
  470. row1.CreateCell(j);
  471. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString());
  472. }
  473. else if (j > 5 && j == columnNum - 5)
  474. {
  475. row1.CreateCell(j);
  476. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  477. }
  478. else if (columnNum > 5 && j == columnNum - 5)
  479. {
  480. row1.CreateCell(j);
  481. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  482. }
  483. else
  484. {
  485. row1.CreateCell(j);
  486. }
  487. row1.GetCell(j).CellStyle = style;
  488. }
  489. row1 = sheet.CreateRow(PaintIndex);
  490. PaintIndex = PaintIndex + 1;
  491. //第二行添加型号
  492. for (int j = 0; j < columnNum - 3; j++)
  493. {
  494. if (j == 0)
  495. {
  496. row1.CreateCell(j);
  497. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_spec"].ToString());
  498. }
  499. else if (columnNum > 5 && j == columnNum - 5)
  500. {
  501. row1.CreateCell(j);
  502. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  503. }
  504. else if (columnNum <= 5 && j == columnNum - 4)
  505. {
  506. row1.CreateCell(j);
  507. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  508. }
  509. else
  510. {
  511. row1.CreateCell(j);
  512. }
  513. row1.GetCell(j).CellStyle = style;
  514. }
  515. //添加列名
  516. row1 = sheet.CreateRow(PaintIndex);
  517. PaintIndex = PaintIndex + 1;
  518. for (int j = 4; j < columnNum; j++)
  519. {
  520. row1.CreateCell(j - 4);
  521. row1.Cells[j - 4].CellStyle = styleborder;
  522. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  523. {
  524. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  525. }
  526. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  527. {
  528. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  529. }
  530. else
  531. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  532. //如果chw_itemname1的值为空,则值为100和0,其中一列不显示,不显示
  533. 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"))
  534. {
  535. sheet.SetColumnHidden(j - 4, true);
  536. }
  537. }
  538. row1 = sheet.CreateRow(PaintIndex);
  539. PaintIndex = PaintIndex + 1;
  540. }
  541. }
  542. //添加数据内容
  543. for (int j = 4; j < columnNum; j++)
  544. {
  545. string Data = DataTable.Rows[i][j].ToString();
  546. row1.CreateCell(j - 4);
  547. row1.Cells[j - 4].SetCellValue(Data);
  548. row1.GetCell(j - 4).CellStyle = styleborder;
  549. if (DataTable.Columns[j].ColumnName == "io_qty")
  550. {
  551. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  552. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  553. }
  554. if (DataTable.Columns[j].ColumnName == "rownum")
  555. {
  556. row1.Cells[j - 4].SetCellValue(i + 1);
  557. }
  558. }
  559. //固定行号分组的时候自动拼接新的DataTable
  560. if (i == rowNum - 1)
  561. {
  562. DataRow dr = FirstDT.NewRow();
  563. dr["pr_spec"] = DataTable.Rows[i]["pr_spec"].ToString();
  564. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  565. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  566. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  567. dr["pd_ordercode"] = First_OrderCode;
  568. dr["pr_spec1"] = First_Prspec;
  569. dr["ch_splitbatch"] = First_Batch;
  570. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  571. dr["num"] = (i % PageSize) + 1;
  572. dr["io_qty"] = sumCount;
  573. FirstDT.Rows.Add(dr);
  574. row1 = sheet.CreateRow(PaintIndex);
  575. PaintIndex = PaintIndex + 1;
  576. for (int j = 0; j < columnNum - 4; j++)
  577. {
  578. if (j == 0)
  579. {
  580. row1.CreateCell(j);
  581. row1.Cells[j].SetCellValue("小计");
  582. }
  583. else if (j == 2)
  584. {
  585. row1.CreateCell(j);
  586. row1.Cells[j].SetCellValue(sumCount);
  587. }
  588. else
  589. {
  590. row1.CreateCell(j);
  591. }
  592. row1.Cells[j].CellStyle = styleborder;
  593. }
  594. row1 = sheet.CreateRow(PaintIndex);
  595. PaintIndex = PaintIndex + 1;
  596. for (int j = 0; j < columnNum - 3; j++)
  597. {
  598. if (j == 0)
  599. {
  600. row1.CreateCell(j);
  601. row1.Cells[j].SetCellValue("备注");
  602. }
  603. else if (j == 2)
  604. {
  605. row1.CreateCell(j);
  606. row1.Cells[j].SetCellValue(totalCount);
  607. }
  608. //原本是j == columnNum - 5因为还有spec和order两列隐藏列,所以需要在往后移动
  609. else if (j == columnNum - 6)
  610. {
  611. row1.CreateCell(j);
  612. row1.Cells[j].SetCellValue(rowNum);
  613. }
  614. else if (j > 5 && j == columnNum - 5)
  615. {
  616. row1.CreateCell(j);
  617. row1.Cells[j].SetCellValue("片");
  618. }
  619. else if (columnNum > 5 && j == columnNum - 5)
  620. {
  621. row1.CreateCell(j);
  622. row1.Cells[j].SetCellValue("片");
  623. }
  624. else
  625. {
  626. row1.CreateCell(j);
  627. }
  628. row1.Cells[j].CellStyle = style;
  629. }
  630. }
  631. }
  632. for (int i = 0; i < sheet.LastRowNum; i++)
  633. {
  634. if (i != 0)
  635. {
  636. sheet.AutoSizeColumn(i);
  637. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  638. }
  639. }
  640. break;
  641. case "BatchCode":
  642. string LastBatchCode = "";
  643. for (int i = 0; i < rowNum; i++)
  644. {
  645. IRow row1 = sheet.CreateRow(PaintIndex);
  646. PaintIndex = PaintIndex + 1;
  647. row1.HeightInPoints = 20;
  648. //如果批号不相等的时候
  649. if (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString())
  650. {
  651. for (int j = 0; j < columnNum - 4; j++)
  652. {
  653. if (j == 0)
  654. {
  655. row1.CreateCell(j);
  656. row1.Cells[j].SetCellValue("小计");
  657. }
  658. else if (j == 2)
  659. {
  660. row1.CreateCell(j);
  661. row1.Cells[j].SetCellValue(sumCount);
  662. }
  663. else
  664. {
  665. row1.CreateCell(j);
  666. }
  667. row1.Cells[j].CellStyle = styleborder;
  668. }
  669. sumCount = 0;
  670. row1 = sheet.CreateRow(PaintIndex);
  671. PaintIndex = PaintIndex + 1;
  672. }
  673. //每次到了页数开始分页
  674. if (LastBatchCode == "" || (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString()) || i == rowNum - 1)
  675. {
  676. LastBatchCode = DataTable.Rows[i]["ch_splitbatch"].ToString();
  677. //第一行添加客户信息
  678. if (i != rowNum - 1)
  679. {
  680. for (int j = 0; j < columnNum - 3; j++)
  681. {
  682. if (j == 0)
  683. {
  684. row1.CreateCell(j);
  685. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString());
  686. }
  687. else if (j == columnNum - 4)
  688. {
  689. row1.CreateCell(j);
  690. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  691. }
  692. else
  693. {
  694. row1.CreateCell(j);
  695. }
  696. row1.GetCell(j).CellStyle = style;
  697. }
  698. row1 = sheet.CreateRow(PaintIndex);
  699. PaintIndex = PaintIndex + 1;
  700. //第二行添加型号
  701. for (int j = 0; j < columnNum - 3; j++)
  702. {
  703. if (j == 0)
  704. {
  705. row1.CreateCell(j);
  706. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_spec"].ToString());
  707. }
  708. else if (j == columnNum - 4)
  709. {
  710. row1.CreateCell(j);
  711. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  712. }
  713. else
  714. {
  715. row1.CreateCell(j);
  716. }
  717. row1.GetCell(j).CellStyle = style;
  718. }
  719. //添加列名
  720. row1 = sheet.CreateRow(PaintIndex);
  721. PaintIndex = PaintIndex + 1;
  722. for (int j = 4; j < columnNum; j++)
  723. {
  724. row1.CreateCell(j - 4);
  725. row1.Cells[j - 4].CellStyle = styleborder;
  726. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  727. {
  728. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  729. }
  730. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  731. {
  732. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  733. }
  734. else
  735. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  736. 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"))
  737. {
  738. sheet.SetColumnHidden(j - 4, true);
  739. }
  740. }
  741. row1 = sheet.CreateRow(PaintIndex);
  742. PaintIndex = PaintIndex + 1;
  743. }
  744. }
  745. //添加数据内容
  746. for (int j = 4; j < columnNum; j++)
  747. {
  748. string Data = DataTable.Rows[i][j].ToString();
  749. row1.CreateCell(j - 4);
  750. row1.Cells[j - 4].SetCellValue(Data);
  751. row1.GetCell(j - 4).CellStyle = styleborder;
  752. if (DataTable.Columns[j].ColumnName == "io_qty")
  753. {
  754. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  755. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  756. }
  757. if (DataTable.Columns[j].ColumnName == "rownum")
  758. {
  759. row1.Cells[j - 4].SetCellValue(i + 1);
  760. }
  761. }
  762. if (i == rowNum - 1)
  763. {
  764. row1 = sheet.CreateRow(PaintIndex);
  765. PaintIndex = PaintIndex + 1;
  766. for (int j = 0; j < columnNum - 4; 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(sumCount);
  777. }
  778. else
  779. {
  780. row1.CreateCell(j);
  781. }
  782. row1.Cells[j].CellStyle = styleborder;
  783. }
  784. //创建备注内容
  785. row1 = sheet.CreateRow(PaintIndex);
  786. PaintIndex = PaintIndex + 1;
  787. for (int j = 0; j < columnNum - 3; j++)
  788. {
  789. if (j == 0)
  790. {
  791. row1.CreateCell(j);
  792. row1.Cells[j].SetCellValue("备注");
  793. }
  794. else if (j == 2)
  795. {
  796. row1.CreateCell(j);
  797. row1.Cells[j].SetCellValue(totalCount);
  798. }
  799. else if (j == columnNum - 6)
  800. {
  801. row1.CreateCell(j);
  802. row1.Cells[j].SetCellValue(rowNum);
  803. }
  804. else if (j > 5 && j == columnNum - 5)
  805. {
  806. row1.CreateCell(j);
  807. row1.Cells[j].SetCellValue("片");
  808. }
  809. else if (columnNum > 5 && j == columnNum - 5)
  810. {
  811. row1.CreateCell(j);
  812. row1.Cells[j].SetCellValue("片");
  813. }
  814. else
  815. {
  816. row1.CreateCell(j);
  817. }
  818. row1.Cells[j].CellStyle = style;
  819. }
  820. }
  821. }
  822. for (int i = 0; i < sheet.LastRowNum; i++)
  823. {
  824. if (i != 0)
  825. {
  826. sheet.AutoSizeColumn(i);
  827. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  828. }
  829. }
  830. break;
  831. case "BoxCode":
  832. string LastBoxCode = "";
  833. for (int i = 0; i < rowNum; i++)
  834. {
  835. IRow row1 = sheet.CreateRow(PaintIndex);
  836. PaintIndex = PaintIndex + 1;
  837. row1.HeightInPoints = 20;
  838. //如果批号不相等的时候
  839. if (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["CH_PBCODE"].ToString())
  840. {
  841. for (int j = 0; j < columnNum - 4; j++)
  842. {
  843. if (j == 0)
  844. {
  845. row1.CreateCell(j);
  846. row1.Cells[j].SetCellValue("小计");
  847. }
  848. else if (j == 2)
  849. {
  850. row1.CreateCell(j);
  851. row1.Cells[j].SetCellValue(sumCount);
  852. }
  853. else
  854. {
  855. row1.CreateCell(j);
  856. }
  857. row1.Cells[j].CellStyle = styleborder;
  858. }
  859. sumCount = 0;
  860. row1 = sheet.CreateRow(PaintIndex);
  861. PaintIndex = PaintIndex + 1;
  862. }
  863. //每次到了页数开始分页
  864. if (LastBoxCode == "" || (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["ch_pbcode"].ToString()) || i == rowNum - 1)
  865. {
  866. LastBoxCode = DataTable.Rows[i]["CH_PBCODE"].ToString();
  867. //第一行添加客户信息
  868. if (i != rowNum - 1)
  869. {
  870. for (int j = 0; j < columnNum - 3; j++)
  871. {
  872. if (j == 0)
  873. {
  874. row1.CreateCell(j);
  875. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString());
  876. }
  877. else if (j > 5 && j == columnNum - 5)
  878. {
  879. row1.CreateCell(j);
  880. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  881. }
  882. else if (columnNum > 5 && j == columnNum - 5)
  883. {
  884. row1.CreateCell(j);
  885. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  886. }
  887. else
  888. {
  889. row1.CreateCell(j);
  890. }
  891. row1.GetCell(j).CellStyle = style;
  892. }
  893. row1 = sheet.CreateRow(PaintIndex);
  894. PaintIndex = PaintIndex + 1;
  895. //第二行添加型号
  896. for (int j = 0; j < columnNum - 3; j++)
  897. {
  898. if (j == 0)
  899. {
  900. row1.CreateCell(j);
  901. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_spec"].ToString());
  902. }
  903. else if (j > 5 && j == columnNum - 5)
  904. {
  905. row1.CreateCell(j);
  906. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  907. }
  908. else if (columnNum > 5 && j == columnNum - 5)
  909. {
  910. row1.CreateCell(j);
  911. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  912. }
  913. else
  914. {
  915. row1.CreateCell(j);
  916. }
  917. row1.GetCell(j).CellStyle = style;
  918. }
  919. //添加列名
  920. row1 = sheet.CreateRow(PaintIndex);
  921. PaintIndex = PaintIndex + 1;
  922. for (int j = 4; j < columnNum; j++)
  923. {
  924. //设定固定的列名
  925. row1.CreateCell(j - 4);
  926. row1.Cells[j - 4].CellStyle = styleborder;
  927. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  928. {
  929. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  930. }
  931. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  932. {
  933. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  934. }
  935. else
  936. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  937. 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"))
  938. {
  939. sheet.SetColumnHidden(j - 4, true);
  940. }
  941. }
  942. row1 = sheet.CreateRow(PaintIndex);
  943. PaintIndex = PaintIndex + 1;
  944. }
  945. }
  946. //添加数据内容
  947. for (int j = 4; j < columnNum; j++)
  948. {
  949. string Data = DataTable.Rows[i][j].ToString();
  950. row1.CreateCell(j - 4);
  951. row1.Cells[j - 4].SetCellValue(Data);
  952. row1.GetCell(j - 4).CellStyle = styleborder;
  953. if (DataTable.Columns[j].ColumnName == "io_qty")
  954. {
  955. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  956. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  957. }
  958. if (DataTable.Columns[j].ColumnName == "rownum")
  959. {
  960. row1.Cells[j - 4].SetCellValue(i + 1);
  961. }
  962. }
  963. if (i == rowNum - 1)
  964. {
  965. row1 = sheet.CreateRow(PaintIndex);
  966. PaintIndex = PaintIndex + 1;
  967. for (int j = 0; j < columnNum - 4; j++)
  968. {
  969. if (j == 0)
  970. {
  971. row1.CreateCell(j);
  972. row1.Cells[j].SetCellValue("小计");
  973. }
  974. else if (j == 2)
  975. {
  976. row1.CreateCell(j);
  977. row1.Cells[j].SetCellValue(sumCount);
  978. }
  979. else
  980. {
  981. row1.CreateCell(j);
  982. }
  983. row1.Cells[j].CellStyle = styleborder;
  984. }
  985. row1 = sheet.CreateRow(PaintIndex);
  986. PaintIndex = PaintIndex + 1;
  987. for (int j = 0; j < columnNum - 3; j++)
  988. {
  989. if (j == 0)
  990. {
  991. row1.CreateCell(j);
  992. row1.Cells[j].SetCellValue("备注");
  993. }
  994. else if (j == 2)
  995. {
  996. row1.CreateCell(j);
  997. row1.Cells[j].SetCellValue(totalCount);
  998. }
  999. else if (j == columnNum - 6)
  1000. {
  1001. row1.CreateCell(j);
  1002. row1.Cells[j].SetCellValue(rowNum);
  1003. }
  1004. else if (j > 5 && j == columnNum - 5)
  1005. {
  1006. row1.CreateCell(j);
  1007. row1.Cells[j].SetCellValue("片");
  1008. }
  1009. else if (columnNum > 5 && j == columnNum - 5)
  1010. {
  1011. row1.CreateCell(j);
  1012. row1.Cells[j].SetCellValue("片");
  1013. }
  1014. else
  1015. {
  1016. row1.CreateCell(j);
  1017. }
  1018. row1.Cells[j].CellStyle = style;
  1019. }
  1020. }
  1021. }
  1022. for (int i = 0; i < sheet.LastRowNum; i++)
  1023. {
  1024. if (i != 0)
  1025. {
  1026. sheet.AutoSizeColumn(i);
  1027. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  1028. }
  1029. }
  1030. break;
  1031. default:
  1032. break;
  1033. }
  1034. //填充首页
  1035. sumCount = 0;
  1036. totalCount = 0;
  1037. PaintIndex = 1;
  1038. ISheet sheet2 = book.CreateSheet("首页");
  1039. row = sheet2.CreateRow(0);
  1040. row.CreateCell(0);
  1041. row.Cells[0].SetCellValue(" 深爱半导体有限公司芯片出货清单");
  1042. row.GetCell(0).CellStyle = style;
  1043. rowNum = FirstDT.Rows.Count;
  1044. //不需要显示的列移除
  1045. for (int i = FirstDT.Columns.Count - 1; i > 0; i--)
  1046. {
  1047. for (int j = 0; j < box.Length; j++)
  1048. {
  1049. if (box[j].Name == "FirstPage_WID" && !box[j].Checked)
  1050. {
  1051. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  1052. {
  1053. FirstDT.Columns.RemoveAt(i);
  1054. }
  1055. }
  1056. if (box[j].Name == "FirstPage_YIELD" && !box[j].Checked)
  1057. {
  1058. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_yeild"))
  1059. {
  1060. FirstDT.Columns.RemoveAt(i);
  1061. }
  1062. }
  1063. if (box[j].Name == "FirstPage_REMARK" && !box[j].Checked)
  1064. {
  1065. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  1066. {
  1067. FirstDT.Columns.RemoveAt(i);
  1068. }
  1069. }
  1070. }
  1071. }
  1072. columnNum = FirstDT.Columns.Count;
  1073. for (int i = 0; i < rowNum; i++)
  1074. {
  1075. IRow row1 = sheet2.CreateRow(PaintIndex);
  1076. PaintIndex = PaintIndex + 1;
  1077. row1.HeightInPoints = 20;
  1078. //只需要绘制一行
  1079. if (i == 0)
  1080. {
  1081. for (int j = 0; j < columnNum - 3; j++)
  1082. {
  1083. if (j == 0)
  1084. {
  1085. row1.CreateCell(j);
  1086. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["pi_title"].ToString());
  1087. }
  1088. else if (j > 5 && j == columnNum - 5)
  1089. {
  1090. row1.CreateCell(j);
  1091. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  1092. }
  1093. else if (columnNum > 5 && j == columnNum - 5)
  1094. {
  1095. row1.CreateCell(j);
  1096. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  1097. }
  1098. else
  1099. {
  1100. row1.CreateCell(j);
  1101. }
  1102. row1.GetCell(j).CellStyle = style;
  1103. }
  1104. row1 = sheet2.CreateRow(PaintIndex);
  1105. PaintIndex = PaintIndex + 1;
  1106. //第二行添加型号
  1107. for (int j = 0; j < columnNum - 3; j++)
  1108. {
  1109. if (j == 0)
  1110. {
  1111. row1.CreateCell(j);
  1112. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["pr_spec"].ToString());
  1113. }
  1114. else if (j > 5 && j == columnNum - 5)
  1115. {
  1116. row1.CreateCell(j);
  1117. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  1118. }
  1119. else if (columnNum > 5 && j == columnNum - 5)
  1120. {
  1121. row1.CreateCell(j);
  1122. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  1123. }
  1124. else
  1125. {
  1126. row1.CreateCell(j);
  1127. }
  1128. row1.GetCell(j).CellStyle = style;
  1129. }
  1130. row1 = sheet2.CreateRow(PaintIndex);
  1131. PaintIndex = PaintIndex + 1;
  1132. //添加列名
  1133. for (int j = 4; j < columnNum; j++)
  1134. {
  1135. row1.CreateCell(j - 4);
  1136. row1.Cells[j - 4].CellStyle = styleborder;
  1137. row1.Cells[j - 4].SetCellValue(FirstDT.Columns[j].Caption);
  1138. }
  1139. row1 = sheet2.CreateRow(PaintIndex);
  1140. PaintIndex = PaintIndex + 1;
  1141. }
  1142. //添加数据内容
  1143. for (int j = 4; j < columnNum; j++)
  1144. {
  1145. string Data = FirstDT.Rows[i][j].ToString();
  1146. row1.CreateCell(j - 4);
  1147. row1.Cells[j - 4].SetCellValue(Data);
  1148. row1.GetCell(j - 4).CellStyle = styleborder;
  1149. if (FirstDT.Columns[j].ColumnName == "num")
  1150. {
  1151. sumCount += int.Parse(Data);
  1152. }
  1153. if (FirstDT.Columns[j].ColumnName == "io_qty")
  1154. {
  1155. totalCount += int.Parse(Data);
  1156. }
  1157. }
  1158. //添加总计行
  1159. if (i == rowNum - 1)
  1160. {
  1161. row1 = sheet2.CreateRow(PaintIndex);
  1162. PaintIndex = PaintIndex + 1;
  1163. for (int j = 0; j < columnNum - 4; j++)
  1164. {
  1165. if (j == 0)
  1166. {
  1167. row1.CreateCell(j);
  1168. row1.Cells[j].CellStyle = styleborder;
  1169. row1.Cells[j].SetCellValue("总计");
  1170. }
  1171. else if (j == columnNum - 6)
  1172. {
  1173. row1.CreateCell(j);
  1174. row1.Cells[j].CellStyle = styleborder;
  1175. row1.Cells[j].SetCellValue(sumCount);
  1176. }
  1177. else if (j == columnNum - 5)
  1178. {
  1179. row1.CreateCell(j);
  1180. row1.Cells[j].CellStyle = styleborder;
  1181. row1.Cells[j].SetCellValue(totalCount);
  1182. }
  1183. else
  1184. {
  1185. row1.CreateCell(j);
  1186. row1.Cells[j].CellStyle = styleborder;
  1187. }
  1188. }
  1189. }
  1190. }
  1191. for (int i = 0; i < sheet2.LastRowNum; i++)
  1192. {
  1193. if (i != 0)
  1194. {
  1195. sheet2.AutoSizeColumn(i);
  1196. sheet2.SetColumnWidth(i, sheet2.GetColumnWidth(i) + 1000);
  1197. }
  1198. }
  1199. //将book的内容写入内存流中返回
  1200. book.Write(ms);
  1201. return ms;
  1202. }
  1203. }
  1204. }