ExcelHandler.cs 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  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_Special(DataTable firstsdt, DataTable dt, string FolderPath, string FileName, string Type, int PageSize, List<CheckBox> conditionbox)
  41. {
  42. //创建一个内存流,用来接收转换成Excel的内容
  43. MemoryStream ms;
  44. ms = DataTableToExcel2(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 string ExportExcel(DataTable firstsdt, DataTable dt, string FolderPath, string FileName, string Type, int PageSize, List<CheckBox> conditionbox)
  60. {
  61. //创建一个内存流,用来接收转换成Excel的内容
  62. MemoryStream ms;
  63. ms = DataTableToExcel1(firstsdt, dt, Type, FileName, PageSize, conditionbox);
  64. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  65. string filePath = @FolderPath + "\\" + FileName + ".xls";
  66. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  67. byte[] data = ms.ToArray();
  68. fs.Write(data, 0, data.Length);
  69. fs.Flush();
  70. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  71. ms.Dispose();
  72. fs.Dispose();
  73. return filePath;
  74. }
  75. /// <summary>
  76. /// 导入Excel
  77. /// </summary>
  78. public void ImportExcel(DataTable DataTable, string TableName)
  79. {
  80. int columnNum = DataTable.Columns.Count;
  81. int rowNum = DataTable.Columns.Count;
  82. string[] field = new string[columnNum];
  83. for (int i = 0; i < columnNum; i++)
  84. {
  85. field[i] = DataTable.Rows[0][i].ToString();
  86. }
  87. }
  88. public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
  89. {
  90. DataTable dataTable = null;
  91. FileStream fs = null;
  92. DataColumn column = null;
  93. DataRow dataRow = null;
  94. IWorkbook workbook = null;
  95. ISheet sheet = null;
  96. IRow row = null;
  97. ICell cell = null;
  98. int startRow = 0;
  99. try
  100. {
  101. using (fs = File.OpenRead(filePath))
  102. {
  103. // 2007版本
  104. if (filePath.IndexOf(".xlsx") > 0)
  105. {
  106. workbook = new XSSFWorkbook(fs);
  107. }
  108. // 2003版本
  109. else if (filePath.IndexOf(".xls") > 0)
  110. {
  111. workbook = new HSSFWorkbook(fs);
  112. }
  113. if (workbook != null)
  114. {
  115. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  116. dataTable = new DataTable();
  117. if (sheet != null)
  118. {
  119. int rowCount = sheet.LastRowNum;//总行数
  120. if (rowCount > 0)
  121. {
  122. IRow firstRow = sheet.GetRow(0);//第一行
  123. int cellCount = firstRow.LastCellNum;//列数
  124. //构建datatable的列
  125. if (isColumnName)
  126. {
  127. startRow = 1;//如果第一行是列名,则从第二行开始读取
  128. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  129. {
  130. cell = firstRow.GetCell(i);
  131. if (cell != null)
  132. {
  133. if (cell.StringCellValue != null)
  134. {
  135. column = new DataColumn(cell.StringCellValue);
  136. dataTable.Columns.Add(column);
  137. }
  138. }
  139. }
  140. }
  141. else
  142. {
  143. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  144. {
  145. column = new DataColumn("column" + (i + 1));
  146. dataTable.Columns.Add(column);
  147. }
  148. }
  149. //填充行
  150. for (int i = startRow; i <= rowCount; ++i)
  151. {
  152. row = sheet.GetRow(i);
  153. if (row == null) continue;
  154. dataRow = dataTable.NewRow();
  155. for (int j = row.FirstCellNum; j < cellCount; ++j)
  156. {
  157. cell = row.GetCell(j);
  158. if (cell == null)
  159. {
  160. dataRow[j] = "";
  161. }
  162. else
  163. {
  164. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  165. switch (cell.CellType)
  166. {
  167. case CellType.Blank:
  168. dataRow[j] = "";
  169. break;
  170. case CellType.Numeric:
  171. short format = cell.CellStyle.DataFormat;
  172. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  173. if (format == 14 || format == 31 || format == 57 || format == 58)
  174. dataRow[j] = cell.DateCellValue;
  175. else
  176. dataRow[j] = cell.NumericCellValue;
  177. break;
  178. case CellType.String:
  179. dataRow[j] = cell.StringCellValue;
  180. break;
  181. }
  182. }
  183. }
  184. dataTable.Rows.Add(dataRow);
  185. }
  186. }
  187. }
  188. }
  189. }
  190. return dataTable;
  191. }
  192. catch (Exception)
  193. {
  194. if (fs != null)
  195. {
  196. fs.Close();
  197. }
  198. return null;
  199. }
  200. }
  201. int RowHeight = 12;
  202. /// <summary>
  203. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  204. /// </summary>
  205. /// <param name="DataTable"></param>
  206. /// <returns></returns>
  207. public MemoryStream DataTableToExcel(DataTable DataTable)
  208. {
  209. //创建内存流
  210. MemoryStream ms = new MemoryStream();
  211. //创建一个Book,相当于一个Excel文件
  212. HSSFWorkbook book = new HSSFWorkbook();
  213. //Excel中的Sheet
  214. ISheet sheet = book.CreateSheet("sheet1");
  215. //获取行数量和列数量
  216. int rowNum = DataTable.Rows.Count;
  217. int columnNum = DataTable.Columns.Count;
  218. //设置列的宽度,根据首行的列的内容的长度来设置
  219. for (int i = 0; i < columnNum; i++)
  220. {
  221. int dataLength;
  222. //如果内容比标题短则取标题长度
  223. if (DataTable.Rows[0][i].ToString().Length < DataTable.Columns[i].ColumnName.Length)
  224. {
  225. dataLength = DataTable.Columns[i].ColumnName.Length;
  226. dataLength = dataLength * 300;
  227. }
  228. else
  229. {
  230. dataLength = DataTable.Rows[0][i].ToString().Length;
  231. dataLength = dataLength * 300;
  232. }
  233. sheet.SetColumnWidth(i, dataLength);
  234. }
  235. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  236. IRow row = sheet.CreateRow(0);
  237. //冻结第一行
  238. sheet.CreateFreezePane(0, 1, 0, 1);
  239. ICellStyle style = book.CreateCellStyle();
  240. style.FillForegroundColor = HSSFColor.PaleBlue.Index;
  241. style.FillPattern = FillPattern.BigSpots;
  242. style.FillBackgroundColor = HSSFColor.LightGreen.Index;
  243. //设置边框
  244. style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  245. style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  246. style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  247. style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  248. row.HeightInPoints = 20;
  249. //固定第一行
  250. //row.RowStyle.IsLocked=true;
  251. //给第一行的标签赋值样式和值
  252. for (int j = 0; j < columnNum; j++)
  253. {
  254. row.CreateCell(j);
  255. row.Cells[j].CellStyle = style;
  256. row.Cells[j].CellStyle.VerticalAlignment = VerticalAlignment.Center;
  257. row.Cells[j].CellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  258. row.Cells[j].SetCellValue(DataTable.Columns[j].Caption);
  259. }
  260. //将DataTable的值循环赋值给book,Aligment设置居中
  261. //之前已经画了带颜色的第一行,所以从i=1开始画
  262. for (int i = 0; i < rowNum; i++)
  263. {
  264. IRow row1 = sheet.CreateRow(i + 1);
  265. row1.HeightInPoints = 20;
  266. for (int j = 0; j < columnNum; j++)
  267. {
  268. row1.CreateCell(j);
  269. row1.Cells[j].SetCellValue(DataTable.Rows[i][j].ToString());
  270. row1.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.Center;
  271. }
  272. }
  273. //将book的内容写入内存流中返回
  274. book.Write(ms);
  275. return ms;
  276. }
  277. /// <summary>
  278. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  279. /// </summary>
  280. /// <param name="DataTable"></param>
  281. /// <returns></returns>
  282. public MemoryStream DataTableToExcel1(DataTable FirstDT, DataTable DataTable, string Type, string Inoutno, int PageSize, List<CheckBox> conditionbox)
  283. {
  284. //转换为序列
  285. CheckBox[] box = conditionbox.ToArray();
  286. //创建内存流
  287. MemoryStream ms = new MemoryStream();
  288. //创建一个Book,相当于一个Excel文件
  289. HSSFWorkbook book = new HSSFWorkbook();
  290. //Excel中的Sheet
  291. ISheet sheet = book.CreateSheet("分页");
  292. sheet.SetMargin(MarginType.TopMargin, 0.4);
  293. sheet.SetMargin(MarginType.BottomMargin, 0.4);
  294. sheet.SetMargin(MarginType.LeftMargin, 0.4);
  295. sheet.SetMargin(MarginType.RightMargin, 0.4);
  296. //芯片号需要作为更新盒号的条件
  297. HSSFFont ffont = (HSSFFont)book.CreateFont();
  298. ffont.FontName = "宋体";
  299. bool ShowChcode = true;
  300. //更新箱号
  301. List<string> pib_id = new List<string>();
  302. //系统打印箱号
  303. List<string> pib_outboxcode1 = new List<string>();
  304. int BoxCode = 1;
  305. ICellStyle style = book.CreateCellStyle();
  306. style.VerticalAlignment = VerticalAlignment.Center;
  307. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  308. style.SetFont(ffont);
  309. ICellStyle styleborder = book.CreateCellStyle();
  310. styleborder.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  311. styleborder.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  312. styleborder.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  313. styleborder.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  314. styleborder.VerticalAlignment = VerticalAlignment.Center;
  315. styleborder.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  316. styleborder.SetFont(ffont);
  317. string pi_inoutno = "";
  318. List<string> NotShowColumn = new List<string>();
  319. NotShowColumn.Add("ch_level");
  320. NotShowColumn.Add("pib_id");
  321. NotShowColumn.Add("chw_itemname1");
  322. NotShowColumn.Add("chw_itemname2");
  323. NotShowColumn.Add("pd_ordercode");
  324. NotShowColumn.Add("pr_size");
  325. NotShowColumn.Add("me_desc");
  326. NotShowColumn.Add("pr_orispeccode1");
  327. NotShowColumn.Add("pi_title");
  328. //展示的内容列
  329. int ShowColumnsCount = 0;
  330. //设置列的宽度,根据首行的列的内容的长度来设置
  331. for (int i = DataTable.Columns.Count - 1; i > 0; i--)
  332. {
  333. //统计显示的列数
  334. if (!NotShowColumn.Contains(DataTable.Columns[i].ColumnName.ToLower()))
  335. {
  336. ShowColumnsCount = ShowColumnsCount + 1;
  337. }
  338. for (int j = 0; j < box.Length; j++)
  339. {
  340. if (box[j].Name.ToLower() == "ch_bluefilm" && !box[j].Checked)
  341. {
  342. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_bluefilm"))
  343. {
  344. DataTable.Columns.RemoveAt(i);
  345. break;
  346. }
  347. }
  348. if (box[j].Name.ToLower() == "ch_code" && !box[j].Checked)
  349. {
  350. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_code"))
  351. {
  352. ShowChcode = true;
  353. break;
  354. }
  355. }
  356. if (box[j].Name.ToLower() == "ch_splitbatch" && !box[j].Checked)
  357. {
  358. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_splitbatch"))
  359. {
  360. DataTable.Columns.RemoveAt(i);
  361. break;
  362. }
  363. }
  364. if (box[j].Name.ToLower() == "ch_waterid" && !box[j].Checked)
  365. {
  366. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  367. {
  368. DataTable.Columns.RemoveAt(i);
  369. break;
  370. }
  371. }
  372. if (box[j].Name.ToLower() == "ch_pbcode" && !box[j].Checked)
  373. {
  374. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_pbcode"))
  375. {
  376. DataTable.Columns.RemoveAt(i);
  377. break;
  378. }
  379. }
  380. if (box[j].Name.ToLower() == "ch_remark" && !box[j].Checked)
  381. {
  382. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  383. {
  384. DataTable.Columns.RemoveAt(i);
  385. break;
  386. }
  387. }
  388. }
  389. }
  390. //获取行数量和列数量
  391. int rowNum = DataTable.Rows.Count;
  392. int columnNum = DataTable.Columns.Count;
  393. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  394. IRow row = sheet.CreateRow(0);
  395. //冻结第一行
  396. sheet.CreateFreezePane(0, 1, 0, 1);
  397. row.HeightInPoints = RowHeight;
  398. //固定第一行
  399. //row.RowStyle.IsLocked=true;
  400. //给第一行的标签赋值样式和值
  401. // ffont.FontHeight = 13;
  402. row.CreateCell(0);
  403. row.Cells[0].SetCellValue(" 深爱半导体股份有限公司芯片出货清单");
  404. row.GetCell(0).CellStyle.SetFont((ffont));
  405. //ffont.FontHeight = 10;
  406. //开始绘制的Index
  407. int PaintIndex = 1;
  408. int sumCount = 0;
  409. int totalCount = 0;
  410. switch (Type)
  411. {
  412. case "FixRow":
  413. //清理系统取出来的数据
  414. BaseUtil.CleanDataTableData(FirstDT);
  415. //首页参数拼接
  416. string First_OrderCode = "";
  417. string First_Prspec = "";
  418. string First_Batch = "";
  419. int NumIndex = 0;
  420. ArrayList<string> First_WID = new ArrayList<string>();
  421. for (int i = 0; i < rowNum; i++)
  422. {
  423. IRow row1 = sheet.CreateRow(PaintIndex);
  424. PaintIndex = PaintIndex + 1;
  425. row1.HeightInPoints = RowHeight;
  426. //不包含的订单号
  427. if (DataTable.Columns.Contains("pd_ordercode") && !First_OrderCode.Contains(DataTable.Rows[i]["pd_ordercode"].ToString()))
  428. {
  429. First_OrderCode += DataTable.Rows[i]["pd_ordercode"].ToString() + " ";
  430. }
  431. //不包含的物料型号
  432. if (DataTable.Columns.Contains("pr_orispeccode1") && !First_Prspec.Contains(DataTable.Rows[i]["pr_orispeccode1"].ToString()))
  433. {
  434. First_Prspec += DataTable.Rows[i]["pr_orispeccode1"].ToString() + " ";
  435. }
  436. //不包含扩撒批号
  437. if (DataTable.Columns.Contains("ch_splitbatch") && !First_Batch.Contains(DataTable.Rows[i]["ch_splitbatch"].ToString()))
  438. {
  439. First_Batch += DataTable.Rows[i]["ch_splitbatch"].ToString() + " ";
  440. }
  441. //不包含Wafer_id
  442. if (DataTable.Columns.Contains("Wafer_ID") && !First_WID.Contains(DataTable.Rows[i]["Wafer_ID"].ToString()))
  443. {
  444. First_WID.Add(DataTable.Rows[i]["Wafer_ID"].ToString());
  445. }
  446. if (i / PageSize >= 1 && i % PageSize == 0)
  447. {
  448. DataRow dr = FirstDT.NewRow();
  449. dr["pr_orispeccode"] = DataTable.Rows[i]["pr_orispeccode"].ToString();
  450. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  451. pi_inoutno = DataTable.Rows[i]["pi_inoutno"].ToString();
  452. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  453. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  454. dr["pd_ordercode"] = First_OrderCode;
  455. dr["pr_orispeccode1"] = First_Prspec;
  456. dr["ch_splitbatch"] = First_Batch;
  457. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  458. dr["num"] = PageSize;
  459. dr["io_qty"] = sumCount;
  460. FirstDT.Rows.Add(dr);
  461. First_OrderCode = "";
  462. First_Prspec = "";
  463. First_Batch = "";
  464. First_WID.Clear();
  465. BoxCode = BoxCode + 1;
  466. for (int j = 0; j < columnNum - 4; j++)
  467. {
  468. row1.CreateCell(j);
  469. if (j == 0)
  470. {
  471. row1.Cells[j].SetCellValue("小计");
  472. }
  473. else if (j == 1)
  474. {
  475. row1.Cells[1].SetCellValue((i % PageSize == 0 ? PageSize : i) + "片");
  476. }
  477. else if (j == 2)
  478. {
  479. row1.Cells[2].SetCellValue(sumCount);
  480. }
  481. row1.Cells[j].CellStyle = styleborder;
  482. }
  483. sumCount = 0;
  484. row1 = sheet.CreateRow(PaintIndex);
  485. sheet.SetRowBreak(PaintIndex - 1);
  486. sheet.Footer.Center = "第&P页,共&N页";
  487. PaintIndex = PaintIndex + 1;
  488. }
  489. //每次到了页数开始分页
  490. if (i % PageSize == 0 || i == rowNum - 1)
  491. {
  492. //第一行添加客户信息 rownum只有一行的情
  493. if (i != rowNum - 1 || rowNum == 1)
  494. {
  495. for (int j = 0; j < columnNum - 3; j++)
  496. {
  497. if (j == 0)
  498. {
  499. row1.CreateCell(j);
  500. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString() + " " + DataTable.Rows[i]["ch_level"].ToString());
  501. }
  502. else if (columnNum > 14 && j == columnNum - 14)
  503. {
  504. row1.CreateCell(j);
  505. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  506. }
  507. else
  508. {
  509. row1.CreateCell(j);
  510. }
  511. row1.GetCell(j).CellStyle = style;
  512. }
  513. row1 = sheet.CreateRow(PaintIndex);
  514. PaintIndex = PaintIndex + 1;
  515. //第二行添加型号
  516. for (int j = 0; j < columnNum - 3; j++)
  517. {
  518. if (j == 0)
  519. {
  520. row1.CreateCell(j);
  521. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_orispeccode"].ToString() + DataTable.Rows[i]["me_desc"].ToString() + DataTable.Rows[i]["pr_size"].ToString());
  522. }
  523. else if (columnNum > 14 && j == columnNum - 14)
  524. {
  525. row1.CreateCell(j);
  526. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  527. }
  528. else
  529. {
  530. row1.CreateCell(j);
  531. }
  532. row1.GetCell(j).CellStyle = style;
  533. }
  534. //添加列名
  535. row1 = sheet.CreateRow(PaintIndex);
  536. PaintIndex = PaintIndex + 1;
  537. //计数列所在的索引
  538. for (int j = 4; j < columnNum; j++)
  539. {
  540. row1.CreateCell(j - 4);
  541. row1.Cells[j - 4].CellStyle = styleborder;
  542. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  543. {
  544. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  545. }
  546. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  547. {
  548. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  549. }
  550. else
  551. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  552. if (DataTable.Columns[j].ColumnName.ToString() == "io_qty")
  553. {
  554. NumIndex = j;
  555. }
  556. //如果chw_itemname1的值为空,则值为100和0,其中一列不显示,不显示
  557. if ((NotShowColumn.Contains(DataTable.Columns[j].ColumnName.ToLower())) || (!ShowChcode && DataTable.Columns[j].ColumnName.ToLower().Contains("ch_code")))
  558. {
  559. sheet.SetColumnHidden(j - 4, true);
  560. }
  561. }
  562. row1 = sheet.CreateRow(PaintIndex);
  563. PaintIndex = PaintIndex + 1;
  564. }
  565. }
  566. //添加数据内容
  567. for (int j = 4; j < columnNum; j++)
  568. {
  569. string Data = DataTable.Rows[i][j].ToString();
  570. row1.CreateCell(j - 4);
  571. row1.Cells[j - 4].SetCellValue(Data);
  572. row1.GetCell(j - 4).CellStyle = styleborder;
  573. if (DataTable.Columns[j].ColumnName == "io_qty")
  574. {
  575. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  576. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  577. }
  578. if (DataTable.Columns[j].ColumnName == "rownum")
  579. {
  580. row1.Cells[j - 4].SetCellValue(i + 1);
  581. }
  582. }
  583. //固定行号分组的时候自动拼接新的DataTable
  584. if (i == rowNum - 1)
  585. {
  586. DataRow dr = FirstDT.NewRow();
  587. dr["pr_orispeccode"] = DataTable.Rows[i]["pr_orispeccode"].ToString();
  588. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  589. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  590. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  591. dr["pd_ordercode"] = First_OrderCode;
  592. dr["pr_orispeccode1"] = First_Prspec;
  593. dr["ch_splitbatch"] = First_Batch;
  594. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  595. dr["num"] = (i % PageSize) + 1;
  596. dr["io_qty"] = sumCount;
  597. FirstDT.Rows.Add(dr);
  598. row1 = sheet.CreateRow(PaintIndex);
  599. PaintIndex = PaintIndex + 1;
  600. for (int j = 0; j < columnNum - 4; j++)
  601. {
  602. row1.CreateCell(j);
  603. if (j == 0)
  604. {
  605. row1.Cells[j].SetCellValue("小计");
  606. }
  607. else if (j == 1)
  608. {
  609. row1.Cells[1].SetCellValue((i % PageSize == 0 ? PageSize : (i % PageSize + 1)) + "片");
  610. }
  611. else if (j == 2)
  612. {
  613. row1.Cells[2].SetCellValue(sumCount);
  614. }
  615. row1.Cells[j].CellStyle = styleborder;
  616. }
  617. row1 = sheet.CreateRow(PaintIndex);
  618. //for (int j = 0; j < columnNum - 3; j++)
  619. //{
  620. // if (j == 0)
  621. // {
  622. // row1.CreateCell(j);
  623. // row1.Cells[j].SetCellValue("备注");
  624. // }
  625. // else if (j == 2)
  626. // {
  627. // row1.CreateCell(j);
  628. // row1.Cells[j].SetCellValue(totalCount);
  629. // }
  630. // //原本是j == columnNum - 5因为还有spec和order两列隐藏列,所以需要在往后移动
  631. // else if (j == columnNum - 6)
  632. // {
  633. // row1.CreateCell(j);
  634. // row1.Cells[j].SetCellValue(rowNum);
  635. // }
  636. // else if (j > 5 && j == columnNum - 5)
  637. // {
  638. // row1.CreateCell(j);
  639. // row1.Cells[j].SetCellValue("片");
  640. // }
  641. // else if (columnNum > 5 && j == columnNum - 5)
  642. // {
  643. // row1.CreateCell(j);
  644. // row1.Cells[j].SetCellValue("片");
  645. // }
  646. // else
  647. // {
  648. // row1.CreateCell(j);
  649. // }
  650. // row1.Cells[j].CellStyle = style;
  651. //}
  652. sheet.SetRowBreak(PaintIndex);
  653. sheet.Footer.Center = "第&P页,共&N页";
  654. PaintIndex = PaintIndex + 1;
  655. }
  656. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  657. pib_outboxcode1.Add(BoxCode.ToString());
  658. }
  659. for (int i = 0; i < sheet.LastRowNum; i++)
  660. {
  661. if (i != 0)
  662. {
  663. sheet.AutoSizeColumn(i);
  664. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  665. }
  666. }
  667. break;
  668. case "BatchCode":
  669. string LastBatchCode = "";
  670. int PageNum = 0;
  671. for (int i = 0; i < rowNum; i++)
  672. {
  673. IRow row1 = sheet.CreateRow(PaintIndex);
  674. PaintIndex = PaintIndex + 1;
  675. row1.HeightInPoints = RowHeight;
  676. //如果批号不相等的时候
  677. PageNum = PageNum + 1;
  678. if (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString())
  679. {
  680. BoxCode = BoxCode + 1;
  681. for (int j = 0; j < columnNum - 4; j++)
  682. {
  683. row1.CreateCell(j);
  684. if (j == 0)
  685. {
  686. row1.Cells[j].SetCellValue("小计");
  687. row1.Cells[j].CellStyle = styleborder;
  688. }
  689. else if (j == 1)
  690. {
  691. row1.Cells[1].SetCellValue(PageNum - 1 + "片");
  692. }
  693. else if (j == 2)
  694. {
  695. row1.Cells[2].SetCellValue(sumCount);
  696. row1.Cells[j].CellStyle = styleborder;
  697. }
  698. else
  699. {
  700. row1.Cells[j].CellStyle = styleborder;
  701. }
  702. }
  703. sumCount = 0;
  704. row1 = sheet.CreateRow(PaintIndex);
  705. sheet.SetRowBreak(PaintIndex - 1);
  706. sheet.Footer.Center = "第&P页,共&N页";
  707. PaintIndex = PaintIndex + 1;
  708. PageNum = 1;
  709. }
  710. //每次到了页数开始分页
  711. if (LastBatchCode == "" || (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString()) || i == rowNum - 1)
  712. {
  713. LastBatchCode = DataTable.Rows[i]["ch_splitbatch"].ToString();
  714. //第一行添加客户信息
  715. if (i != rowNum - 1)
  716. {
  717. for (int j = 0; j < columnNum - 3; j++)
  718. {
  719. if (j == 0)
  720. {
  721. row1.CreateCell(j);
  722. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString() + " " + DataTable.Rows[i]["ch_level"].ToString());
  723. }
  724. else if (columnNum > 14 && j == columnNum - 14)
  725. {
  726. row1.CreateCell(j);
  727. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  728. }
  729. else
  730. {
  731. row1.CreateCell(j);
  732. }
  733. row1.GetCell(j).CellStyle = style;
  734. }
  735. row1 = sheet.CreateRow(PaintIndex);
  736. PaintIndex = PaintIndex + 1;
  737. //第二行添加型号
  738. for (int j = 0; j < columnNum - 3; j++)
  739. {
  740. if (j == 0)
  741. {
  742. row1.CreateCell(j);
  743. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_orispeccode"].ToString() + DataTable.Rows[i]["me_desc"].ToString() + DataTable.Rows[i]["pr_size"].ToString());
  744. }
  745. else if (columnNum > 14 && j == columnNum - 14)
  746. {
  747. row1.CreateCell(j);
  748. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  749. }
  750. else
  751. {
  752. row1.CreateCell(j);
  753. }
  754. row1.GetCell(j).CellStyle = style;
  755. }
  756. //添加列名
  757. row1 = sheet.CreateRow(PaintIndex);
  758. PaintIndex = PaintIndex + 1;
  759. for (int j = 4; j < columnNum; j++)
  760. {
  761. row1.CreateCell(j - 4);
  762. row1.Cells[j - 4].CellStyle = styleborder;
  763. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  764. {
  765. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  766. }
  767. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  768. {
  769. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  770. }
  771. else
  772. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  773. if ((NotShowColumn.Contains(DataTable.Columns[j].ColumnName.ToLower())) || (!ShowChcode && DataTable.Columns[j].ColumnName.ToLower().Contains("ch_code")))
  774. {
  775. sheet.SetColumnHidden(j - 4, true);
  776. }
  777. }
  778. row1 = sheet.CreateRow(PaintIndex);
  779. PaintIndex = PaintIndex + 1;
  780. }
  781. }
  782. //添加数据内容
  783. for (int j = 4; j < columnNum; j++)
  784. {
  785. string Data = DataTable.Rows[i][j].ToString();
  786. row1.CreateCell(j - 4);
  787. row1.Cells[j - 4].SetCellValue(Data);
  788. row1.GetCell(j - 4).CellStyle = styleborder;
  789. if (DataTable.Columns[j].ColumnName == "io_qty")
  790. {
  791. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  792. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  793. }
  794. if (DataTable.Columns[j].ColumnName == "rownum")
  795. {
  796. row1.Cells[j - 4].SetCellValue(i + 1);
  797. }
  798. }
  799. if (i == rowNum - 1)
  800. {
  801. row1 = sheet.CreateRow(PaintIndex);
  802. PaintIndex = PaintIndex + 1;
  803. for (int j = 0; j < columnNum - 4; j++)
  804. {
  805. row1.CreateCell(j);
  806. if (j == 0)
  807. {
  808. row1.Cells[j].SetCellValue("小计");
  809. row1.Cells[j].CellStyle = styleborder;
  810. }
  811. else if (j == 1)
  812. {
  813. row1.Cells[1].SetCellValue(PageNum + "片");
  814. }
  815. else if (j == 2)
  816. {
  817. row1.Cells[2].SetCellValue(sumCount);
  818. row1.Cells[j].CellStyle = styleborder;
  819. }
  820. else
  821. {
  822. row1.Cells[j].CellStyle = styleborder;
  823. }
  824. }
  825. //创建备注内容
  826. row1 = sheet.CreateRow(PaintIndex);
  827. //for (int j = 0; j < columnNum - 3; j++)
  828. //{
  829. // if (j == 0)
  830. // {
  831. // row1.CreateCell(j);
  832. // row1.Cells[j].SetCellValue("备注");
  833. // }
  834. // else if (j == 2)
  835. // {
  836. // row1.CreateCell(j);
  837. // row1.Cells[j].SetCellValue(totalCount);
  838. // }
  839. // else if (j == columnNum - 6)
  840. // {
  841. // row1.CreateCell(j);
  842. // row1.Cells[j].SetCellValue(rowNum);
  843. // }
  844. // else if (j > 5 && j == columnNum - 5)
  845. // {
  846. // row1.CreateCell(j);
  847. // row1.Cells[j].SetCellValue("片");
  848. // }
  849. // else if (columnNum > 5 && j == columnNum - 5)
  850. // {
  851. // row1.CreateCell(j);
  852. // row1.Cells[j].SetCellValue("片");
  853. // }
  854. // else
  855. // {
  856. // row1.CreateCell(j);
  857. // }
  858. // row1.Cells[j].CellStyle = style;
  859. //}
  860. sheet.SetRowBreak(PaintIndex);
  861. sheet.Footer.Center = "第&P页,共&N页";
  862. PaintIndex = PaintIndex + 1;
  863. PageNum = 1;
  864. }
  865. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  866. pib_outboxcode1.Add(BoxCode.ToString());
  867. }
  868. for (int i = 0; i < sheet.LastRowNum; i++)
  869. {
  870. if (i != 0)
  871. {
  872. sheet.AutoSizeColumn(i);
  873. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  874. }
  875. }
  876. break;
  877. case "BoxCode":
  878. string LastBoxCode = "";
  879. int PageNum1 = 0;
  880. for (int i = 0; i < rowNum; i++)
  881. {
  882. IRow row1 = sheet.CreateRow(PaintIndex);
  883. PaintIndex = PaintIndex + 1;
  884. row1.HeightInPoints = RowHeight;
  885. PageNum1 = PageNum1 + 1;
  886. //如果批号不相等的时候
  887. if (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["CH_PBCODE"].ToString())
  888. {
  889. BoxCode = BoxCode + 1;
  890. for (int j = 0; j < columnNum - 4; j++)
  891. {
  892. row1.CreateCell(j);
  893. if (j == 0)
  894. {
  895. row1.Cells[j].SetCellValue("小计");
  896. row1.Cells[j].CellStyle = styleborder;
  897. }
  898. else if (j == 1)
  899. {
  900. row1.Cells[1].SetCellValue(PageNum1 + "片");
  901. }
  902. else if (j == 2)
  903. {
  904. row1.Cells[2].SetCellValue(sumCount);
  905. row1.Cells[j].CellStyle = styleborder;
  906. }
  907. else
  908. {
  909. row1.Cells[j].CellStyle = styleborder;
  910. }
  911. }
  912. sumCount = 0;
  913. row1 = sheet.CreateRow(PaintIndex);
  914. sheet.SetRowBreak(PaintIndex - 1);
  915. sheet.Footer.Center = "第&P页,共&N页";
  916. PaintIndex = PaintIndex + 1;
  917. }
  918. //每次到了页数开始分页
  919. if (LastBoxCode == "" || (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["ch_pbcode"].ToString()) || i == rowNum - 1)
  920. {
  921. LastBoxCode = DataTable.Rows[i]["CH_PBCODE"].ToString();
  922. //第一行添加客户信息
  923. if (i != rowNum - 1)
  924. {
  925. for (int j = 0; j < columnNum - 3; j++)
  926. {
  927. if (j == 0)
  928. {
  929. row1.CreateCell(j);
  930. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString() + " " + DataTable.Rows[i]["ch_level"].ToString());
  931. }
  932. else if (columnNum > 14 && j == columnNum - 14)
  933. {
  934. row1.CreateCell(j);
  935. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  936. }
  937. else
  938. {
  939. row1.CreateCell(j);
  940. }
  941. row1.GetCell(j).CellStyle = style;
  942. }
  943. row1 = sheet.CreateRow(PaintIndex);
  944. PaintIndex = PaintIndex + 1;
  945. //第二行添加型号
  946. for (int j = 0; j < columnNum - 3; j++)
  947. {
  948. if (j == 0)
  949. {
  950. row1.CreateCell(j);
  951. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_orispeccode"].ToString() + DataTable.Rows[i]["me_desc"].ToString() + DataTable.Rows[i]["pr_size"].ToString());
  952. }
  953. else if (columnNum > 14 && j == columnNum - 14)
  954. {
  955. row1.CreateCell(j);
  956. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  957. }
  958. else
  959. {
  960. row1.CreateCell(j);
  961. }
  962. row1.GetCell(j).CellStyle = style;
  963. }
  964. //添加列名
  965. row1 = sheet.CreateRow(PaintIndex);
  966. PaintIndex = PaintIndex + 1;
  967. for (int j = 4; j < columnNum; j++)
  968. {
  969. //设定固定的列名
  970. row1.CreateCell(j - 4);
  971. row1.Cells[j - 4].CellStyle = styleborder;
  972. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  973. {
  974. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  975. }
  976. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  977. {
  978. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  979. }
  980. else
  981. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  982. if ((NotShowColumn.Contains(DataTable.Columns[j].ColumnName.ToLower())) || (!ShowChcode && DataTable.Columns[j].ColumnName.ToLower().Contains("ch_code")))
  983. {
  984. sheet.SetColumnHidden(j - 4, true);
  985. }
  986. }
  987. row1 = sheet.CreateRow(PaintIndex);
  988. PaintIndex = PaintIndex + 1;
  989. }
  990. }
  991. //添加数据内容
  992. for (int j = 4; j < columnNum; j++)
  993. {
  994. string Data = DataTable.Rows[i][j].ToString();
  995. row1.CreateCell(j - 4);
  996. row1.Cells[j - 4].SetCellValue(Data);
  997. row1.GetCell(j - 4).CellStyle = styleborder;
  998. if (DataTable.Columns[j].ColumnName == "io_qty")
  999. {
  1000. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  1001. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  1002. }
  1003. if (DataTable.Columns[j].ColumnName == "rownum")
  1004. {
  1005. row1.Cells[j - 4].SetCellValue(i + 1);
  1006. }
  1007. }
  1008. if (i == rowNum - 1)
  1009. {
  1010. row1 = sheet.CreateRow(PaintIndex);
  1011. PaintIndex = PaintIndex + 1;
  1012. for (int j = 0; j < columnNum - 4; j++)
  1013. {
  1014. row1.CreateCell(j);
  1015. if (j == 0)
  1016. {
  1017. row1.Cells[j].SetCellValue("小计");
  1018. row1.Cells[j].CellStyle = styleborder;
  1019. }
  1020. else if (j == 1)
  1021. {
  1022. row1.Cells[1].SetCellValue(PageNum1 + "片");
  1023. }
  1024. else if (j == 2)
  1025. {
  1026. row1.Cells[2].SetCellValue(sumCount);
  1027. row1.Cells[j].CellStyle = styleborder;
  1028. }
  1029. else
  1030. {
  1031. row1.Cells[j].CellStyle = styleborder;
  1032. }
  1033. }
  1034. row1 = sheet.CreateRow(PaintIndex);
  1035. //for (int j = 0; j < columnNum - 3; j++)
  1036. //{
  1037. // if (j == 0)
  1038. // {
  1039. // row1.CreateCell(j);
  1040. // row1.Cells[j].SetCellValue("备注");
  1041. // }
  1042. // else if (j == 2)
  1043. // {
  1044. // row1.CreateCell(j);
  1045. // row1.Cells[j].SetCellValue(totalCount);
  1046. // }
  1047. // else if (j == columnNum - 6)
  1048. // {
  1049. // row1.CreateCell(j);
  1050. // row1.Cells[j].SetCellValue(rowNum);
  1051. // }
  1052. // else if (j > 5 && j == columnNum - 5)
  1053. // {
  1054. // row1.CreateCell(j);
  1055. // row1.Cells[j].SetCellValue("片");
  1056. // }
  1057. // else if (columnNum > 5 && j == columnNum - 5)
  1058. // {
  1059. // row1.CreateCell(j);
  1060. // row1.Cells[j].SetCellValue("片");
  1061. // }
  1062. // else
  1063. // {
  1064. // row1.CreateCell(j);
  1065. // }
  1066. // row1.Cells[j].CellStyle = style;
  1067. //}
  1068. sheet.SetRowBreak(PaintIndex);
  1069. sheet.Footer.Center = "第&P页,共&N页";
  1070. PaintIndex = PaintIndex + 1;
  1071. PageNum1 = 1;
  1072. }
  1073. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  1074. pib_outboxcode1.Add(BoxCode.ToString());
  1075. }
  1076. for (int i = 0; i < sheet.LastRowNum; i++)
  1077. {
  1078. if (i != 0)
  1079. {
  1080. sheet.AutoSizeColumn(i);
  1081. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  1082. }
  1083. }
  1084. break;
  1085. default:
  1086. break;
  1087. }
  1088. dh.BatchInsert("update prodiobarcode set pib_outboxcode1=:pib_outboxcode1 where pib_inoutno='" + Inoutno + "' and pib_id=:pib_id", new string[] { "pib_outboxcode1", "pib_id" }, pib_outboxcode1.ToArray(), pib_id.ToArray());
  1089. //删除下载链接再重新插入
  1090. HttpHandler.GenDownLoadLinK(Inoutno);
  1091. //填充首页
  1092. sumCount = 0;
  1093. totalCount = 0;
  1094. PaintIndex = 1;
  1095. ISheet sheet2 = book.CreateSheet("首页");
  1096. row = sheet2.CreateRow(0);
  1097. row.CreateCell(0);
  1098. row.Cells[0].SetCellValue(" 深爱半导体股份有限公司芯片出货清单");
  1099. row.GetCell(0).CellStyle = style;
  1100. rowNum = FirstDT.Rows.Count;
  1101. //不需要显示的列移除
  1102. for (int i = FirstDT.Columns.Count - 1; i > 0; i--)
  1103. {
  1104. for (int j = 0; j < box.Length; j++)
  1105. {
  1106. if (box[j].Name == "FirstPage_WID" && !box[j].Checked)
  1107. {
  1108. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  1109. {
  1110. FirstDT.Columns.RemoveAt(i);
  1111. }
  1112. }
  1113. if (box[j].Name == "FirstPage_YIELD" && !box[j].Checked)
  1114. {
  1115. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_yeild"))
  1116. {
  1117. FirstDT.Columns.RemoveAt(i);
  1118. }
  1119. }
  1120. if (box[j].Name == "FirstPage_REMARK" && !box[j].Checked)
  1121. {
  1122. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  1123. {
  1124. FirstDT.Columns.RemoveAt(i);
  1125. }
  1126. }
  1127. }
  1128. }
  1129. columnNum = FirstDT.Columns.Count;
  1130. for (int i = 0; i < rowNum; i++)
  1131. {
  1132. IRow row1 = sheet2.CreateRow(PaintIndex);
  1133. PaintIndex = PaintIndex + 1;
  1134. row1.HeightInPoints = RowHeight;
  1135. //只需要绘制一行
  1136. if (i == 0)
  1137. {
  1138. for (int j = 0; j < columnNum - 3; j++)
  1139. {
  1140. if (j == 0)
  1141. {
  1142. row1.CreateCell(j);
  1143. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["pi_title"].ToString());
  1144. }
  1145. else if (j > 5 && j == columnNum - 5)
  1146. {
  1147. row1.CreateCell(j);
  1148. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  1149. }
  1150. else if (columnNum > 5 && j == columnNum - 5)
  1151. {
  1152. row1.CreateCell(j);
  1153. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  1154. }
  1155. else
  1156. {
  1157. row1.CreateCell(j);
  1158. }
  1159. row1.GetCell(j).CellStyle = style;
  1160. }
  1161. row1 = sheet2.CreateRow(PaintIndex);
  1162. PaintIndex = PaintIndex + 1;
  1163. //第二行添加型号
  1164. for (int j = 0; j < columnNum - 3; j++)
  1165. {
  1166. if (j == 0)
  1167. {
  1168. row1.CreateCell(j);
  1169. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["pr_orispeccode"].ToString());
  1170. }
  1171. else if (j > 5 && j == columnNum - 5)
  1172. {
  1173. row1.CreateCell(j);
  1174. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  1175. }
  1176. else if (columnNum > 5 && j == columnNum - 5)
  1177. {
  1178. row1.CreateCell(j);
  1179. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  1180. }
  1181. else
  1182. {
  1183. row1.CreateCell(j);
  1184. }
  1185. row1.GetCell(j).CellStyle = style;
  1186. }
  1187. row1 = sheet2.CreateRow(PaintIndex);
  1188. PaintIndex = PaintIndex + 1;
  1189. //添加列名
  1190. for (int j = 4; j < columnNum; j++)
  1191. {
  1192. row1.CreateCell(j - 4);
  1193. row1.Cells[j - 4].CellStyle = styleborder;
  1194. row1.Cells[j - 4].SetCellValue(FirstDT.Columns[j].Caption);
  1195. }
  1196. row1 = sheet2.CreateRow(PaintIndex);
  1197. PaintIndex = PaintIndex + 1;
  1198. }
  1199. //添加数据内容
  1200. for (int j = 4; j < columnNum; j++)
  1201. {
  1202. string Data = FirstDT.Rows[i][j].ToString();
  1203. row1.CreateCell(j - 4);
  1204. row1.Cells[j - 4].SetCellValue(Data);
  1205. row1.GetCell(j - 4).CellStyle = styleborder;
  1206. if (FirstDT.Columns[j].ColumnName == "num")
  1207. {
  1208. sumCount += int.Parse(Data);
  1209. }
  1210. if (FirstDT.Columns[j].ColumnName == "io_qty")
  1211. {
  1212. totalCount += int.Parse(Data);
  1213. }
  1214. }
  1215. //添加总计行
  1216. if (i == rowNum - 1)
  1217. {
  1218. row1 = sheet2.CreateRow(PaintIndex);
  1219. PaintIndex = PaintIndex + 1;
  1220. for (int j = 0; j < columnNum - 4; j++)
  1221. {
  1222. if (j == 0)
  1223. {
  1224. row1.CreateCell(j);
  1225. row1.Cells[j].CellStyle = styleborder;
  1226. row1.Cells[j].SetCellValue("总计");
  1227. }
  1228. else if (j == columnNum - 6)
  1229. {
  1230. row1.CreateCell(j);
  1231. row1.Cells[j].CellStyle = styleborder;
  1232. row1.Cells[j].SetCellValue(sumCount);
  1233. }
  1234. else if (j == columnNum - 5)
  1235. {
  1236. row1.CreateCell(j);
  1237. row1.Cells[j].CellStyle = styleborder;
  1238. row1.Cells[j].SetCellValue(totalCount);
  1239. }
  1240. else
  1241. {
  1242. row1.CreateCell(j);
  1243. row1.Cells[j].CellStyle = styleborder;
  1244. }
  1245. }
  1246. }
  1247. }
  1248. for (int i = 0; i < sheet2.LastRowNum; i++)
  1249. {
  1250. if (i != 0)
  1251. {
  1252. sheet2.AutoSizeColumn(i);
  1253. sheet2.SetColumnWidth(i, sheet2.GetColumnWidth(i) + 1000);
  1254. }
  1255. }
  1256. //将book的内容写入内存流中返回
  1257. book.Write(ms);
  1258. return ms;
  1259. }
  1260. /// <summary>
  1261. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  1262. /// </summary>
  1263. /// <param name="DataTable"></param>
  1264. /// <returns></returns>
  1265. public MemoryStream DataTableToExcel2(DataTable FirstDT, DataTable DataTable, string Type, string Inoutno, int PageSize, List<CheckBox> conditionbox)
  1266. {
  1267. //转换为序列
  1268. CheckBox[] box = conditionbox.ToArray();
  1269. //创建内存流
  1270. MemoryStream ms = new MemoryStream();
  1271. //创建一个Book,相当于一个Excel文件
  1272. HSSFWorkbook book = new HSSFWorkbook();
  1273. //Excel中的Sheet
  1274. ISheet sheet = book.CreateSheet("分页");
  1275. sheet.SetMargin(MarginType.TopMargin, 0.4);
  1276. sheet.SetMargin(MarginType.BottomMargin, 0.4);
  1277. sheet.SetMargin(MarginType.LeftMargin, 0.4);
  1278. sheet.SetMargin(MarginType.RightMargin, 0.4);
  1279. //芯片号需要作为更新盒号的条件
  1280. HSSFFont ffont = (HSSFFont)book.CreateFont();
  1281. ffont.FontName = "宋体";
  1282. bool ShowChcode = true;
  1283. //更新箱号
  1284. List<string> pib_id = new List<string>();
  1285. //系统打印箱号
  1286. List<string> pib_outboxcode1 = new List<string>();
  1287. int BoxCode = 1;
  1288. ICellStyle style = book.CreateCellStyle();
  1289. style.VerticalAlignment = VerticalAlignment.Center;
  1290. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  1291. style.SetFont(ffont);
  1292. ICellStyle styleborder = book.CreateCellStyle();
  1293. styleborder.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1294. styleborder.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1295. styleborder.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1296. styleborder.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1297. styleborder.VerticalAlignment = VerticalAlignment.Center;
  1298. styleborder.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  1299. styleborder.SetFont(ffont);
  1300. string pi_inoutno = "";
  1301. List<string> NotShowColumn = new List<string>();
  1302. NotShowColumn.Add("ch_level");
  1303. NotShowColumn.Add("pib_id");
  1304. NotShowColumn.Add("chw_itemname1");
  1305. NotShowColumn.Add("chw_itemname2");
  1306. NotShowColumn.Add("pd_ordercode");
  1307. NotShowColumn.Add("pr_size");
  1308. NotShowColumn.Add("me_desc");
  1309. NotShowColumn.Add("pr_orispeccode1");
  1310. NotShowColumn.Add("pi_title");
  1311. //展示的内容列
  1312. int ShowColumnsCount = 0;
  1313. //设置列的宽度,根据首行的列的内容的长度来设置
  1314. for (int i = DataTable.Columns.Count - 1; i > 0; i--)
  1315. {
  1316. //统计显示的列数
  1317. if (!NotShowColumn.Contains(DataTable.Columns[i].ColumnName.ToLower()))
  1318. {
  1319. ShowColumnsCount = ShowColumnsCount + 1;
  1320. }
  1321. for (int j = 0; j < box.Length; j++)
  1322. {
  1323. if (box[j].Name.ToLower() == "ch_bluefilm" && !box[j].Checked)
  1324. {
  1325. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_bluefilm"))
  1326. {
  1327. DataTable.Columns.RemoveAt(i);
  1328. break;
  1329. }
  1330. }
  1331. if (box[j].Name.ToLower() == "ch_code" && !box[j].Checked)
  1332. {
  1333. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_code"))
  1334. {
  1335. ShowChcode = true;
  1336. break;
  1337. }
  1338. }
  1339. if (box[j].Name.ToLower() == "ch_splitbatch" && !box[j].Checked)
  1340. {
  1341. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_splitbatch"))
  1342. {
  1343. DataTable.Columns.RemoveAt(i);
  1344. break;
  1345. }
  1346. }
  1347. if (box[j].Name.ToLower() == "ch_waterid" && !box[j].Checked)
  1348. {
  1349. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  1350. {
  1351. DataTable.Columns.RemoveAt(i);
  1352. break;
  1353. }
  1354. }
  1355. if (box[j].Name.ToLower() == "ch_pbcode" && !box[j].Checked)
  1356. {
  1357. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_pbcode"))
  1358. {
  1359. DataTable.Columns.RemoveAt(i);
  1360. break;
  1361. }
  1362. }
  1363. if (box[j].Name.ToLower() == "ch_remark" && !box[j].Checked)
  1364. {
  1365. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  1366. {
  1367. DataTable.Columns.RemoveAt(i);
  1368. break;
  1369. }
  1370. }
  1371. }
  1372. }
  1373. //获取行数量和列数量
  1374. int rowNum = DataTable.Rows.Count;
  1375. int columnNum = DataTable.Columns.Count;
  1376. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  1377. //ffont.FontHeight = 10;
  1378. //开始绘制的Index
  1379. int PaintIndex = 1;
  1380. int sumCount = 0;
  1381. int totalCount = 0;
  1382. //Title的字体
  1383. HSSFFont titlefont = (HSSFFont)book.CreateFont();
  1384. titlefont.FontName = "宋体";
  1385. titlefont.FontHeight = 270;
  1386. titlefont.Boldweight = (short)FontBoldWeight.Bold;
  1387. HSSFFont ColumnTitle = (HSSFFont)book.CreateFont();
  1388. ColumnTitle.FontName = "宋体";
  1389. ColumnTitle.Boldweight = (short)FontBoldWeight.Bold;
  1390. ICellStyle ColumnTitleStyle = book.CreateCellStyle();
  1391. ColumnTitleStyle.SetFont(ColumnTitle);
  1392. ColumnTitleStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1393. ColumnTitleStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1394. ColumnTitleStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1395. ColumnTitleStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1396. ICellStyle TitleStyle = book.CreateCellStyle();
  1397. TitleStyle.SetFont(titlefont);
  1398. switch (Type)
  1399. {
  1400. case "FixRow":
  1401. //清理系统取出来的数据
  1402. BaseUtil.CleanDataTableData(FirstDT);
  1403. //首页参数拼接
  1404. string First_OrderCode = "";
  1405. string First_Prspec = "";
  1406. string First_Batch = "";
  1407. int NumIndex = 0;
  1408. ArrayList<string> First_WID = new ArrayList<string>();
  1409. for (int i = 0; i < rowNum; i++)
  1410. {
  1411. IRow row1 = null;
  1412. if (PaintIndex != 1)
  1413. {
  1414. row1 = sheet.CreateRow(PaintIndex);
  1415. PaintIndex = PaintIndex + 1;
  1416. row1.HeightInPoints = RowHeight;
  1417. }
  1418. //不包含的订单号
  1419. if (DataTable.Columns.Contains("pd_ordercode") && !First_OrderCode.Contains(DataTable.Rows[i]["pd_ordercode"].ToString()))
  1420. {
  1421. First_OrderCode += DataTable.Rows[i]["pd_ordercode"].ToString() + " ";
  1422. }
  1423. //不包含的物料型号
  1424. if (DataTable.Columns.Contains("pr_orispeccode1") && !First_Prspec.Contains(DataTable.Rows[i]["pr_orispeccode1"].ToString()))
  1425. {
  1426. First_Prspec += DataTable.Rows[i]["pr_orispeccode1"].ToString() + " ";
  1427. }
  1428. //不包含扩撒批号
  1429. if (DataTable.Columns.Contains("ch_splitbatch") && !First_Batch.Contains(DataTable.Rows[i]["ch_splitbatch"].ToString()))
  1430. {
  1431. First_Batch += DataTable.Rows[i]["ch_splitbatch"].ToString() + " ";
  1432. }
  1433. //不包含Wafer_id
  1434. if (DataTable.Columns.Contains("Wafer_ID") && !First_WID.Contains(DataTable.Rows[i]["Wafer_ID"].ToString()))
  1435. {
  1436. First_WID.Add(DataTable.Rows[i]["Wafer_ID"].ToString());
  1437. }
  1438. if (i / PageSize >= 1 && i % PageSize == 0)
  1439. {
  1440. DataRow dr = FirstDT.NewRow();
  1441. dr["pr_orispeccode"] = DataTable.Rows[i]["pr_orispeccode"].ToString();
  1442. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  1443. pi_inoutno = DataTable.Rows[i]["pi_inoutno"].ToString();
  1444. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  1445. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  1446. dr["pd_ordercode"] = First_OrderCode;
  1447. dr["pr_orispeccode1"] = First_Prspec;
  1448. dr["ch_splitbatch"] = First_Batch;
  1449. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  1450. dr["num"] = PageSize;
  1451. dr["io_qty"] = sumCount;
  1452. FirstDT.Rows.Add(dr);
  1453. First_OrderCode = "";
  1454. First_Prspec = "";
  1455. First_Batch = "";
  1456. First_WID.Clear();
  1457. BoxCode = BoxCode + 1;
  1458. for (int j = 0; j < columnNum - 4; j++)
  1459. {
  1460. row1.CreateCell(j);
  1461. if (j == 0)
  1462. {
  1463. row1.Cells[j].SetCellValue("小计");
  1464. }
  1465. else if (j == 1)
  1466. {
  1467. row1.Cells[1].SetCellValue((i % PageSize == 0 ? PageSize : i) + "片");
  1468. row1.Cells[1].CellStyle = ColumnTitleStyle;
  1469. }
  1470. else if (j == 2)
  1471. {
  1472. row1.Cells[2].SetCellValue(sumCount);
  1473. }
  1474. row1.Cells[j].CellStyle = styleborder;
  1475. }
  1476. sumCount = 0;
  1477. row1 = sheet.CreateRow(PaintIndex);
  1478. sheet.SetRowBreak(PaintIndex - 1);
  1479. sheet.Footer.Center = "第&P页,共&N页";
  1480. PaintIndex = PaintIndex + 1;
  1481. }
  1482. //每次到了页数开始分页
  1483. if (i % PageSize == 0 || i == rowNum - 1)
  1484. {
  1485. //第一行添加客户信息 rownum只有一行的情
  1486. if (i != rowNum - 1 || rowNum == 1)
  1487. {
  1488. //抬头设置
  1489. row1 = sheet.CreateRow(PaintIndex);
  1490. PaintIndex = PaintIndex + 1;
  1491. row1.CreateCell(0).SetCellValue(" 深爱半导体股份有限公司芯片出货清单");
  1492. row1.GetCell(0).CellStyle = TitleStyle;
  1493. row1 = sheet.CreateRow(PaintIndex);
  1494. PaintIndex = PaintIndex + 1;
  1495. //特殊客户添加一列空行
  1496. for (int j = 0; j < columnNum - 3; j++)
  1497. {
  1498. if (j == 0)
  1499. {
  1500. row1.CreateCell(j);
  1501. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString() + " " + DataTable.Rows[i]["ch_level"].ToString());
  1502. }
  1503. else if (columnNum > 14 && j == columnNum - 14)
  1504. {
  1505. row1.CreateCell(j);
  1506. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  1507. }
  1508. else
  1509. {
  1510. row1.CreateCell(j);
  1511. }
  1512. row1.GetCell(j).CellStyle = style;
  1513. }
  1514. row1 = sheet.CreateRow(PaintIndex);
  1515. PaintIndex = PaintIndex + 1;
  1516. //第二行添加型号
  1517. for (int j = 0; j < columnNum - 3; j++)
  1518. {
  1519. if (j == 0)
  1520. {
  1521. row1.CreateCell(j);
  1522. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_orispeccode"].ToString() + DataTable.Rows[i]["me_desc"].ToString() + " " + DataTable.Rows[i]["pr_size"].ToString() + " 销售部门:市场部");
  1523. }
  1524. else if (columnNum > 14 && j == columnNum - 14)
  1525. {
  1526. row1.CreateCell(j);
  1527. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  1528. }
  1529. else
  1530. {
  1531. row1.CreateCell(j);
  1532. }
  1533. row1.GetCell(j).CellStyle = style;
  1534. }
  1535. //特殊客户添加一列空行
  1536. row1 = sheet.CreateRow(PaintIndex);
  1537. PaintIndex = PaintIndex + 1;
  1538. //添加列名
  1539. row1 = sheet.CreateRow(PaintIndex);
  1540. PaintIndex = PaintIndex + 1;
  1541. //计数列所在的索引
  1542. for (int j = 4; j < columnNum; j++)
  1543. {
  1544. row1.CreateCell(j - 4);
  1545. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1546. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  1547. {
  1548. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  1549. }
  1550. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  1551. {
  1552. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  1553. }
  1554. else
  1555. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  1556. if (DataTable.Columns[j].ColumnName.ToString() == "io_qty")
  1557. {
  1558. NumIndex = j;
  1559. }
  1560. //如果chw_itemname1的值为空,则值为100和0,其中一列不显示,不显示
  1561. if ((NotShowColumn.Contains(DataTable.Columns[j].ColumnName.ToLower())) || (!ShowChcode && DataTable.Columns[j].ColumnName.ToLower().Contains("ch_code")))
  1562. {
  1563. sheet.SetColumnHidden(j - 4, true);
  1564. }
  1565. }
  1566. row1 = sheet.CreateRow(PaintIndex);
  1567. PaintIndex = PaintIndex + 1;
  1568. }
  1569. }
  1570. //添加数据内容
  1571. for (int j = 4; j < columnNum; j++)
  1572. {
  1573. string Data = DataTable.Rows[i][j].ToString();
  1574. row1.CreateCell(j - 4);
  1575. row1.Cells[j - 4].SetCellValue(Data);
  1576. row1.GetCell(j - 4).CellStyle = styleborder;
  1577. if (DataTable.Columns[j].ColumnName == "io_qty")
  1578. {
  1579. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  1580. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  1581. }
  1582. if (DataTable.Columns[j].ColumnName == "rownum")
  1583. {
  1584. row1.Cells[j - 4].SetCellValue(i + 1);
  1585. }
  1586. }
  1587. //固定行号分组的时候自动拼接新的DataTable
  1588. if (i == rowNum - 1)
  1589. {
  1590. DataRow dr = FirstDT.NewRow();
  1591. dr["pr_orispeccode"] = DataTable.Rows[i]["pr_orispeccode"].ToString();
  1592. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  1593. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  1594. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  1595. dr["pd_ordercode"] = First_OrderCode;
  1596. dr["pr_orispeccode1"] = First_Prspec;
  1597. dr["ch_splitbatch"] = First_Batch;
  1598. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  1599. dr["num"] = (i % PageSize) + 1;
  1600. dr["io_qty"] = sumCount;
  1601. FirstDT.Rows.Add(dr);
  1602. row1 = sheet.CreateRow(PaintIndex);
  1603. PaintIndex = PaintIndex + 1;
  1604. for (int j = 0; j < columnNum - 4; j++)
  1605. {
  1606. row1.CreateCell(j);
  1607. if (j == 0)
  1608. {
  1609. row1.Cells[j].SetCellValue("小计");
  1610. }
  1611. else if (j == 1)
  1612. {
  1613. row1.Cells[1].SetCellValue((i % PageSize == 0 ? PageSize : (i % PageSize + 1)) + "片");
  1614. row1.Cells[1].CellStyle = ColumnTitleStyle;
  1615. }
  1616. else if (j == 2)
  1617. {
  1618. row1.Cells[2].SetCellValue(sumCount);
  1619. }
  1620. row1.Cells[j].CellStyle = styleborder;
  1621. }
  1622. row1 = sheet.CreateRow(PaintIndex);
  1623. sheet.SetRowBreak(PaintIndex);
  1624. sheet.Footer.Center = "第&P页,共&N页";
  1625. PaintIndex = PaintIndex + 1;
  1626. }
  1627. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  1628. pib_outboxcode1.Add(BoxCode.ToString());
  1629. }
  1630. for (int i = 0; i < sheet.LastRowNum; i++)
  1631. {
  1632. if (i != 0)
  1633. {
  1634. sheet.AutoSizeColumn(i);
  1635. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  1636. }
  1637. }
  1638. break;
  1639. case "BatchCode":
  1640. string LastBatchCode = "";
  1641. int PageNum = 0;
  1642. for (int i = 0; i < rowNum; i++)
  1643. {
  1644. IRow row1 = sheet.CreateRow(PaintIndex);
  1645. PaintIndex = PaintIndex + 1;
  1646. row1.HeightInPoints = RowHeight;
  1647. //如果批号不相等的时候
  1648. PageNum = PageNum + 1;
  1649. if (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString())
  1650. {
  1651. BoxCode = BoxCode + 1;
  1652. for (int j = 0; j < columnNum - 4; j++)
  1653. {
  1654. row1.CreateCell(j);
  1655. if (j == 0)
  1656. {
  1657. row1.Cells[j].SetCellValue("小计");
  1658. row1.Cells[j].CellStyle = styleborder;
  1659. }
  1660. else if (j == 1)
  1661. {
  1662. row1.Cells[1].SetCellValue(PageNum - 1 + "片");
  1663. row1.Cells[1].CellStyle = ColumnTitleStyle;
  1664. }
  1665. else if (j == 2)
  1666. {
  1667. row1.Cells[2].SetCellValue(sumCount);
  1668. row1.Cells[j].CellStyle = styleborder;
  1669. }
  1670. else
  1671. {
  1672. row1.Cells[j].CellStyle = styleborder;
  1673. }
  1674. }
  1675. sumCount = 0;
  1676. row1 = sheet.CreateRow(PaintIndex);
  1677. sheet.SetRowBreak(PaintIndex - 1);
  1678. sheet.Footer.Center = "第&P页,共&N页";
  1679. PaintIndex = PaintIndex + 1;
  1680. PageNum = 1;
  1681. }
  1682. //每次到了页数开始分页
  1683. if (LastBatchCode == "" || (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString()))
  1684. {
  1685. LastBatchCode = DataTable.Rows[i]["ch_splitbatch"].ToString();
  1686. //第一行添加客户信息
  1687. row1 = sheet.CreateRow(PaintIndex);
  1688. PaintIndex = PaintIndex + 1;
  1689. row1.CreateCell(0).SetCellValue(" 深爱半导体股份有限公司芯片出货清单");
  1690. row1.GetCell(0).CellStyle = TitleStyle;
  1691. row1 = sheet.CreateRow(PaintIndex);
  1692. PaintIndex = PaintIndex + 1;
  1693. if (i != rowNum - 1)
  1694. {
  1695. for (int j = 0; j < columnNum - 3; j++)
  1696. {
  1697. if (j == 0)
  1698. {
  1699. row1.CreateCell(j);
  1700. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString() + " " + DataTable.Rows[i]["ch_level"].ToString());
  1701. }
  1702. else if (columnNum > 14 && j == columnNum - 14)
  1703. {
  1704. row1.CreateCell(j);
  1705. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  1706. }
  1707. else
  1708. {
  1709. row1.CreateCell(j);
  1710. }
  1711. row1.GetCell(j).CellStyle = style;
  1712. }
  1713. row1 = sheet.CreateRow(PaintIndex);
  1714. PaintIndex = PaintIndex + 1;
  1715. //第二行添加型号
  1716. for (int j = 0; j < columnNum - 3; j++)
  1717. {
  1718. if (j == 0)
  1719. {
  1720. row1.CreateCell(j);
  1721. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_orispeccode"].ToString() + DataTable.Rows[i]["me_desc"].ToString() + " " + DataTable.Rows[i]["pr_size"].ToString() + " 销售部门:市场部");
  1722. }
  1723. else if (columnNum > 14 && j == columnNum - 14)
  1724. {
  1725. row1.CreateCell(j);
  1726. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  1727. }
  1728. else
  1729. {
  1730. row1.CreateCell(j);
  1731. }
  1732. row1.GetCell(j).CellStyle = style;
  1733. }
  1734. //添加列名
  1735. row1 = sheet.CreateRow(PaintIndex);
  1736. PaintIndex = PaintIndex + 1;
  1737. for (int j = 4; j < columnNum; j++)
  1738. {
  1739. row1.CreateCell(j - 4);
  1740. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1741. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  1742. {
  1743. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  1744. }
  1745. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  1746. {
  1747. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  1748. }
  1749. else
  1750. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  1751. if ((NotShowColumn.Contains(DataTable.Columns[j].ColumnName.ToLower())) || (!ShowChcode && DataTable.Columns[j].ColumnName.ToLower().Contains("ch_code")))
  1752. {
  1753. sheet.SetColumnHidden(j - 4, true);
  1754. }
  1755. }
  1756. row1 = sheet.CreateRow(PaintIndex);
  1757. PaintIndex = PaintIndex + 1;
  1758. }
  1759. }
  1760. //添加数据内容
  1761. for (int j = 4; j < columnNum; j++)
  1762. {
  1763. string Data = DataTable.Rows[i][j].ToString();
  1764. row1.CreateCell(j - 4);
  1765. row1.Cells[j - 4].SetCellValue(Data);
  1766. row1.GetCell(j - 4).CellStyle = styleborder;
  1767. if (DataTable.Columns[j].ColumnName == "io_qty")
  1768. {
  1769. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  1770. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  1771. }
  1772. if (DataTable.Columns[j].ColumnName == "rownum")
  1773. {
  1774. row1.Cells[j - 4].SetCellValue(i + 1);
  1775. }
  1776. }
  1777. if (i == rowNum - 1)
  1778. {
  1779. row1 = sheet.CreateRow(PaintIndex);
  1780. PaintIndex = PaintIndex + 1;
  1781. for (int j = 0; j < columnNum - 4; j++)
  1782. {
  1783. row1.CreateCell(j);
  1784. if (j == 0)
  1785. {
  1786. row1.Cells[j].SetCellValue("小计");
  1787. row1.Cells[j].CellStyle = styleborder;
  1788. }
  1789. else if (j == 1)
  1790. {
  1791. row1.Cells[1].SetCellValue(PageNum + "片");
  1792. row1.Cells[1].CellStyle = ColumnTitleStyle;
  1793. }
  1794. else if (j == 2)
  1795. {
  1796. row1.Cells[2].SetCellValue(sumCount);
  1797. row1.Cells[j].CellStyle = styleborder;
  1798. }
  1799. else
  1800. {
  1801. row1.Cells[j].CellStyle = styleborder;
  1802. }
  1803. }
  1804. //创建备注内容
  1805. row1 = sheet.CreateRow(PaintIndex);
  1806. sheet.SetRowBreak(PaintIndex);
  1807. sheet.Footer.Center = "第&P页,共&N页";
  1808. PaintIndex = PaintIndex + 1;
  1809. PageNum = 1;
  1810. }
  1811. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  1812. pib_outboxcode1.Add(BoxCode.ToString());
  1813. }
  1814. for (int i = 0; i < sheet.LastRowNum; i++)
  1815. {
  1816. if (i != 0)
  1817. {
  1818. sheet.AutoSizeColumn(i);
  1819. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  1820. }
  1821. }
  1822. break;
  1823. case "BoxCode":
  1824. string LastBoxCode = "";
  1825. int PageNum1 = 0;
  1826. for (int i = 0; i < rowNum; i++)
  1827. {
  1828. IRow row1 = sheet.CreateRow(PaintIndex);
  1829. PaintIndex = PaintIndex + 1;
  1830. row1.HeightInPoints = RowHeight;
  1831. PageNum1 = PageNum1 + 1;
  1832. //如果批号不相等的时候
  1833. if (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["CH_PBCODE"].ToString())
  1834. {
  1835. BoxCode = BoxCode + 1;
  1836. for (int j = 0; j < columnNum - 4; j++)
  1837. {
  1838. row1.CreateCell(j);
  1839. if (j == 0)
  1840. {
  1841. row1.Cells[j].SetCellValue("小计");
  1842. row1.Cells[j].CellStyle = styleborder;
  1843. }
  1844. else if (j == 1)
  1845. {
  1846. row1.Cells[1].SetCellValue(PageNum1 - 1 + "片");
  1847. row1.Cells[1].CellStyle = ColumnTitleStyle;
  1848. }
  1849. else if (j == 2)
  1850. {
  1851. row1.Cells[2].SetCellValue(sumCount);
  1852. row1.Cells[j].CellStyle = styleborder;
  1853. }
  1854. else
  1855. {
  1856. row1.Cells[j].CellStyle = styleborder;
  1857. }
  1858. }
  1859. sumCount = 0;
  1860. row1 = sheet.CreateRow(PaintIndex);
  1861. sheet.SetRowBreak(PaintIndex - 1);
  1862. sheet.Footer.Center = "第&P页,共&N页";
  1863. PaintIndex = PaintIndex + 1;
  1864. PageNum1 = 1;
  1865. }
  1866. //每次到了页数开始分页
  1867. if (LastBoxCode == "" || (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["ch_pbcode"].ToString()))
  1868. {
  1869. LastBoxCode = DataTable.Rows[i]["CH_PBCODE"].ToString();
  1870. row1 = sheet.CreateRow(PaintIndex);
  1871. PaintIndex = PaintIndex + 1;
  1872. row1.CreateCell(0).SetCellValue(" 深爱半导体股份有限公司芯片出货清单");
  1873. row1.GetCell(0).CellStyle = TitleStyle;
  1874. row1 = sheet.CreateRow(PaintIndex);
  1875. PaintIndex = PaintIndex + 1;
  1876. //第一行添加客户信息
  1877. if (i != rowNum - 1)
  1878. {
  1879. for (int j = 0; j < columnNum - 3; j++)
  1880. {
  1881. if (j == 0)
  1882. {
  1883. row1.CreateCell(j);
  1884. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_title"].ToString() + " " + DataTable.Rows[i]["ch_level"].ToString());
  1885. }
  1886. else if (columnNum > 14 && j == columnNum - 14)
  1887. {
  1888. row1.CreateCell(j);
  1889. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  1890. }
  1891. else
  1892. {
  1893. row1.CreateCell(j);
  1894. }
  1895. row1.GetCell(j).CellStyle = style;
  1896. }
  1897. row1 = sheet.CreateRow(PaintIndex);
  1898. PaintIndex = PaintIndex + 1;
  1899. //第二行添加型号
  1900. for (int j = 0; j < columnNum - 3; j++)
  1901. {
  1902. if (j == 0)
  1903. {
  1904. row1.CreateCell(j);
  1905. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pr_orispeccode"].ToString() + DataTable.Rows[i]["me_desc"].ToString() + " " + DataTable.Rows[i]["pr_size"].ToString() + " 销售部门:市场部");
  1906. }
  1907. else if (columnNum > 14 && j == columnNum - 14)
  1908. {
  1909. row1.CreateCell(j);
  1910. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  1911. }
  1912. else
  1913. {
  1914. row1.CreateCell(j);
  1915. }
  1916. row1.GetCell(j).CellStyle = style;
  1917. }
  1918. //添加列名
  1919. row1 = sheet.CreateRow(PaintIndex);
  1920. PaintIndex = PaintIndex + 1;
  1921. for (int j = 4; j < columnNum; j++)
  1922. {
  1923. //设定固定的列名
  1924. row1.CreateCell(j - 4);
  1925. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1926. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  1927. {
  1928. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  1929. }
  1930. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  1931. {
  1932. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  1933. }
  1934. else
  1935. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  1936. if ((NotShowColumn.Contains(DataTable.Columns[j].ColumnName.ToLower())) || (!ShowChcode && DataTable.Columns[j].ColumnName.ToLower().Contains("ch_code")))
  1937. {
  1938. sheet.SetColumnHidden(j - 4, true);
  1939. }
  1940. }
  1941. row1 = sheet.CreateRow(PaintIndex);
  1942. PaintIndex = PaintIndex + 1;
  1943. }
  1944. }
  1945. //添加数据内容
  1946. for (int j = 4; j < columnNum; j++)
  1947. {
  1948. string Data = DataTable.Rows[i][j].ToString();
  1949. row1.CreateCell(j - 4);
  1950. row1.Cells[j - 4].SetCellValue(Data);
  1951. row1.GetCell(j - 4).CellStyle = styleborder;
  1952. if (DataTable.Columns[j].ColumnName == "io_qty")
  1953. {
  1954. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  1955. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  1956. }
  1957. if (DataTable.Columns[j].ColumnName == "rownum")
  1958. {
  1959. row1.Cells[j - 4].SetCellValue(i + 1);
  1960. }
  1961. }
  1962. if (i == rowNum - 1)
  1963. {
  1964. row1 = sheet.CreateRow(PaintIndex);
  1965. PaintIndex = PaintIndex + 1;
  1966. for (int j = 0; j < columnNum - 4; j++)
  1967. {
  1968. row1.CreateCell(j);
  1969. if (j == 0)
  1970. {
  1971. row1.Cells[j].SetCellValue("小计");
  1972. row1.Cells[j].CellStyle = styleborder;
  1973. }
  1974. else if (j == 1)
  1975. {
  1976. row1.Cells[1].SetCellValue(PageNum1 + "片");
  1977. row1.Cells[1].CellStyle = ColumnTitleStyle;
  1978. }
  1979. else if (j == 2)
  1980. {
  1981. row1.Cells[2].SetCellValue(sumCount);
  1982. row1.Cells[j].CellStyle = styleborder;
  1983. }
  1984. else
  1985. {
  1986. row1.Cells[j].CellStyle = styleborder;
  1987. }
  1988. }
  1989. row1 = sheet.CreateRow(PaintIndex);
  1990. sheet.SetRowBreak(PaintIndex);
  1991. sheet.Footer.Center = "第&P页,共&N页";
  1992. PaintIndex = PaintIndex + 1;
  1993. PageNum1 = 1;
  1994. }
  1995. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  1996. pib_outboxcode1.Add(BoxCode.ToString());
  1997. }
  1998. for (int i = 0; i < sheet.LastRowNum; i++)
  1999. {
  2000. if (i != 0)
  2001. {
  2002. sheet.AutoSizeColumn(i);
  2003. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  2004. }
  2005. }
  2006. break;
  2007. default:
  2008. break;
  2009. }
  2010. dh.BatchInsert("update prodiobarcode set pib_outboxcode1=:pib_outboxcode1 where pib_inoutno='" + Inoutno + "' and pib_id=:pib_id", new string[] { "pib_outboxcode1", "pib_id" }, pib_outboxcode1.ToArray(), pib_id.ToArray());
  2011. //删除下载链接再重新插入
  2012. HttpHandler.GenDownLoadLinK(Inoutno);
  2013. //填充首页
  2014. sumCount = 0;
  2015. totalCount = 0;
  2016. PaintIndex = 1;
  2017. ISheet sheet2 = book.CreateSheet("首页");
  2018. IRow row = sheet.CreateRow(0);
  2019. row = sheet2.CreateRow(0);
  2020. row.CreateCell(0);
  2021. row.Cells[0].SetCellValue(" 深爱半导体股份有限公司芯片出货清单");
  2022. row.GetCell(0).CellStyle = style;
  2023. rowNum = FirstDT.Rows.Count;
  2024. //不需要显示的列移除
  2025. for (int i = FirstDT.Columns.Count - 1; i > 0; i--)
  2026. {
  2027. for (int j = 0; j < box.Length; j++)
  2028. {
  2029. if (box[j].Name == "FirstPage_WID" && !box[j].Checked)
  2030. {
  2031. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  2032. {
  2033. FirstDT.Columns.RemoveAt(i);
  2034. }
  2035. }
  2036. if (box[j].Name == "FirstPage_YIELD" && !box[j].Checked)
  2037. {
  2038. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_yeild"))
  2039. {
  2040. FirstDT.Columns.RemoveAt(i);
  2041. }
  2042. }
  2043. if (box[j].Name == "FirstPage_REMARK" && !box[j].Checked)
  2044. {
  2045. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  2046. {
  2047. FirstDT.Columns.RemoveAt(i);
  2048. }
  2049. }
  2050. }
  2051. }
  2052. columnNum = FirstDT.Columns.Count;
  2053. for (int i = 0; i < rowNum; i++)
  2054. {
  2055. IRow row1 = sheet2.CreateRow(PaintIndex);
  2056. PaintIndex = PaintIndex + 1;
  2057. row1.HeightInPoints = RowHeight;
  2058. //只需要绘制一行
  2059. if (i == 0)
  2060. {
  2061. for (int j = 0; j < columnNum - 3; j++)
  2062. {
  2063. if (j == 0)
  2064. {
  2065. row1.CreateCell(j);
  2066. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["pi_title"].ToString());
  2067. }
  2068. else if (j > 5 && j == columnNum - 5)
  2069. {
  2070. row1.CreateCell(j);
  2071. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  2072. }
  2073. else if (columnNum > 5 && j == columnNum - 5)
  2074. {
  2075. row1.CreateCell(j);
  2076. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_inoutno"].ToString());
  2077. }
  2078. else
  2079. {
  2080. row1.CreateCell(j);
  2081. }
  2082. row1.GetCell(j).CellStyle = style;
  2083. }
  2084. row1 = sheet2.CreateRow(PaintIndex);
  2085. PaintIndex = PaintIndex + 1;
  2086. //第二行添加型号
  2087. for (int j = 0; j < columnNum - 3; j++)
  2088. {
  2089. if (j == 0)
  2090. {
  2091. row1.CreateCell(j);
  2092. row1.Cells[j].SetCellValue(FirstDT.Rows[i]["pr_orispeccode"].ToString());
  2093. }
  2094. else if (j > 5 && j == columnNum - 5)
  2095. {
  2096. row1.CreateCell(j);
  2097. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  2098. }
  2099. else if (columnNum > 5 && j == columnNum - 5)
  2100. {
  2101. row1.CreateCell(j);
  2102. row1.Cells[j].SetCellValue(DataTable.Rows[i]["pi_date"].ToString());
  2103. }
  2104. else
  2105. {
  2106. row1.CreateCell(j);
  2107. }
  2108. row1.GetCell(j).CellStyle = style;
  2109. }
  2110. row1 = sheet2.CreateRow(PaintIndex);
  2111. PaintIndex = PaintIndex + 1;
  2112. //添加列名
  2113. for (int j = 4; j < columnNum; j++)
  2114. {
  2115. row1.CreateCell(j - 4);
  2116. row1.Cells[j - 4].CellStyle = styleborder;
  2117. row1.Cells[j - 4].SetCellValue(FirstDT.Columns[j].Caption);
  2118. }
  2119. row1 = sheet2.CreateRow(PaintIndex);
  2120. PaintIndex = PaintIndex + 1;
  2121. }
  2122. //添加数据内容
  2123. for (int j = 4; j < columnNum; j++)
  2124. {
  2125. string Data = FirstDT.Rows[i][j].ToString();
  2126. row1.CreateCell(j - 4);
  2127. row1.Cells[j - 4].SetCellValue(Data);
  2128. row1.GetCell(j - 4).CellStyle = styleborder;
  2129. if (FirstDT.Columns[j].ColumnName == "num")
  2130. {
  2131. sumCount += int.Parse(Data);
  2132. }
  2133. if (FirstDT.Columns[j].ColumnName == "io_qty")
  2134. {
  2135. totalCount += int.Parse(Data);
  2136. }
  2137. }
  2138. //添加总计行
  2139. if (i == rowNum - 1)
  2140. {
  2141. row1 = sheet2.CreateRow(PaintIndex);
  2142. PaintIndex = PaintIndex + 1;
  2143. for (int j = 0; j < columnNum - 4; j++)
  2144. {
  2145. if (j == 0)
  2146. {
  2147. row1.CreateCell(j);
  2148. row1.Cells[j].CellStyle = styleborder;
  2149. row1.Cells[j].SetCellValue("总计");
  2150. }
  2151. else if (j == columnNum - 6)
  2152. {
  2153. row1.CreateCell(j);
  2154. row1.Cells[j].CellStyle = styleborder;
  2155. row1.Cells[j].SetCellValue(sumCount);
  2156. }
  2157. else if (j == columnNum - 5)
  2158. {
  2159. row1.CreateCell(j);
  2160. row1.Cells[j].CellStyle = styleborder;
  2161. row1.Cells[j].SetCellValue(totalCount);
  2162. }
  2163. else
  2164. {
  2165. row1.CreateCell(j);
  2166. row1.Cells[j].CellStyle = styleborder;
  2167. }
  2168. }
  2169. }
  2170. }
  2171. for (int i = 0; i < sheet2.LastRowNum; i++)
  2172. {
  2173. if (i != 0)
  2174. {
  2175. sheet2.AutoSizeColumn(i);
  2176. sheet2.SetColumnWidth(i, sheet2.GetColumnWidth(i) + 1000);
  2177. }
  2178. }
  2179. //将book的内容写入内存流中返回
  2180. book.Write(ms);
  2181. return ms;
  2182. }
  2183. }
  2184. }