ExcelHandler.cs 62 KB

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