ExcelHandler.cs 131 KB

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