ExcelHandler.cs 116 KB

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