ExcelHandler.cs 118 KB

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