ExcelHandler.cs 126 KB

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