ExcelHandler.cs 197 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778
  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_Normal(DataTable firstsdt, DataTable dt, string FolderPath, string FileName, string Type, int PageSize, List<CheckBox> conditionbox)
  41. {
  42. //创建一个内存流,用来接收转换成Excel的内容
  43. MemoryStream ms;
  44. ms = DataTableToExcel_Normal(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. /// 晶源客户特殊方案
  66. /// </summary>
  67. public string JINYUAN(DataTable firstsdt, DataTable dt, string FolderPath, string FileName, string Type, int PageSize, List<CheckBox> conditionbox)
  68. {
  69. //创建一个内存流,用来接收转换成Excel的内容
  70. MemoryStream ms;
  71. ms = DataTableToExcel_JINYUAN(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. /// 利普芯客户特殊方案
  93. /// </summary>
  94. public string LIPIUXIN(DataTable firstsdt, DataTable dt, string FolderPath, string FileName, string Type, int PageSize, List<CheckBox> conditionbox)
  95. {
  96. //创建一个内存流,用来接收转换成Excel的内容
  97. MemoryStream ms;
  98. ms = DataTableToExcel_LIPUXIN(firstsdt, dt, Type, FileName, PageSize, conditionbox);
  99. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  100. string filePath = @FolderPath + "\\" + FileName + ".xls";
  101. int counter = 1;
  102. string filename = FileName + ".xls";
  103. while (File.Exists(filePath))
  104. {
  105. filename = string.Format("{0}({1}){2}", FileName, counter, ".xls"); //文件名+(count)+后缀
  106. filePath = Path.Combine(FolderPath, filename); //保存路径
  107. counter++; //count+1
  108. }
  109. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  110. byte[] data = ms.ToArray();
  111. fs.Write(data, 0, data.Length);
  112. fs.Flush();
  113. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  114. ms.Dispose();
  115. fs.Dispose();
  116. return filePath;
  117. }
  118. /// <summary>
  119. /// 导入Excel
  120. /// </summary>
  121. public void ImportExcel(DataTable DataTable, string TableName)
  122. {
  123. int columnNum = DataTable.Columns.Count;
  124. int rowNum = DataTable.Columns.Count;
  125. string[] field = new string[columnNum];
  126. for (int i = 0; i < columnNum; i++)
  127. {
  128. field[i] = DataTable.Rows[0][i].ToString();
  129. }
  130. }
  131. public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
  132. {
  133. DataTable dataTable = null;
  134. FileStream fs = null;
  135. DataColumn column = null;
  136. DataRow dataRow = null;
  137. IWorkbook workbook = null;
  138. ISheet sheet = null;
  139. IRow row = null;
  140. ICell cell = null;
  141. int startRow = 0;
  142. try
  143. {
  144. using (fs = File.OpenRead(filePath))
  145. {
  146. // 2007版本
  147. if (filePath.IndexOf(".xlsx") > 0)
  148. {
  149. workbook = new XSSFWorkbook(fs);
  150. }
  151. // 2003版本
  152. else if (filePath.IndexOf(".xls") > 0)
  153. {
  154. workbook = new HSSFWorkbook(fs);
  155. }
  156. if (workbook != null)
  157. {
  158. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  159. dataTable = new DataTable();
  160. if (sheet != null)
  161. {
  162. int rowCount = sheet.LastRowNum;//总行数
  163. if (rowCount > 0)
  164. {
  165. IRow firstRow = sheet.GetRow(0);//第一行
  166. int cellCount = firstRow.LastCellNum;//列数
  167. //构建datatable的列
  168. if (isColumnName)
  169. {
  170. startRow = 1;//如果第一行是列名,则从第二行开始读取
  171. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  172. {
  173. cell = firstRow.GetCell(i);
  174. if (cell != null)
  175. {
  176. if (cell.StringCellValue != null)
  177. {
  178. column = new DataColumn(cell.StringCellValue);
  179. dataTable.Columns.Add(column);
  180. }
  181. }
  182. }
  183. }
  184. else
  185. {
  186. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  187. {
  188. column = new DataColumn("column" + (i + 1));
  189. dataTable.Columns.Add(column);
  190. }
  191. }
  192. //填充行
  193. for (int i = startRow; i <= rowCount; ++i)
  194. {
  195. row = sheet.GetRow(i);
  196. if (row == null) continue;
  197. dataRow = dataTable.NewRow();
  198. for (int j = row.FirstCellNum; j < cellCount; ++j)
  199. {
  200. cell = row.GetCell(j);
  201. if (cell == null)
  202. {
  203. dataRow[j] = "";
  204. }
  205. else
  206. {
  207. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  208. switch (cell.CellType)
  209. {
  210. case CellType.Blank:
  211. dataRow[j] = "";
  212. break;
  213. case CellType.Numeric:
  214. short format = cell.CellStyle.DataFormat;
  215. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  216. if (format == 14 || format == 31 || format == 57 || format == 58)
  217. dataRow[j] = cell.DateCellValue;
  218. else
  219. dataRow[j] = cell.NumericCellValue;
  220. break;
  221. case CellType.String:
  222. dataRow[j] = cell.StringCellValue;
  223. break;
  224. }
  225. }
  226. }
  227. dataTable.Rows.Add(dataRow);
  228. }
  229. }
  230. }
  231. }
  232. }
  233. return dataTable;
  234. }
  235. catch (Exception)
  236. {
  237. if (fs != null)
  238. {
  239. fs.Close();
  240. }
  241. return null;
  242. }
  243. }
  244. int RowHeight = 12;
  245. /// <summary>
  246. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  247. /// </summary>
  248. /// <param name="DataTable"></param>
  249. /// <returns></returns>
  250. public MemoryStream DataTableToExcel(DataTable DataTable)
  251. {
  252. //创建内存流
  253. MemoryStream ms = new MemoryStream();
  254. //创建一个Book,相当于一个Excel文件
  255. HSSFWorkbook book = new HSSFWorkbook();
  256. //Excel中的Sheet
  257. ISheet sheet = book.CreateSheet("sheet1");
  258. //获取行数量和列数量
  259. int rowNum = DataTable.Rows.Count;
  260. int columnNum = DataTable.Columns.Count;
  261. //设置列的宽度,根据首行的列的内容的长度来设置
  262. for (int i = 0; i < columnNum; i++)
  263. {
  264. int dataLength;
  265. //如果内容比标题短则取标题长度
  266. if (DataTable.Rows[0][i].ToString().Length < DataTable.Columns[i].ColumnName.Length)
  267. {
  268. dataLength = DataTable.Columns[i].ColumnName.Length;
  269. dataLength = dataLength * 300;
  270. }
  271. else
  272. {
  273. dataLength = DataTable.Rows[0][i].ToString().Length;
  274. dataLength = dataLength * 300;
  275. }
  276. sheet.SetColumnWidth(i, dataLength);
  277. }
  278. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  279. IRow row = sheet.CreateRow(0);
  280. //冻结第一行
  281. sheet.CreateFreezePane(0, 1, 0, 1);
  282. ICellStyle style = book.CreateCellStyle();
  283. style.FillForegroundColor = HSSFColor.PaleBlue.Index;
  284. style.FillPattern = FillPattern.BigSpots;
  285. style.FillBackgroundColor = HSSFColor.LightGreen.Index;
  286. //设置边框
  287. style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  288. style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  289. style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  290. style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  291. row.HeightInPoints = 20;
  292. //固定第一行
  293. //row.RowStyle.IsLocked=true;
  294. //给第一行的标签赋值样式和值
  295. for (int j = 0; j < columnNum; j++)
  296. {
  297. row.CreateCell(j);
  298. row.Cells[j].CellStyle = style;
  299. row.Cells[j].CellStyle.VerticalAlignment = VerticalAlignment.Center;
  300. row.Cells[j].CellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  301. row.Cells[j].SetCellValue(DataTable.Columns[j].Caption);
  302. }
  303. //将DataTable的值循环赋值给book,Aligment设置居中
  304. //之前已经画了带颜色的第一行,所以从i=1开始画
  305. for (int i = 0; i < rowNum; i++)
  306. {
  307. IRow row1 = sheet.CreateRow(i + 1);
  308. row1.HeightInPoints = 20;
  309. for (int j = 0; j < columnNum; j++)
  310. {
  311. row1.CreateCell(j);
  312. row1.Cells[j].SetCellValue(DataTable.Rows[i][j].ToString());
  313. row1.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.Center;
  314. }
  315. }
  316. //将book的内容写入内存流中返回
  317. book.Write(ms);
  318. return ms;
  319. }
  320. /// <summary>
  321. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  322. /// </summary>
  323. /// <param name="DataTable"></param>
  324. /// <returns></returns>
  325. public MemoryStream DataTableToExcel_Normal(DataTable FirstDT, DataTable DataTable, string Type, string Inoutno, int PageSize, List<CheckBox> conditionbox)
  326. {
  327. string 小计 = "";
  328. string 总计 = "";
  329. string 片 = "";
  330. string companyname = "";
  331. if (dh.getFieldDataByCondition("ProdInout", "pi_exporttype", "pi_inoutno='" + Inoutno + "'").ToString() == "Chinese")
  332. {
  333. 小计 = "小计";
  334. 总计 = "总计";
  335. companyname = "深爱半导体股份有限公司芯片出货清单";
  336. }
  337. else
  338. {
  339. 小计 = "total";
  340. 总计 = "total";
  341. companyname = "SHENZHEN SI SEMICONDUCTORS CO.,LTD";
  342. }
  343. //转换为序列
  344. CheckBox[] box = conditionbox.ToArray();
  345. //创建内存流
  346. MemoryStream ms = new MemoryStream();
  347. //创建一个Book,相当于一个Excel文件
  348. HSSFWorkbook book = new HSSFWorkbook();
  349. //Excel中的Sheet
  350. ISheet sheet = book.CreateSheet("分页");
  351. sheet.SetMargin(MarginType.TopMargin, 0.4);
  352. sheet.SetMargin(MarginType.BottomMargin, 0.4);
  353. sheet.SetMargin(MarginType.LeftMargin, 0.4);
  354. sheet.SetMargin(MarginType.RightMargin, 0.4);
  355. sheet.FitToPage = true;
  356. sheet.PrintSetup.FitHeight = 200;
  357. //芯片号需要作为更新盒号的条件
  358. HSSFFont ffont = (HSSFFont)book.CreateFont();
  359. ffont.FontName = "宋体";
  360. bool ShowChcode = true;
  361. //更新箱号
  362. List<string> pib_id = new List<string>();
  363. //系统打印箱号
  364. List<string> pib_outboxcode1 = new List<string>();
  365. int BoxCode = 1;
  366. ICellStyle style = book.CreateCellStyle();
  367. style.VerticalAlignment = VerticalAlignment.Center;
  368. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  369. style.SetFont(ffont);
  370. ICellStyle styleborder = book.CreateCellStyle();
  371. styleborder.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  372. styleborder.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  373. styleborder.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  374. styleborder.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  375. styleborder.VerticalAlignment = VerticalAlignment.Center;
  376. styleborder.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  377. styleborder.SetFont(ffont);
  378. string pi_inoutno = "";
  379. HSSFFont ColumnTitle = (HSSFFont)book.CreateFont();
  380. ColumnTitle.FontName = "宋体";
  381. ColumnTitle.Boldweight = (short)FontBoldWeight.Bold;
  382. ICellStyle ColumnTitleStyle = book.CreateCellStyle();
  383. ColumnTitleStyle.SetFont(ColumnTitle);
  384. ColumnTitleStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  385. ColumnTitleStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  386. ColumnTitleStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  387. ColumnTitleStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  388. List<string> NotShowColumn = new List<string>();
  389. NotShowColumn.Add("ch_level");
  390. NotShowColumn.Add("pib_id");
  391. NotShowColumn.Add("chw_itemname1");
  392. NotShowColumn.Add("chw_itemname2");
  393. NotShowColumn.Add("pd_ordercode");
  394. NotShowColumn.Add("pr_size");
  395. NotShowColumn.Add("me_desc");
  396. NotShowColumn.Add("pr_orispeccode");
  397. NotShowColumn.Add("pr_orispeccode1");
  398. NotShowColumn.Add("pi_title");
  399. NotShowColumn.Add("pi_chipouttype");
  400. NotShowColumn.Add("pi_date");
  401. int ShowColumnCount = 0;
  402. //展示的内容列
  403. //设置列的宽度,根据首行的列的内容的长度来设置
  404. for (int i = DataTable.Columns.Count - 1; i > 0; i--)
  405. {
  406. for (int j = 0; j < box.Length; j++)
  407. {
  408. if (box[j].Name.ToLower() == "ch_bluefilm" && !box[j].Checked)
  409. {
  410. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_bluefilm"))
  411. {
  412. DataTable.Columns.RemoveAt(i);
  413. break;
  414. }
  415. }
  416. if (box[j].Name.ToLower() == "ch_code" && !box[j].Checked)
  417. {
  418. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_code"))
  419. {
  420. ShowChcode = true;
  421. break;
  422. }
  423. }
  424. if (box[j].Name.ToLower() == "ch_splitbatch" && !box[j].Checked)
  425. {
  426. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_splitbatch"))
  427. {
  428. DataTable.Columns.RemoveAt(i);
  429. break;
  430. }
  431. }
  432. if (box[j].Name.ToLower() == "ch_waterid" && !box[j].Checked)
  433. {
  434. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  435. {
  436. DataTable.Columns.RemoveAt(i);
  437. break;
  438. }
  439. }
  440. if (box[j].Name.ToLower() == "ch_pbcode" && !box[j].Checked)
  441. {
  442. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_pbcode"))
  443. {
  444. DataTable.Columns.RemoveAt(i);
  445. break;
  446. }
  447. }
  448. if (box[j].Name.ToLower() == "ch_remark" && !box[j].Checked)
  449. {
  450. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  451. {
  452. DataTable.Columns.RemoveAt(i);
  453. break;
  454. }
  455. }
  456. }
  457. }
  458. //获取行数量和列数量
  459. int rowNum = DataTable.Rows.Count;
  460. int columnNum = DataTable.Columns.Count;
  461. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  462. IRow row = sheet.CreateRow(0);
  463. //冻结第一行
  464. sheet.CreateFreezePane(0, 1, 0, 1);
  465. row.HeightInPoints = RowHeight;
  466. //固定第一行
  467. //row.RowStyle.IsLocked=true;
  468. //给第一行的标签赋值样式和值
  469. // ffont.FontHeight = 13;
  470. row.CreateCell(0);
  471. row.Cells[0].SetCellValue(" " + companyname);
  472. row.GetCell(0).CellStyle.SetFont((ffont));
  473. //ffont.FontHeight = 10;
  474. //开始绘制的Index
  475. int PaintIndex = 0;
  476. int sumCount = 0;
  477. int totalCount = 0;
  478. switch (Type)
  479. {
  480. case "FixRow":
  481. //清理系统取出来的数据
  482. BaseUtil.CleanDataTableData(FirstDT);
  483. //首页参数拼接
  484. string First_OrderCode = "";
  485. string First_Prspec = "";
  486. string First_Batch = "";
  487. int NumIndex = 0;
  488. ArrayList<string> First_WID = new ArrayList<string>();
  489. for (int i = 0; i < rowNum; i++)
  490. {
  491. IRow row1 = sheet.CreateRow(PaintIndex);
  492. PaintIndex = PaintIndex + 1;
  493. row1.HeightInPoints = RowHeight;
  494. //不包含的订单号
  495. if (DataTable.Columns.Contains("pd_ordercode") && !First_OrderCode.Contains(DataTable.Rows[i]["pd_ordercode"].ToString()))
  496. {
  497. First_OrderCode += DataTable.Rows[i]["pd_ordercode"].ToString() + " ";
  498. }
  499. //不包含的物料型号
  500. if (DataTable.Columns.Contains("pr_orispeccode") && !First_Prspec.Contains(DataTable.Rows[i]["pr_orispeccode"].ToString()))
  501. {
  502. First_Prspec += DataTable.Rows[i]["pr_orispeccode"].ToString() + " ";
  503. }
  504. //不包含扩撒批号
  505. if (DataTable.Columns.Contains("ch_splitbatch") && !First_Batch.Contains(DataTable.Rows[i]["ch_splitbatch"].ToString()))
  506. {
  507. First_Batch += DataTable.Rows[i]["ch_splitbatch"].ToString() + " ";
  508. }
  509. //不包含Wafer_id
  510. if (DataTable.Columns.Contains("Wafer_ID") && !First_WID.Contains(DataTable.Rows[i]["Wafer_ID"].ToString()))
  511. {
  512. First_WID.Add(DataTable.Rows[i]["Wafer_ID"].ToString());
  513. }
  514. if (i / PageSize >= 1 && i % PageSize == 0)
  515. {
  516. DataRow dr = FirstDT.NewRow();
  517. dr["pr_orispeccode"] = DataTable.Rows[i]["pr_orispeccode"].ToString();
  518. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  519. pi_inoutno = DataTable.Rows[i]["pi_inoutno"].ToString();
  520. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  521. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  522. dr["pd_ordercode"] = First_OrderCode;
  523. dr["pr_orispeccode"] = First_Prspec;
  524. dr["ch_splitbatch"] = First_Batch;
  525. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  526. dr["num"] = PageSize;
  527. dr["io_qty"] = sumCount;
  528. FirstDT.Rows.Add(dr);
  529. First_OrderCode = "";
  530. First_Prspec = "";
  531. First_Batch = "";
  532. First_WID.Clear();
  533. BoxCode = BoxCode + 1;
  534. for (int j = 0; j < ShowColumnCount; j++)
  535. {
  536. row1.CreateCell(j);
  537. if (j == 0)
  538. {
  539. row1.Cells[j].SetCellValue(小计);
  540. }
  541. else if (j == 1)
  542. {
  543. row1.Cells[1].SetCellValue((i % PageSize == 0 ? PageSize : i) + 片);
  544. }
  545. else if (j == 2)
  546. {
  547. row1.Cells[2].SetCellValue(sumCount);
  548. }
  549. row1.Cells[j].CellStyle = styleborder;
  550. }
  551. sumCount = 0;
  552. row1 = sheet.CreateRow(PaintIndex);
  553. sheet.SetRowBreak(PaintIndex - 1);
  554. sheet.Footer.Center = "第&P页,共&N页";
  555. PaintIndex = PaintIndex + 1;
  556. }
  557. //每次到了页数开始分页
  558. if (i % PageSize == 0 || i == rowNum - 1)
  559. {
  560. //第一行添加客户信息 rownum只有一行的情
  561. if (i != rowNum - 1 || rowNum == 1 || (rowNum - i == rowNum % PageSize))
  562. {
  563. for (int j = 0; j < columnNum - 3; j++)
  564. {
  565. if (j == 0)
  566. {
  567. row1.CreateCell(j);
  568. row1.Cells[j].SetCellValue(DataTable.Columns["pi_title"].Caption + ":" + DataTable.Rows[i]["pi_title"].ToString() + " "
  569. + DataTable.Columns["ch_level"].Caption + ":" + DataTable.Rows[i]["ch_level"].ToString());
  570. }
  571. else if (columnNum > 14 && j == columnNum - 14)
  572. {
  573. row1.CreateCell(j);
  574. row1.Cells[j].SetCellValue(DataTable.Columns["pi_inoutno"].Caption + ":" + DataTable.Rows[i]["pi_inoutno"].ToString());
  575. }
  576. else
  577. {
  578. row1.CreateCell(j);
  579. }
  580. row1.GetCell(j).CellStyle = style;
  581. }
  582. row1 = sheet.CreateRow(PaintIndex);
  583. PaintIndex = PaintIndex + 1;
  584. //第二行添加型号
  585. for (int j = 0; j < columnNum - 3; j++)
  586. {
  587. if (j == 0)
  588. {
  589. row1.CreateCell(j);
  590. row1.Cells[j].SetCellValue(DataTable.Columns["pr_orispeccode"].Caption + ":" + DataTable.Rows[i]["pr_orispeccode"].ToString() + " "
  591. + "(" + DataTable.Rows[i]["me_desc"].ToString() + ") " + DataTable.Columns["pi_chipouttype"].Caption + ":" + DataTable.Rows[i]["pi_chipouttype"].ToString());
  592. }
  593. else if (j == 3)
  594. {
  595. row1.CreateCell(j);
  596. row1.Cells[j].SetCellValue(DataTable.Columns["pr_size"].Caption + ":" + DataTable.Rows[i]["pr_size"].ToString());
  597. }
  598. else if (columnNum > 14 && j == columnNum - 14)
  599. {
  600. row1.CreateCell(j);
  601. row1.Cells[j].SetCellValue(DataTable.Columns["pi_date"].Caption + ":" + DataTable.Rows[i]["pi_date"].ToString());
  602. }
  603. else
  604. {
  605. row1.CreateCell(j);
  606. }
  607. row1.GetCell(j).CellStyle = style;
  608. }
  609. //添加列名
  610. row1 = sheet.CreateRow(PaintIndex);
  611. PaintIndex = PaintIndex + 1;
  612. //计数列所在的索引
  613. int showcount = 0;
  614. for (int j = 4; j < columnNum; j++)
  615. {
  616. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  617. {
  618. showcount = showcount + 1;
  619. row1.CreateCell(j - 4);
  620. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  621. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  622. }
  623. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  624. {
  625. showcount = showcount + 1;
  626. row1.CreateCell(j - 4);
  627. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  628. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  629. }
  630. else if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  631. (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")))
  632. {
  633. }
  634. else
  635. {
  636. showcount = showcount + 1;
  637. row1.CreateCell(j - 4);
  638. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  639. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  640. }
  641. if (DataTable.Columns[j].ColumnName.ToString() == "io_qty")
  642. {
  643. NumIndex = j;
  644. }
  645. }
  646. ShowColumnCount = showcount;
  647. row1 = sheet.CreateRow(PaintIndex);
  648. PaintIndex = PaintIndex + 1;
  649. }
  650. }
  651. //添加数据内容
  652. for (int j = 4; j < columnNum; j++)
  653. {
  654. string Data = DataTable.Rows[i][j].ToString();
  655. row1.CreateCell(j - 4);
  656. if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  657. (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")))
  658. {
  659. }
  660. else
  661. {
  662. row1.Cells[j - 4].SetCellValue(Data);
  663. row1.GetCell(j - 4).CellStyle = styleborder;
  664. }
  665. if (DataTable.Columns[j].ColumnName == "io_qty")
  666. {
  667. row1.Cells[j - 4].SetCellValue(int.Parse(Data));
  668. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  669. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  670. }
  671. if (DataTable.Columns[j].ColumnName == "rownum")
  672. {
  673. row1.Cells[j - 4].SetCellValue(i + 1);
  674. }
  675. }
  676. //固定行号分组的时候自动拼接新的DataTable
  677. if (i == rowNum - 1)
  678. {
  679. DataRow dr = FirstDT.NewRow();
  680. dr["pr_orispeccode"] = DataTable.Rows[i]["pr_orispeccode"].ToString();
  681. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  682. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  683. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  684. dr["pd_ordercode"] = First_OrderCode;
  685. dr["pr_orispeccode"] = First_Prspec;
  686. dr["ch_splitbatch"] = First_Batch;
  687. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  688. dr["num"] = (i % PageSize) + 1;
  689. dr["io_qty"] = sumCount;
  690. FirstDT.Rows.Add(dr);
  691. row1 = sheet.CreateRow(PaintIndex);
  692. PaintIndex = PaintIndex + 1;
  693. for (int j = 0; j < ShowColumnCount; j++)
  694. {
  695. row1.CreateCell(j);
  696. if (j == 0)
  697. {
  698. row1.Cells[j].SetCellValue(小计);
  699. }
  700. else if (j == 1)
  701. {
  702. row1.Cells[1].SetCellValue(rowNum % PageSize + 片);
  703. }
  704. else if (j == 2)
  705. {
  706. row1.Cells[2].SetCellValue(sumCount);
  707. }
  708. row1.Cells[j].CellStyle = styleborder;
  709. }
  710. row1 = sheet.CreateRow(PaintIndex);
  711. for (int j = 0; j < columnNum - 3; j++)
  712. {
  713. if (j == 0)
  714. {
  715. row1.CreateCell(j);
  716. row1.Cells[j].SetCellValue("备注");
  717. }
  718. else if (j == 2)
  719. {
  720. row1.CreateCell(j);
  721. row1.Cells[j].SetCellValue(totalCount);
  722. }
  723. else if (j == 3)
  724. {
  725. row1.CreateCell(j);
  726. row1.Cells[j].SetCellValue(rowNum);
  727. }
  728. else if (j == 4)
  729. {
  730. row1.CreateCell(j);
  731. row1.Cells[j].SetCellValue(片);
  732. }
  733. else
  734. {
  735. row1.CreateCell(j);
  736. }
  737. row1.Cells[j].CellStyle = style;
  738. }
  739. sheet.SetRowBreak(PaintIndex);
  740. sheet.Footer.Center = "第&P页,共&N页";
  741. PaintIndex = PaintIndex + 1;
  742. }
  743. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  744. pib_outboxcode1.Add(BoxCode.ToString());
  745. }
  746. for (int i = 0; i < sheet.LastRowNum; i++)
  747. {
  748. if (i != 0)
  749. {
  750. sheet.AutoSizeColumn(i);
  751. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  752. }
  753. }
  754. break;
  755. case "BatchCode":
  756. string LastBatchCode = "";
  757. int PageNum = 0;
  758. for (int i = 0; i < rowNum; i++)
  759. {
  760. IRow row1 = sheet.CreateRow(PaintIndex);
  761. PaintIndex = PaintIndex + 1;
  762. row1.HeightInPoints = RowHeight;
  763. //如果批号不相等的时候
  764. PageNum = PageNum + 1;
  765. if (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString())
  766. {
  767. BoxCode = BoxCode + 1;
  768. for (int j = 0; j < ShowColumnCount; j++)
  769. {
  770. row1.CreateCell(j);
  771. if (j == 0)
  772. {
  773. row1.Cells[j].SetCellValue(小计);
  774. row1.Cells[j].CellStyle = styleborder;
  775. }
  776. else if (j == 1)
  777. {
  778. row1.Cells[1].SetCellValue(PageNum - 1 + 片);
  779. }
  780. else if (j == 2)
  781. {
  782. row1.Cells[2].SetCellValue(sumCount);
  783. row1.Cells[j].CellStyle = styleborder;
  784. }
  785. else
  786. {
  787. row1.Cells[j].CellStyle = styleborder;
  788. }
  789. }
  790. sumCount = 0;
  791. row1 = sheet.CreateRow(PaintIndex);
  792. sheet.SetRowBreak(PaintIndex - 1);
  793. sheet.Footer.Center = "第&P页,共&N页";
  794. PaintIndex = PaintIndex + 1;
  795. PageNum = 1;
  796. }
  797. //每次到了页数开始分页
  798. if (LastBatchCode == "" || (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString()) || i == rowNum - 1)
  799. {
  800. LastBatchCode = DataTable.Rows[i]["ch_splitbatch"].ToString();
  801. //第一行添加客户信息
  802. //if (i != rowNum - 1)
  803. {
  804. for (int j = 0; j < columnNum - 3; j++)
  805. {
  806. if (j == 0)
  807. {
  808. row1.CreateCell(j);
  809. row1.Cells[j].SetCellValue(DataTable.Columns["pi_title"].Caption + ":" + DataTable.Rows[i]["pi_title"].ToString() + " "
  810. + DataTable.Columns["ch_level"].Caption + ":" + DataTable.Rows[i]["ch_level"].ToString());
  811. }
  812. else if (columnNum > 14 && j == columnNum - 14)
  813. {
  814. row1.CreateCell(j);
  815. row1.Cells[j].SetCellValue(DataTable.Columns["pi_inoutno"].Caption + ":" + DataTable.Rows[i]["pi_inoutno"].ToString());
  816. }
  817. else
  818. {
  819. row1.CreateCell(j);
  820. }
  821. row1.GetCell(j).CellStyle = style;
  822. }
  823. row1 = sheet.CreateRow(PaintIndex);
  824. PaintIndex = PaintIndex + 1;
  825. //第二行添加型号
  826. for (int j = 0; j < columnNum - 3; j++)
  827. {
  828. if (j == 0)
  829. {
  830. row1.CreateCell(j);
  831. row1.Cells[j].SetCellValue(DataTable.Columns["pr_orispeccode"].Caption + ":" + DataTable.Rows[i]["pr_orispeccode"].ToString() + " "
  832. + "(" + DataTable.Rows[i]["me_desc"].ToString() + ") " + DataTable.Columns["pi_chipouttype"].Caption + ":" + DataTable.Rows[i]["pi_chipouttype"].ToString());
  833. }
  834. else if (j == 3)
  835. {
  836. row1.CreateCell(j);
  837. row1.Cells[j].SetCellValue(DataTable.Columns["pr_size"].Caption + ":" + DataTable.Rows[i]["pr_size"].ToString());
  838. }
  839. else if (columnNum > 14 && j == columnNum - 14)
  840. {
  841. row1.CreateCell(j);
  842. row1.Cells[j].SetCellValue(DataTable.Columns["pi_date"].Caption + ":" + DataTable.Rows[i]["pi_date"].ToString());
  843. }
  844. else
  845. {
  846. row1.CreateCell(j);
  847. }
  848. row1.GetCell(j).CellStyle = style;
  849. }
  850. //添加列名
  851. row1 = sheet.CreateRow(PaintIndex);
  852. PaintIndex = PaintIndex + 1;
  853. int showcount = 0;
  854. for (int j = 4; j < columnNum; j++)
  855. {
  856. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  857. {
  858. showcount = showcount + 1;
  859. row1.CreateCell(j - 4);
  860. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  861. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  862. }
  863. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  864. {
  865. showcount = showcount + 1;
  866. row1.CreateCell(j - 4);
  867. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  868. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  869. }
  870. else if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  871. (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")))
  872. {
  873. }
  874. else
  875. {
  876. showcount = showcount + 1;
  877. row1.CreateCell(j - 4);
  878. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  879. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  880. }
  881. }
  882. ShowColumnCount = showcount;
  883. row1 = sheet.CreateRow(PaintIndex);
  884. PaintIndex = PaintIndex + 1;
  885. }
  886. }
  887. //添加数据内容
  888. for (int j = 4; j < columnNum; j++)
  889. {
  890. string Data = DataTable.Rows[i][j].ToString();
  891. row1.CreateCell(j - 4);
  892. if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  893. (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")))
  894. {
  895. }
  896. else
  897. {
  898. row1.Cells[j - 4].SetCellValue(Data);
  899. row1.GetCell(j - 4).CellStyle = styleborder;
  900. }
  901. if (DataTable.Columns[j].ColumnName == "io_qty")
  902. {
  903. row1.Cells[j - 4].SetCellValue(int.Parse(Data));
  904. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  905. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  906. }
  907. if (DataTable.Columns[j].ColumnName == "rownum")
  908. {
  909. row1.Cells[j - 4].SetCellValue(i + 1);
  910. }
  911. }
  912. if (i == rowNum - 1)
  913. {
  914. row1 = sheet.CreateRow(PaintIndex);
  915. PaintIndex = PaintIndex + 1;
  916. for (int j = 0; j < ShowColumnCount; j++)
  917. {
  918. row1.CreateCell(j);
  919. if (j == 0)
  920. {
  921. row1.Cells[j].SetCellValue(小计);
  922. row1.Cells[j].CellStyle = styleborder;
  923. }
  924. else if (j == 1)
  925. {
  926. row1.Cells[1].SetCellValue(PageNum + 片);
  927. }
  928. else if (j == 2)
  929. {
  930. row1.Cells[2].SetCellValue(sumCount);
  931. row1.Cells[j].CellStyle = styleborder;
  932. }
  933. else
  934. {
  935. row1.Cells[j].CellStyle = styleborder;
  936. }
  937. }
  938. //创建备注内容
  939. row1 = sheet.CreateRow(PaintIndex);
  940. for (int j = 0; j < columnNum - 3; j++)
  941. {
  942. if (j == 0)
  943. {
  944. row1.CreateCell(j);
  945. row1.Cells[j].SetCellValue("备注");
  946. }
  947. else if (j == 2)
  948. {
  949. row1.CreateCell(j);
  950. row1.Cells[j].SetCellValue(totalCount);
  951. }
  952. else if (j == 3)
  953. {
  954. row1.CreateCell(j);
  955. row1.Cells[j].SetCellValue(rowNum);
  956. }
  957. else if (j == 4)
  958. {
  959. row1.CreateCell(j);
  960. row1.Cells[j].SetCellValue(片);
  961. }
  962. else
  963. {
  964. row1.CreateCell(j);
  965. }
  966. row1.Cells[j].CellStyle = style;
  967. }
  968. sheet.SetRowBreak(PaintIndex);
  969. sheet.Footer.Center = "第&P页,共&N页";
  970. PaintIndex = PaintIndex + 1;
  971. PageNum = 1;
  972. }
  973. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  974. pib_outboxcode1.Add(BoxCode.ToString());
  975. }
  976. for (int i = 0; i < sheet.LastRowNum; i++)
  977. {
  978. if (i != 0)
  979. {
  980. sheet.AutoSizeColumn(i);
  981. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  982. }
  983. }
  984. break;
  985. case "BoxCode":
  986. string LastBoxCode = "";
  987. int PageNum1 = 0;
  988. for (int i = 0; i < rowNum; i++)
  989. {
  990. IRow row1 = sheet.CreateRow(PaintIndex);
  991. PaintIndex = PaintIndex + 1;
  992. row1.HeightInPoints = RowHeight;
  993. PageNum1 = PageNum1 + 1;
  994. //如果批号不相等的时候
  995. if (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["CH_PBCODE"].ToString())
  996. {
  997. BoxCode = BoxCode + 1;
  998. for (int j = 0; j < ShowColumnCount; j++)
  999. {
  1000. row1.CreateCell(j);
  1001. if (j == 0)
  1002. {
  1003. row1.Cells[j].SetCellValue(小计);
  1004. row1.Cells[j].CellStyle = styleborder;
  1005. }
  1006. else if (j == 1)
  1007. {
  1008. row1.Cells[1].SetCellValue(PageNum1 + 片);
  1009. }
  1010. else if (j == 2)
  1011. {
  1012. row1.Cells[2].SetCellValue(sumCount);
  1013. row1.Cells[j].CellStyle = styleborder;
  1014. }
  1015. else
  1016. {
  1017. row1.Cells[j].CellStyle = styleborder;
  1018. }
  1019. }
  1020. sumCount = 0;
  1021. row1 = sheet.CreateRow(PaintIndex);
  1022. sheet.SetRowBreak(PaintIndex - 1);
  1023. sheet.Footer.Center = "第&P页,共&N页";
  1024. PaintIndex = PaintIndex + 1;
  1025. }
  1026. //每次到了页数开始分页
  1027. if (LastBoxCode == "" || (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["ch_pbcode"].ToString()) || i == rowNum - 1)
  1028. {
  1029. LastBoxCode = DataTable.Rows[i]["CH_PBCODE"].ToString();
  1030. //第一行添加客户信息
  1031. //if (i != rowNum - 1)
  1032. {
  1033. for (int j = 0; j < columnNum - 3; j++)
  1034. {
  1035. if (j == 0)
  1036. {
  1037. row1.CreateCell(j);
  1038. row1.Cells[j].SetCellValue(DataTable.Columns["pi_title"].Caption + ":" + DataTable.Rows[i]["pi_title"].ToString() + " "
  1039. + DataTable.Columns["ch_level"].Caption + ":" + DataTable.Rows[i]["ch_level"].ToString());
  1040. }
  1041. else if (columnNum > 14 && j == columnNum - 14)
  1042. {
  1043. row1.CreateCell(j);
  1044. row1.Cells[j].SetCellValue(DataTable.Columns["pi_inoutno"].Caption + ":" + DataTable.Rows[i]["pi_inoutno"].ToString());
  1045. }
  1046. else
  1047. {
  1048. row1.CreateCell(j);
  1049. }
  1050. row1.GetCell(j).CellStyle = style;
  1051. }
  1052. row1 = sheet.CreateRow(PaintIndex);
  1053. PaintIndex = PaintIndex + 1;
  1054. //第二行添加型号
  1055. for (int j = 0; j < columnNum - 3; j++)
  1056. {
  1057. if (j == 0)
  1058. {
  1059. row1.CreateCell(j);
  1060. row1.Cells[j].SetCellValue(DataTable.Columns["pr_orispeccode"].Caption + ":" + DataTable.Rows[i]["pr_orispeccode"].ToString() + " "
  1061. + "(" + DataTable.Rows[i]["me_desc"].ToString() + ") " + DataTable.Columns["pi_chipouttype"].Caption + ":" + DataTable.Rows[i]["pi_chipouttype"].ToString());
  1062. }
  1063. else if (j == 3)
  1064. {
  1065. row1.CreateCell(j);
  1066. row1.Cells[j].SetCellValue(DataTable.Columns["pr_size"].Caption + ":" + DataTable.Rows[i]["pr_size"].ToString());
  1067. }
  1068. else if (columnNum > 14 && j == columnNum - 14)
  1069. {
  1070. row1.CreateCell(j);
  1071. row1.Cells[j].SetCellValue(DataTable.Columns["pi_date"].Caption + ":" + DataTable.Rows[i]["pi_date"].ToString());
  1072. }
  1073. else
  1074. {
  1075. row1.CreateCell(j);
  1076. }
  1077. row1.GetCell(j).CellStyle = style;
  1078. }
  1079. //添加列名
  1080. row1 = sheet.CreateRow(PaintIndex);
  1081. PaintIndex = PaintIndex + 1;
  1082. int showcount = 0;
  1083. for (int j = 4; j < columnNum; j++)
  1084. {
  1085. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  1086. {
  1087. showcount = showcount + 1;
  1088. row1.CreateCell(j - 4);
  1089. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1090. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  1091. }
  1092. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  1093. {
  1094. showcount = showcount + 1;
  1095. row1.CreateCell(j - 4);
  1096. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1097. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  1098. }
  1099. else if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  1100. (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")))
  1101. {
  1102. }
  1103. else
  1104. {
  1105. showcount = showcount + 1;
  1106. row1.CreateCell(j - 4);
  1107. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1108. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  1109. }
  1110. }
  1111. ShowColumnCount = showcount;
  1112. row1 = sheet.CreateRow(PaintIndex);
  1113. PaintIndex = PaintIndex + 1;
  1114. }
  1115. }
  1116. //添加数据内容
  1117. for (int j = 4; j < columnNum; j++)
  1118. {
  1119. string Data = DataTable.Rows[i][j].ToString();
  1120. row1.CreateCell(j - 4);
  1121. if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  1122. (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")))
  1123. {
  1124. }
  1125. else
  1126. {
  1127. row1.Cells[j - 4].SetCellValue(Data);
  1128. row1.GetCell(j - 4).CellStyle = styleborder;
  1129. }
  1130. if (DataTable.Columns[j].ColumnName == "io_qty")
  1131. {
  1132. row1.Cells[j - 4].SetCellValue(int.Parse(Data));
  1133. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  1134. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  1135. }
  1136. if (DataTable.Columns[j].ColumnName == "rownum")
  1137. {
  1138. row1.Cells[j - 4].SetCellValue(i + 1);
  1139. }
  1140. }
  1141. if (i == rowNum - 1)
  1142. {
  1143. row1 = sheet.CreateRow(PaintIndex);
  1144. PaintIndex = PaintIndex + 1;
  1145. for (int j = 0; j < ShowColumnCount; j++)
  1146. {
  1147. row1.CreateCell(j);
  1148. if (j == 0)
  1149. {
  1150. row1.Cells[j].SetCellValue(小计);
  1151. row1.Cells[j].CellStyle = styleborder;
  1152. }
  1153. else if (j == 1)
  1154. {
  1155. row1.Cells[1].SetCellValue(PageNum1 + 片);
  1156. }
  1157. else if (j == 2)
  1158. {
  1159. row1.Cells[2].SetCellValue(sumCount);
  1160. row1.Cells[j].CellStyle = styleborder;
  1161. }
  1162. else
  1163. {
  1164. row1.Cells[j].CellStyle = styleborder;
  1165. }
  1166. }
  1167. row1 = sheet.CreateRow(PaintIndex);
  1168. for (int j = 0; j < columnNum - 3; j++)
  1169. {
  1170. if (j == 0)
  1171. {
  1172. row1.CreateCell(j);
  1173. row1.Cells[j].SetCellValue("备注");
  1174. }
  1175. else if (j == 2)
  1176. {
  1177. row1.CreateCell(j);
  1178. row1.Cells[j].SetCellValue(totalCount);
  1179. }
  1180. else if (j == 3)
  1181. {
  1182. row1.CreateCell(j);
  1183. row1.Cells[j].SetCellValue(rowNum);
  1184. }
  1185. else if (j == 4)
  1186. {
  1187. row1.CreateCell(j);
  1188. row1.Cells[j].SetCellValue(片);
  1189. }
  1190. else
  1191. {
  1192. row1.CreateCell(j);
  1193. }
  1194. row1.Cells[j].CellStyle = style;
  1195. }
  1196. sheet.SetRowBreak(PaintIndex);
  1197. sheet.Footer.Center = "第&P页,共&N页";
  1198. PaintIndex = PaintIndex + 1;
  1199. PageNum1 = 1;
  1200. }
  1201. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  1202. pib_outboxcode1.Add(BoxCode.ToString());
  1203. }
  1204. for (int i = 0; i < sheet.LastRowNum; i++)
  1205. {
  1206. if (i != 0)
  1207. {
  1208. sheet.AutoSizeColumn(i);
  1209. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  1210. }
  1211. }
  1212. break;
  1213. default:
  1214. break;
  1215. }
  1216. 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());
  1217. //删除下载链接再重新插入
  1218. HttpHandler.GenDownLoadLinK(Inoutno);
  1219. //填充首页
  1220. sumCount = 0;
  1221. totalCount = 0;
  1222. PaintIndex = 1;
  1223. ISheet sheet2 = book.CreateSheet("首页");
  1224. row = sheet2.CreateRow(0);
  1225. row.CreateCell(0);
  1226. row.Cells[0].SetCellValue(" " + companyname);
  1227. row.GetCell(0).CellStyle = style;
  1228. rowNum = FirstDT.Rows.Count;
  1229. //不需要显示的列移除
  1230. for (int i = FirstDT.Columns.Count - 1; i > 0; i--)
  1231. {
  1232. for (int j = 0; j < box.Length; j++)
  1233. {
  1234. if (box[j].Name == "FirstPage_WID" && !box[j].Checked)
  1235. {
  1236. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  1237. {
  1238. FirstDT.Columns.RemoveAt(i);
  1239. }
  1240. }
  1241. if (box[j].Name == "FirstPage_YIELD" && !box[j].Checked)
  1242. {
  1243. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_yeild"))
  1244. {
  1245. FirstDT.Columns.RemoveAt(i);
  1246. }
  1247. }
  1248. if (box[j].Name == "FirstPage_REMARK" && !box[j].Checked)
  1249. {
  1250. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  1251. {
  1252. FirstDT.Columns.RemoveAt(i);
  1253. }
  1254. }
  1255. }
  1256. }
  1257. columnNum = FirstDT.Columns.Count;
  1258. for (int i = 0; i < rowNum; i++)
  1259. {
  1260. IRow row1 = sheet2.CreateRow(PaintIndex);
  1261. PaintIndex = PaintIndex + 1;
  1262. row1.HeightInPoints = RowHeight;
  1263. //只需要绘制一行
  1264. if (i == 0)
  1265. {
  1266. for (int j = 0; j < columnNum - 3; j++)
  1267. {
  1268. if (j == 0)
  1269. {
  1270. row1.CreateCell(j);
  1271. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_title"].Caption + ":" + FirstDT.Rows[i]["pi_title"].ToString());
  1272. }
  1273. else if (j > 5 && j == columnNum - 5)
  1274. {
  1275. row1.CreateCell(j);
  1276. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_inoutno"].Caption + ":" + FirstDT.Rows[i]["pi_inoutno"].ToString());
  1277. }
  1278. else if (columnNum > 5 && j == columnNum - 5)
  1279. {
  1280. row1.CreateCell(j);
  1281. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_inoutno"].Caption + ":" + FirstDT.Rows[i]["pi_inoutno"].ToString());
  1282. }
  1283. else
  1284. {
  1285. row1.CreateCell(j);
  1286. }
  1287. row1.GetCell(j).CellStyle = style;
  1288. }
  1289. row1 = sheet2.CreateRow(PaintIndex);
  1290. PaintIndex = PaintIndex + 1;
  1291. //第二行添加型号
  1292. for (int j = 0; j < columnNum - 3; j++)
  1293. {
  1294. if (j == 0)
  1295. {
  1296. row1.CreateCell(j);
  1297. row1.Cells[j].SetCellValue(FirstDT.Columns["pr_orispeccode"].Caption + ":" + FirstDT.Rows[i]["pr_orispeccode"].ToString());
  1298. }
  1299. else if (j > 5 && j == columnNum - 5)
  1300. {
  1301. row1.CreateCell(j);
  1302. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_date"].Caption + ":" + FirstDT.Rows[i]["pi_date"].ToString());
  1303. }
  1304. else if (columnNum > 5 && j == columnNum - 5)
  1305. {
  1306. row1.CreateCell(j);
  1307. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_date"].Caption + ":" + FirstDT.Rows[i]["pi_date"].ToString());
  1308. }
  1309. else
  1310. {
  1311. row1.CreateCell(j);
  1312. }
  1313. row1.GetCell(j).CellStyle = style;
  1314. }
  1315. row1 = sheet2.CreateRow(PaintIndex);
  1316. PaintIndex = PaintIndex + 1;
  1317. //添加列名
  1318. for (int j = 4; j < columnNum; j++)
  1319. {
  1320. row1.CreateCell(j - 4);
  1321. row1.Cells[j - 4].CellStyle = styleborder;
  1322. row1.Cells[j - 4].SetCellValue(FirstDT.Columns[j].Caption);
  1323. }
  1324. row1 = sheet2.CreateRow(PaintIndex);
  1325. PaintIndex = PaintIndex + 1;
  1326. }
  1327. //添加数据内容
  1328. for (int j = 4; j < columnNum; j++)
  1329. {
  1330. string Data = FirstDT.Rows[i][j].ToString();
  1331. row1.CreateCell(j - 4);
  1332. row1.Cells[j - 4].SetCellValue(Data);
  1333. row1.GetCell(j - 4).CellStyle = styleborder;
  1334. if (FirstDT.Columns[j].ColumnName == "num")
  1335. {
  1336. sumCount += int.Parse(Data);
  1337. }
  1338. if (FirstDT.Columns[j].ColumnName == "io_qty")
  1339. {
  1340. totalCount += int.Parse(Data);
  1341. }
  1342. }
  1343. //添加总计行
  1344. if (i == rowNum - 1)
  1345. {
  1346. row1 = sheet2.CreateRow(PaintIndex);
  1347. PaintIndex = PaintIndex + 1;
  1348. for (int j = 0; j < columnNum - 4; j++)
  1349. {
  1350. if (j == 0)
  1351. {
  1352. row1.CreateCell(j);
  1353. row1.Cells[j].CellStyle = styleborder;
  1354. row1.Cells[j].SetCellValue(总计);
  1355. }
  1356. else if (j == columnNum - 6)
  1357. {
  1358. row1.CreateCell(j);
  1359. row1.Cells[j].CellStyle = styleborder;
  1360. row1.Cells[j].SetCellValue(sumCount);
  1361. }
  1362. else if (j == columnNum - 5)
  1363. {
  1364. row1.CreateCell(j);
  1365. row1.Cells[j].CellStyle = styleborder;
  1366. row1.Cells[j].SetCellValue(totalCount);
  1367. }
  1368. else
  1369. {
  1370. row1.CreateCell(j);
  1371. row1.Cells[j].CellStyle = styleborder;
  1372. }
  1373. }
  1374. }
  1375. }
  1376. for (int i = 0; i < sheet2.LastRowNum; i++)
  1377. {
  1378. if (i != 0)
  1379. {
  1380. sheet2.AutoSizeColumn(i);
  1381. sheet2.SetColumnWidth(i, sheet2.GetColumnWidth(i) + 1000);
  1382. }
  1383. }
  1384. //将book的内容写入内存流中返回
  1385. book.Write(ms);
  1386. return ms;
  1387. }
  1388. /// <summary>
  1389. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  1390. /// </summary>
  1391. /// <param name="DataTable"></param>
  1392. /// <returns></returns>
  1393. private MemoryStream DataTableToExcel_JINYUAN(DataTable FirstDT, DataTable DataTable, string Type, string Inoutno, int PageSize, List<CheckBox> conditionbox)
  1394. {
  1395. string 小计 = "";
  1396. string 总计 = "";
  1397. string 片 = "";
  1398. string companyname = "";
  1399. if (dh.getFieldDataByCondition("ProdInout", "pi_exporttype", "pi_inoutno='" + Inoutno + "'").ToString() == "Chinese")
  1400. {
  1401. 小计 = "小计";
  1402. 总计 = "总计";
  1403. 片 = "片";
  1404. companyname = "深爱半导体股份有限公司芯片出货清单";
  1405. }
  1406. else
  1407. {
  1408. 小计 = "total";
  1409. 总计 = "total";
  1410. 片 = "slice";
  1411. companyname = "SHENZHEN SI SEMICONDUCTORS CO.,LTD";
  1412. }
  1413. //转换为序列
  1414. CheckBox[] box = conditionbox.ToArray();
  1415. //创建内存流
  1416. MemoryStream ms = new MemoryStream();
  1417. //创建一个Book,相当于一个Excel文件
  1418. HSSFWorkbook book = new HSSFWorkbook();
  1419. //Excel中的Sheet
  1420. ISheet sheet = book.CreateSheet("分页");
  1421. sheet.SetMargin(MarginType.TopMargin, 0.4);
  1422. sheet.SetMargin(MarginType.BottomMargin, 0.4);
  1423. sheet.SetMargin(MarginType.LeftMargin, 0.4);
  1424. sheet.SetMargin(MarginType.RightMargin, 0.4);
  1425. sheet.PrintSetup.NoColor = true;
  1426. sheet.PrintSetup.Landscape = true;
  1427. sheet.PrintSetup.PaperSize = (short)PaperSize.A4;
  1428. //芯片号需要作为更新盒号的条件
  1429. HSSFFont ffont = (HSSFFont)book.CreateFont();
  1430. ffont.FontName = "宋体";
  1431. bool ShowChcode = true;
  1432. //更新箱号
  1433. List<string> pib_id = new List<string>();
  1434. //系统打印箱号
  1435. List<string> pib_outboxcode1 = new List<string>();
  1436. int BoxCode = 1;
  1437. ICellStyle style = book.CreateCellStyle();
  1438. style.VerticalAlignment = VerticalAlignment.Center;
  1439. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  1440. style.SetFont(ffont);
  1441. ICellStyle styleborder = book.CreateCellStyle();
  1442. styleborder.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1443. styleborder.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1444. styleborder.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1445. styleborder.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1446. styleborder.VerticalAlignment = VerticalAlignment.Center;
  1447. styleborder.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  1448. styleborder.SetFont(ffont);
  1449. string pi_inoutno = "";
  1450. List<string> NotShowColumn = new List<string>();
  1451. NotShowColumn.Add("ch_level");
  1452. NotShowColumn.Add("pib_id");
  1453. NotShowColumn.Add("chw_itemname1");
  1454. NotShowColumn.Add("chw_itemname2");
  1455. NotShowColumn.Add("pd_ordercode");
  1456. NotShowColumn.Add("pr_size");
  1457. NotShowColumn.Add("me_desc");
  1458. NotShowColumn.Add("pr_orispeccode");
  1459. NotShowColumn.Add("pr_orispeccode1");
  1460. NotShowColumn.Add("pi_title");
  1461. NotShowColumn.Add("pi_date");
  1462. NotShowColumn.Add("pi_chipouttype");
  1463. //设置列的宽度,根据首行的列的内容的长度来设置
  1464. for (int i = DataTable.Columns.Count - 1; i > 0; i--)
  1465. {
  1466. for (int j = 0; j < box.Length; j++)
  1467. {
  1468. if (box[j].Name.ToLower() == "ch_bluefilm" && !box[j].Checked)
  1469. {
  1470. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_bluefilm"))
  1471. {
  1472. DataTable.Columns.RemoveAt(i);
  1473. break;
  1474. }
  1475. }
  1476. if (box[j].Name.ToLower() == "ch_code" && !box[j].Checked)
  1477. {
  1478. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_code"))
  1479. {
  1480. ShowChcode = true;
  1481. break;
  1482. }
  1483. }
  1484. if (box[j].Name.ToLower() == "ch_splitbatch" && !box[j].Checked)
  1485. {
  1486. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_splitbatch"))
  1487. {
  1488. DataTable.Columns.RemoveAt(i);
  1489. break;
  1490. }
  1491. }
  1492. if (box[j].Name.ToLower() == "ch_waterid" && !box[j].Checked)
  1493. {
  1494. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  1495. {
  1496. DataTable.Columns.RemoveAt(i);
  1497. break;
  1498. }
  1499. }
  1500. if (box[j].Name.ToLower() == "ch_pbcode" && !box[j].Checked)
  1501. {
  1502. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_pbcode"))
  1503. {
  1504. DataTable.Columns.RemoveAt(i);
  1505. break;
  1506. }
  1507. }
  1508. if (box[j].Name.ToLower() == "ch_remark" && !box[j].Checked)
  1509. {
  1510. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  1511. {
  1512. DataTable.Columns.RemoveAt(i);
  1513. break;
  1514. }
  1515. }
  1516. }
  1517. }
  1518. //获取行数量和列数量
  1519. int rowNum = DataTable.Rows.Count;
  1520. int columnNum = DataTable.Columns.Count;
  1521. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  1522. //ffont.FontHeight = 10;
  1523. //开始绘制的Index
  1524. int PaintIndex = 0;
  1525. int sumCount = 0;
  1526. int totalCount = 0;
  1527. //Title的字体
  1528. HSSFFont titlefont = (HSSFFont)book.CreateFont();
  1529. titlefont.FontName = "宋体";
  1530. titlefont.FontHeight = 270;
  1531. titlefont.Boldweight = (short)FontBoldWeight.Bold;
  1532. HSSFFont ColumnTitle = (HSSFFont)book.CreateFont();
  1533. ColumnTitle.FontName = "宋体";
  1534. ColumnTitle.Boldweight = (short)FontBoldWeight.Bold;
  1535. ICellStyle ColumnTitleStyle = book.CreateCellStyle();
  1536. ColumnTitleStyle.SetFont(ColumnTitle);
  1537. ColumnTitleStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1538. ColumnTitleStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1539. ColumnTitleStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1540. ColumnTitleStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1541. ICellStyle TitleStyle = book.CreateCellStyle();
  1542. TitleStyle.SetFont(titlefont);
  1543. int ShowColumnCount = 0;
  1544. IRow row1 = null;
  1545. switch (Type)
  1546. {
  1547. case "FixRow":
  1548. //清理系统取出来的数据
  1549. BaseUtil.CleanDataTableData(FirstDT);
  1550. //首页参数拼接
  1551. string First_OrderCode = "";
  1552. string First_Prspec = "";
  1553. string First_Batch = "";
  1554. int NumIndex = 0;
  1555. ArrayList<string> First_WID = new ArrayList<string>();
  1556. for (int i = 0; i < rowNum; i++)
  1557. {
  1558. if (PaintIndex != 0)
  1559. {
  1560. row1 = sheet.CreateRow(PaintIndex);
  1561. PaintIndex = PaintIndex + 1;
  1562. row1.HeightInPoints = RowHeight;
  1563. }
  1564. //不包含的订单号
  1565. if (DataTable.Columns.Contains("pd_ordercode") && !First_OrderCode.Contains(DataTable.Rows[i]["pd_ordercode"].ToString()))
  1566. {
  1567. First_OrderCode += DataTable.Rows[i]["pd_ordercode"].ToString() + " ";
  1568. }
  1569. //不包含的物料型号
  1570. if (DataTable.Columns.Contains("pr_orispeccode") && !First_Prspec.Contains(DataTable.Rows[i]["pr_orispeccode"].ToString()))
  1571. {
  1572. First_Prspec += DataTable.Rows[i]["pr_orispeccode"].ToString() + " ";
  1573. }
  1574. //不包含扩撒批号
  1575. if (DataTable.Columns.Contains("ch_splitbatch") && !First_Batch.Contains(DataTable.Rows[i]["ch_splitbatch"].ToString()))
  1576. {
  1577. First_Batch += DataTable.Rows[i]["ch_splitbatch"].ToString() + " ";
  1578. }
  1579. //不包含Wafer_id
  1580. if (DataTable.Columns.Contains("Wafer_ID") && !First_WID.Contains(DataTable.Rows[i]["Wafer_ID"].ToString()))
  1581. {
  1582. First_WID.Add(DataTable.Rows[i]["Wafer_ID"].ToString());
  1583. }
  1584. if (i / PageSize >= 1 && i % PageSize == 0)
  1585. {
  1586. DataRow dr = FirstDT.NewRow();
  1587. dr["pr_orispeccode"] = DataTable.Rows[i]["pr_orispeccode"].ToString();
  1588. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  1589. pi_inoutno = DataTable.Rows[i]["pi_inoutno"].ToString();
  1590. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  1591. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  1592. dr["pd_ordercode"] = First_OrderCode;
  1593. dr["pr_orispeccode"] = First_Prspec;
  1594. dr["ch_splitbatch"] = First_Batch;
  1595. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  1596. dr["num"] = PageSize;
  1597. dr["io_qty"] = sumCount;
  1598. FirstDT.Rows.Add(dr);
  1599. First_OrderCode = "";
  1600. First_Prspec = "";
  1601. First_Batch = "";
  1602. First_WID.Clear();
  1603. BoxCode = BoxCode + 1;
  1604. for (int j = 0; j < ShowColumnCount; j++)
  1605. {
  1606. row1.CreateCell(j);
  1607. if (j == 0)
  1608. {
  1609. row1.Cells[j].SetCellValue(小计);
  1610. }
  1611. else if (j == 1)
  1612. {
  1613. row1.Cells[1].SetCellValue((i % PageSize == 0 ? PageSize : i) + 片);
  1614. row1.Cells[1].CellStyle = ColumnTitleStyle;
  1615. }
  1616. else if (j == 2)
  1617. {
  1618. row1.Cells[2].SetCellValue(sumCount);
  1619. }
  1620. row1.Cells[j].CellStyle = styleborder;
  1621. }
  1622. sumCount = 0;
  1623. row1 = sheet.CreateRow(PaintIndex);
  1624. sheet.SetRowBreak(PaintIndex - 1);
  1625. sheet.Footer.Center = "第&P页,共&N页";
  1626. PaintIndex = PaintIndex + 1;
  1627. }
  1628. //每次到了页数开始分页
  1629. if (i % PageSize == 0 || i == rowNum - 1)
  1630. {
  1631. //第一行添加客户信息 rownum只有一行的情
  1632. if (i != rowNum - 1 || rowNum == 1)
  1633. {
  1634. //抬头设置
  1635. row1 = sheet.CreateRow(PaintIndex);
  1636. PaintIndex = PaintIndex + 1;
  1637. row1.CreateCell(0).SetCellValue(" " + companyname);
  1638. row1.GetCell(0).CellStyle = TitleStyle;
  1639. row1 = sheet.CreateRow(PaintIndex);
  1640. PaintIndex = PaintIndex + 1;
  1641. for (int j = 0; j < columnNum - 3; j++)
  1642. {
  1643. if (j == 0)
  1644. {
  1645. row1.CreateCell(j);
  1646. row1.Cells[j].SetCellValue(DataTable.Columns["pi_title"].Caption + ":" + DataTable.Rows[i]["pi_title"].ToString() + " "
  1647. + DataTable.Columns["ch_level"].Caption + ":" + DataTable.Rows[i]["ch_level"].ToString());
  1648. }
  1649. else if (columnNum > 14 && j == columnNum - 14)
  1650. {
  1651. row1.CreateCell(j);
  1652. row1.Cells[j].SetCellValue(DataTable.Columns["pi_inoutno"].Caption + ":" + DataTable.Rows[i]["pi_inoutno"].ToString());
  1653. }
  1654. else
  1655. {
  1656. row1.CreateCell(j);
  1657. }
  1658. row1.GetCell(j).CellStyle = style;
  1659. }
  1660. row1 = sheet.CreateRow(PaintIndex);
  1661. PaintIndex = PaintIndex + 1;
  1662. //第二行添加型号
  1663. for (int j = 0; j < columnNum - 3; j++)
  1664. {
  1665. if (j == 0)
  1666. {
  1667. row1.CreateCell(j);
  1668. row1.Cells[j].SetCellValue(DataTable.Columns["pr_orispeccode"].Caption + ":" + DataTable.Rows[i]["pr_orispeccode"].ToString() + " "
  1669. + DataTable.Columns["pi_chipouttype"].Caption + ":" + DataTable.Rows[i]["pi_chipouttype"].ToString());
  1670. }
  1671. else if (j == 3)
  1672. {
  1673. row1.CreateCell(j);
  1674. row1.Cells[j].SetCellValue(DataTable.Columns["pr_size"].Caption + ":" + DataTable.Rows[i]["pr_size"].ToString());
  1675. }
  1676. else if (columnNum > 14 && j == columnNum - 14)
  1677. {
  1678. row1.CreateCell(j);
  1679. row1.Cells[j].SetCellValue(DataTable.Columns["pi_date"].Caption + ":" + DataTable.Rows[i]["pi_date"].ToString());
  1680. }
  1681. else
  1682. {
  1683. row1.CreateCell(j);
  1684. }
  1685. row1.GetCell(j).CellStyle = style;
  1686. }
  1687. //特殊客户添加一列空行
  1688. row1 = sheet.CreateRow(PaintIndex);
  1689. PaintIndex = PaintIndex + 1;
  1690. for (int j = 0; j < columnNum; j++)
  1691. {
  1692. row1.CreateCell(j).SetCellValue("");
  1693. }
  1694. //添加列名
  1695. row1 = sheet.CreateRow(PaintIndex);
  1696. PaintIndex = PaintIndex + 1;
  1697. //计数列所在的索引
  1698. int showcount = 0;
  1699. //前面有4列标题内容,数据库设置的,需要从4开始索引
  1700. for (int j = 4; j < columnNum; j++)
  1701. {
  1702. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  1703. {
  1704. showcount = showcount + 1;
  1705. row1.CreateCell(j - 4);
  1706. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1707. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  1708. }
  1709. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  1710. {
  1711. showcount = showcount + 1;
  1712. row1.CreateCell(j - 4);
  1713. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1714. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  1715. }
  1716. else if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  1717. (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")))
  1718. {
  1719. }
  1720. else
  1721. {
  1722. showcount = showcount + 1;
  1723. row1.CreateCell(j - 4);
  1724. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1725. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  1726. }
  1727. if (DataTable.Columns[j].ColumnName.ToString() == "io_qty")
  1728. {
  1729. NumIndex = j;
  1730. }
  1731. }
  1732. ShowColumnCount = showcount;
  1733. row1 = sheet.CreateRow(PaintIndex);
  1734. PaintIndex = PaintIndex + 1;
  1735. }
  1736. }
  1737. //添加数据内容
  1738. for (int j = 4; j < columnNum; j++)
  1739. {
  1740. string Data = DataTable.Rows[i][j].ToString();
  1741. row1.CreateCell(j - 4);
  1742. if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  1743. (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")))
  1744. {
  1745. }
  1746. else
  1747. {
  1748. row1.Cells[j - 4].SetCellValue(Data);
  1749. row1.GetCell(j - 4).CellStyle = styleborder;
  1750. }
  1751. if (DataTable.Columns[j].ColumnName == "io_qty")
  1752. {
  1753. row1.Cells[j - 4].SetCellValue(int.Parse(Data));
  1754. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  1755. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  1756. }
  1757. if (DataTable.Columns[j].ColumnName == "rownum")
  1758. {
  1759. row1.Cells[j - 4].SetCellValue(i + 1);
  1760. }
  1761. }
  1762. //固定行号分组的时候自动拼接新的DataTable
  1763. if (i == rowNum - 1)
  1764. {
  1765. DataRow dr = FirstDT.NewRow();
  1766. dr["pr_orispeccode"] = DataTable.Rows[i]["pr_orispeccode"].ToString();
  1767. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  1768. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  1769. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  1770. dr["pd_ordercode"] = First_OrderCode;
  1771. dr["pr_orispeccode"] = First_Prspec;
  1772. dr["ch_splitbatch"] = First_Batch;
  1773. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  1774. dr["num"] = (i % PageSize) + 1;
  1775. dr["io_qty"] = sumCount;
  1776. FirstDT.Rows.Add(dr);
  1777. row1 = sheet.CreateRow(PaintIndex);
  1778. PaintIndex = PaintIndex + 1;
  1779. for (int j = 0; j < ShowColumnCount; j++)
  1780. {
  1781. row1.CreateCell(j);
  1782. if (j == 0)
  1783. {
  1784. row1.Cells[j].SetCellValue(小计);
  1785. }
  1786. else if (j == 1)
  1787. {
  1788. row1.Cells[1].SetCellValue(rowNum % PageSize + 片);
  1789. row1.Cells[1].CellStyle = ColumnTitleStyle;
  1790. }
  1791. else if (j == 2)
  1792. {
  1793. row1.Cells[2].SetCellValue(sumCount);
  1794. }
  1795. row1.Cells[j].CellStyle = styleborder;
  1796. }
  1797. row1 = sheet.CreateRow(PaintIndex);
  1798. for (int j = 0; j < columnNum - 3; j++)
  1799. {
  1800. if (j == 0)
  1801. {
  1802. row1.CreateCell(j);
  1803. row1.Cells[j].SetCellValue("备注");
  1804. }
  1805. else if (j == 2)
  1806. {
  1807. row1.CreateCell(j);
  1808. row1.Cells[j].SetCellValue(totalCount);
  1809. }
  1810. else if (j == 3)
  1811. {
  1812. row1.CreateCell(j);
  1813. row1.Cells[j].SetCellValue(rowNum);
  1814. }
  1815. else if (j == 4)
  1816. {
  1817. row1.CreateCell(j);
  1818. row1.Cells[j].SetCellValue(片);
  1819. }
  1820. else
  1821. {
  1822. row1.CreateCell(j);
  1823. }
  1824. row1.Cells[j].CellStyle = style;
  1825. }
  1826. sheet.SetRowBreak(PaintIndex);
  1827. sheet.Footer.Center = "第&P页,共&N页";
  1828. PaintIndex = PaintIndex + 1;
  1829. }
  1830. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  1831. pib_outboxcode1.Add(BoxCode.ToString());
  1832. }
  1833. for (int i = 0; i < sheet.LastRowNum; i++)
  1834. {
  1835. if (i != 0)
  1836. {
  1837. sheet.AutoSizeColumn(i);
  1838. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  1839. }
  1840. }
  1841. break;
  1842. case "BatchCode":
  1843. string LastBatchCode = "";
  1844. int PageNum = 0;
  1845. for (int i = 0; i < rowNum; i++)
  1846. {
  1847. row1 = sheet.CreateRow(PaintIndex);
  1848. if (i != 0)
  1849. PaintIndex = PaintIndex + 1;
  1850. row1.HeightInPoints = RowHeight;
  1851. //如果批号不相等的时候
  1852. PageNum = PageNum + 1;
  1853. if (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString())
  1854. {
  1855. BoxCode = BoxCode + 1;
  1856. for (int j = 0; j < ShowColumnCount; j++)
  1857. {
  1858. row1.CreateCell(j);
  1859. if (j == 0)
  1860. {
  1861. row1.Cells[j].SetCellValue(小计);
  1862. row1.Cells[j].CellStyle = styleborder;
  1863. }
  1864. else if (j == 1)
  1865. {
  1866. row1.Cells[1].SetCellValue(PageNum - 1 + 片);
  1867. row1.Cells[1].CellStyle = ColumnTitleStyle;
  1868. }
  1869. else if (j == 2)
  1870. {
  1871. row1.Cells[2].SetCellValue(sumCount);
  1872. row1.Cells[j].CellStyle = styleborder;
  1873. }
  1874. else
  1875. {
  1876. row1.Cells[j].CellStyle = styleborder;
  1877. }
  1878. }
  1879. sumCount = 0;
  1880. //row1 = sheet.CreateRow(PaintIndex);
  1881. //sheet.SetRowBreak(PaintIndex - 1);
  1882. sheet.Footer.Center = "第&P页,共&N页";
  1883. PageNum = 1;
  1884. }
  1885. //每次到了页数开始分页
  1886. if (LastBatchCode == "" || (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString()))
  1887. {
  1888. LastBatchCode = DataTable.Rows[i]["ch_splitbatch"].ToString();
  1889. //第一行添加客户信息
  1890. row1 = sheet.CreateRow(PaintIndex);
  1891. PaintIndex = PaintIndex + 1;
  1892. row1.CreateCell(0).SetCellValue(" " + companyname);
  1893. row1.GetCell(0).CellStyle = TitleStyle;
  1894. row1 = sheet.CreateRow(PaintIndex);
  1895. PaintIndex = PaintIndex + 1;
  1896. //if (i != rowNum - 1)
  1897. {
  1898. for (int j = 0; j < columnNum - 3; j++)
  1899. {
  1900. if (j == 0)
  1901. {
  1902. row1.CreateCell(j);
  1903. row1.Cells[j].SetCellValue(DataTable.Columns["pi_title"].Caption + ":" + DataTable.Rows[i]["pi_title"].ToString() + " "
  1904. + DataTable.Columns["ch_level"].Caption + ":" + DataTable.Rows[i]["ch_level"].ToString());
  1905. }
  1906. else if (columnNum > 14 && j == columnNum - 14)
  1907. {
  1908. row1.CreateCell(j);
  1909. row1.Cells[j].SetCellValue(DataTable.Columns["pi_inoutno"].Caption + ":" + DataTable.Rows[i]["pi_inoutno"].ToString());
  1910. }
  1911. else
  1912. {
  1913. row1.CreateCell(j);
  1914. }
  1915. row1.GetCell(j).CellStyle = style;
  1916. }
  1917. row1 = sheet.CreateRow(PaintIndex);
  1918. PaintIndex = PaintIndex + 1;
  1919. //第二行添加型号
  1920. for (int j = 0; j < columnNum - 3; j++)
  1921. {
  1922. if (j == 0)
  1923. {
  1924. row1.CreateCell(j);
  1925. row1.Cells[j].SetCellValue(DataTable.Columns["pr_orispeccode"].Caption + ":" + DataTable.Rows[i]["pr_orispeccode"].ToString() + " "
  1926. + DataTable.Columns["pi_chipouttype"].Caption + ":" + DataTable.Rows[i]["pi_chipouttype"].ToString());
  1927. }
  1928. else if (j == 3)
  1929. {
  1930. row1.CreateCell(j);
  1931. row1.Cells[j].SetCellValue(DataTable.Columns["pr_size"].Caption + ":" + DataTable.Rows[i]["pr_size"].ToString());
  1932. }
  1933. else if (columnNum > 14 && j == columnNum - 14)
  1934. {
  1935. row1.CreateCell(j);
  1936. row1.Cells[j].SetCellValue(DataTable.Columns["pi_date"].Caption + ":" + DataTable.Rows[i]["pi_date"].ToString());
  1937. }
  1938. else
  1939. {
  1940. row1.CreateCell(j);
  1941. }
  1942. row1.GetCell(j).CellStyle = style;
  1943. }
  1944. //特殊客户添加一列空行
  1945. row1 = sheet.CreateRow(PaintIndex);
  1946. PaintIndex = PaintIndex + 1;
  1947. for (int j = 0; j < columnNum; j++)
  1948. {
  1949. row1.CreateCell(j).SetCellValue("");
  1950. }
  1951. //添加列名
  1952. row1 = sheet.CreateRow(PaintIndex);
  1953. PaintIndex = PaintIndex + 1;
  1954. int showcount = 0;
  1955. for (int j = 4; j < columnNum; j++)
  1956. {
  1957. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  1958. {
  1959. showcount = showcount + 1;
  1960. row1.CreateCell(j - 4);
  1961. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1962. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  1963. }
  1964. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  1965. {
  1966. showcount = showcount + 1;
  1967. row1.CreateCell(j - 4);
  1968. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1969. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  1970. }
  1971. else if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  1972. (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")))
  1973. {
  1974. }
  1975. else
  1976. {
  1977. showcount = showcount + 1;
  1978. row1.CreateCell(j - 4);
  1979. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  1980. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  1981. }
  1982. }
  1983. ShowColumnCount = showcount;
  1984. row1 = sheet.CreateRow(PaintIndex);
  1985. PaintIndex = PaintIndex + 1;
  1986. }
  1987. }
  1988. //添加数据内容
  1989. for (int j = 4; j < columnNum; j++)
  1990. {
  1991. string Data = DataTable.Rows[i][j].ToString();
  1992. row1.CreateCell(j - 4);
  1993. if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  1994. (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")))
  1995. {
  1996. }
  1997. else
  1998. {
  1999. row1.Cells[j - 4].SetCellValue(Data);
  2000. row1.GetCell(j - 4).CellStyle = styleborder;
  2001. }
  2002. if (DataTable.Columns[j].ColumnName == "io_qty")
  2003. {
  2004. row1.Cells[j - 4].SetCellValue(int.Parse(Data));
  2005. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  2006. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  2007. }
  2008. if (DataTable.Columns[j].ColumnName == "rownum")
  2009. {
  2010. row1.Cells[j - 4].SetCellValue(i + 1);
  2011. }
  2012. }
  2013. if (i == rowNum - 1)
  2014. {
  2015. row1 = sheet.CreateRow(PaintIndex);
  2016. PaintIndex = PaintIndex + 1;
  2017. for (int j = 0; j < ShowColumnCount; j++)
  2018. {
  2019. row1.CreateCell(j);
  2020. if (j == 0)
  2021. {
  2022. row1.Cells[j].SetCellValue(小计);
  2023. row1.Cells[j].CellStyle = styleborder;
  2024. }
  2025. else if (j == 1)
  2026. {
  2027. row1.Cells[1].SetCellValue(PageNum + 片);
  2028. row1.Cells[1].CellStyle = ColumnTitleStyle;
  2029. }
  2030. else if (j == 2)
  2031. {
  2032. row1.Cells[2].SetCellValue(sumCount);
  2033. row1.Cells[j].CellStyle = styleborder;
  2034. }
  2035. else
  2036. {
  2037. row1.Cells[j].CellStyle = styleborder;
  2038. }
  2039. }
  2040. //创建备注内容
  2041. row1 = sheet.CreateRow(PaintIndex);
  2042. for (int j = 0; j < columnNum - 3; j++)
  2043. {
  2044. if (j == 0)
  2045. {
  2046. row1.CreateCell(j);
  2047. row1.Cells[j].SetCellValue("备注");
  2048. }
  2049. else if (j == 2)
  2050. {
  2051. row1.CreateCell(j);
  2052. row1.Cells[j].SetCellValue(totalCount);
  2053. }
  2054. else if (j == 3)
  2055. {
  2056. row1.CreateCell(j);
  2057. row1.Cells[j].SetCellValue(rowNum);
  2058. }
  2059. else if (j == 4)
  2060. {
  2061. row1.CreateCell(j);
  2062. row1.Cells[j].SetCellValue(片);
  2063. }
  2064. else
  2065. {
  2066. row1.CreateCell(j);
  2067. }
  2068. row1.Cells[j].CellStyle = style;
  2069. }
  2070. sheet.SetRowBreak(PaintIndex);
  2071. sheet.Footer.Center = "第&P页,共&N页";
  2072. PaintIndex = PaintIndex + 1;
  2073. PageNum = 1;
  2074. }
  2075. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  2076. pib_outboxcode1.Add(BoxCode.ToString());
  2077. }
  2078. for (int i = 0; i < sheet.LastRowNum; i++)
  2079. {
  2080. if (i != 0)
  2081. {
  2082. sheet.AutoSizeColumn(i);
  2083. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  2084. }
  2085. }
  2086. break;
  2087. case "BoxCode":
  2088. string LastBoxCode = "";
  2089. int PageNum1 = 0;
  2090. for (int i = 0; i < rowNum; i++)
  2091. {
  2092. row1 = sheet.CreateRow(PaintIndex);
  2093. if (i != 0)
  2094. PaintIndex = PaintIndex + 1;
  2095. row1.HeightInPoints = RowHeight;
  2096. PageNum1 = PageNum1 + 1;
  2097. //如果箱号不相等的时候
  2098. if (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["CH_PBCODE"].ToString())
  2099. {
  2100. BoxCode = BoxCode + 1;
  2101. for (int j = 0; j < ShowColumnCount; j++)
  2102. {
  2103. row1.CreateCell(j);
  2104. if (j == 0)
  2105. {
  2106. row1.Cells[j].SetCellValue(小计);
  2107. row1.Cells[j].CellStyle = styleborder;
  2108. }
  2109. else if (j == 1)
  2110. {
  2111. row1.Cells[1].SetCellValue(PageNum1 - 1 + 片);
  2112. row1.Cells[1].CellStyle = ColumnTitleStyle;
  2113. }
  2114. else if (j == 2)
  2115. {
  2116. row1.Cells[2].SetCellValue(sumCount);
  2117. row1.Cells[j].CellStyle = styleborder;
  2118. }
  2119. else
  2120. {
  2121. row1.Cells[j].CellStyle = styleborder;
  2122. }
  2123. }
  2124. sumCount = 0;
  2125. //row1 = sheet.CreateRow(PaintIndex);
  2126. //sheet.SetRowBreak(PaintIndex - 1);
  2127. sheet.Footer.Center = "第&P页,共&N页";
  2128. PageNum1 = 1;
  2129. }
  2130. //每次到了页数开始分页
  2131. if (LastBoxCode == "" || (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["ch_pbcode"].ToString()))
  2132. {
  2133. LastBoxCode = DataTable.Rows[i]["CH_PBCODE"].ToString();
  2134. row1 = sheet.CreateRow(PaintIndex);
  2135. PaintIndex = PaintIndex + 1;
  2136. row1.CreateCell(0).SetCellValue(" " + companyname);
  2137. row1.GetCell(0).CellStyle = TitleStyle;
  2138. row1 = sheet.CreateRow(PaintIndex);
  2139. PaintIndex = PaintIndex + 1;
  2140. //第一行添加客户信息
  2141. //if (i != rowNum - 1)
  2142. {
  2143. for (int j = 0; j < columnNum - 3; j++)
  2144. {
  2145. if (j == 0)
  2146. {
  2147. row1.CreateCell(j);
  2148. row1.Cells[j].SetCellValue(DataTable.Columns["pi_title"].Caption + ":" + DataTable.Rows[i]["pi_title"].ToString() + " "
  2149. + DataTable.Columns["ch_level"].Caption + ":" + DataTable.Rows[i]["ch_level"].ToString());
  2150. }
  2151. else if (columnNum > 14 && j == columnNum - 14)
  2152. {
  2153. row1.CreateCell(j);
  2154. row1.Cells[j].SetCellValue(DataTable.Columns["pi_inoutno"].Caption + ":" + DataTable.Rows[i]["pi_inoutno"].ToString());
  2155. }
  2156. else
  2157. {
  2158. row1.CreateCell(j);
  2159. }
  2160. row1.GetCell(j).CellStyle = style;
  2161. }
  2162. row1 = sheet.CreateRow(PaintIndex);
  2163. PaintIndex = PaintIndex + 1;
  2164. //第二行添加型号
  2165. for (int j = 0; j < columnNum - 3; j++)
  2166. {
  2167. if (j == 0)
  2168. {
  2169. row1.CreateCell(j);
  2170. row1.Cells[j].SetCellValue(DataTable.Columns["pr_orispeccode"].Caption + ":" + DataTable.Rows[i]["pr_orispeccode"].ToString() + " "
  2171. + DataTable.Columns["pi_chipouttype"].Caption + ":" + DataTable.Rows[i]["pi_chipouttype"].ToString());
  2172. }
  2173. else if (j == 3)
  2174. {
  2175. row1.CreateCell(j);
  2176. row1.Cells[j].SetCellValue(DataTable.Columns["pr_size"].Caption + ":" + DataTable.Rows[i]["pr_size"].ToString());
  2177. }
  2178. else if (columnNum > 14 && j == columnNum - 14)
  2179. {
  2180. row1.CreateCell(j);
  2181. row1.Cells[j].SetCellValue(DataTable.Columns["pi_date"].Caption + ":" + DataTable.Rows[i]["pi_date"].ToString());
  2182. }
  2183. else
  2184. {
  2185. row1.CreateCell(j);
  2186. }
  2187. row1.GetCell(j).CellStyle = style;
  2188. }
  2189. //特殊客户添加一列空行
  2190. row1 = sheet.CreateRow(PaintIndex);
  2191. PaintIndex = PaintIndex + 1;
  2192. for (int j = 0; j < columnNum; j++)
  2193. {
  2194. row1.CreateCell(j).SetCellValue("");
  2195. }
  2196. //添加列名
  2197. row1 = sheet.CreateRow(PaintIndex);
  2198. PaintIndex = PaintIndex + 1;
  2199. int showcount = 0;
  2200. for (int j = 4; j < columnNum; j++)
  2201. {
  2202. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  2203. {
  2204. showcount = showcount + 1;
  2205. row1.CreateCell(j - 4);
  2206. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  2207. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  2208. }
  2209. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  2210. {
  2211. showcount = showcount + 1;
  2212. row1.CreateCell(j - 4);
  2213. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  2214. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  2215. }
  2216. else if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  2217. (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")))
  2218. {
  2219. }
  2220. else
  2221. {
  2222. showcount = showcount + 1;
  2223. row1.CreateCell(j - 4);
  2224. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  2225. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  2226. }
  2227. }
  2228. ShowColumnCount = showcount;
  2229. row1 = sheet.CreateRow(PaintIndex);
  2230. PaintIndex = PaintIndex + 1;
  2231. }
  2232. }
  2233. //添加数据内容
  2234. for (int j = 4; j < columnNum; j++)
  2235. {
  2236. string Data = DataTable.Rows[i][j].ToString();
  2237. row1.CreateCell(j - 4);
  2238. if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  2239. (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")))
  2240. {
  2241. }
  2242. else
  2243. {
  2244. row1.Cells[j - 4].SetCellValue(Data);
  2245. row1.GetCell(j - 4).CellStyle = styleborder;
  2246. }
  2247. if (DataTable.Columns[j].ColumnName == "io_qty")
  2248. {
  2249. row1.Cells[j - 4].SetCellValue(int.Parse(Data));
  2250. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  2251. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  2252. }
  2253. if (DataTable.Columns[j].ColumnName == "rownum")
  2254. {
  2255. row1.Cells[j - 4].SetCellValue(i + 1);
  2256. }
  2257. }
  2258. if (i == rowNum - 1)
  2259. {
  2260. row1 = sheet.CreateRow(PaintIndex);
  2261. PaintIndex = PaintIndex + 1;
  2262. for (int j = 0; j < ShowColumnCount; j++)
  2263. {
  2264. row1.CreateCell(j);
  2265. if (j == 0)
  2266. {
  2267. row1.Cells[j].SetCellValue(小计);
  2268. row1.Cells[j].CellStyle = styleborder;
  2269. }
  2270. else if (j == 1)
  2271. {
  2272. row1.Cells[1].SetCellValue(PageNum1 + 片);
  2273. row1.Cells[1].CellStyle = ColumnTitleStyle;
  2274. }
  2275. else if (j == 2)
  2276. {
  2277. row1.Cells[2].SetCellValue(sumCount);
  2278. row1.Cells[j].CellStyle = styleborder;
  2279. }
  2280. else
  2281. {
  2282. row1.Cells[j].CellStyle = styleborder;
  2283. }
  2284. }
  2285. row1 = sheet.CreateRow(PaintIndex);
  2286. for (int j = 0; j < columnNum - 3; j++)
  2287. {
  2288. if (j == 0)
  2289. {
  2290. row1.CreateCell(j);
  2291. row1.Cells[j].SetCellValue("备注");
  2292. }
  2293. else if (j == 2)
  2294. {
  2295. row1.CreateCell(j);
  2296. row1.Cells[j].SetCellValue(totalCount);
  2297. }
  2298. else if (j == 3)
  2299. {
  2300. row1.CreateCell(j);
  2301. row1.Cells[j].SetCellValue(rowNum);
  2302. }
  2303. else if (j == 4)
  2304. {
  2305. row1.CreateCell(j);
  2306. row1.Cells[j].SetCellValue(片);
  2307. }
  2308. else
  2309. {
  2310. row1.CreateCell(j);
  2311. }
  2312. row1.Cells[j].CellStyle = style;
  2313. }
  2314. sheet.SetRowBreak(PaintIndex);
  2315. sheet.Footer.Center = "第&P页,共&N页";
  2316. PaintIndex = PaintIndex + 1;
  2317. PageNum1 = 1;
  2318. }
  2319. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  2320. pib_outboxcode1.Add(BoxCode.ToString());
  2321. }
  2322. for (int i = 0; i < sheet.LastRowNum; i++)
  2323. {
  2324. if (i != 0)
  2325. {
  2326. sheet.AutoSizeColumn(i);
  2327. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  2328. }
  2329. }
  2330. break;
  2331. default:
  2332. break;
  2333. }
  2334. 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());
  2335. //删除下载链接再重新插入
  2336. HttpHandler.GenDownLoadLinK(Inoutno);
  2337. //填充首页
  2338. sumCount = 0;
  2339. totalCount = 0;
  2340. PaintIndex = 0;
  2341. ISheet sheet2 = book.CreateSheet("首页");
  2342. IRow row = sheet2.CreateRow(0);
  2343. row.CreateCell(0);
  2344. row.Cells[0].SetCellValue(" " + companyname);
  2345. row.GetCell(0).CellStyle = style;
  2346. rowNum = FirstDT.Rows.Count;
  2347. //不需要显示的列移除
  2348. for (int i = FirstDT.Columns.Count - 1; i > 0; i--)
  2349. {
  2350. for (int j = 0; j < box.Length; j++)
  2351. {
  2352. if (box[j].Name == "FirstPage_WID" && !box[j].Checked)
  2353. {
  2354. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  2355. {
  2356. FirstDT.Columns.RemoveAt(i);
  2357. }
  2358. }
  2359. if (box[j].Name == "FirstPage_YIELD" && !box[j].Checked)
  2360. {
  2361. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_yeild"))
  2362. {
  2363. FirstDT.Columns.RemoveAt(i);
  2364. }
  2365. }
  2366. if (box[j].Name == "FirstPage_REMARK" && !box[j].Checked)
  2367. {
  2368. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  2369. {
  2370. FirstDT.Columns.RemoveAt(i);
  2371. }
  2372. }
  2373. }
  2374. }
  2375. columnNum = FirstDT.Columns.Count;
  2376. for (int i = 0; i < rowNum; i++)
  2377. {
  2378. row1 = sheet2.CreateRow(PaintIndex);
  2379. PaintIndex = PaintIndex + 1;
  2380. row1.HeightInPoints = RowHeight;
  2381. //只需要绘制一行
  2382. if (i == 0)
  2383. {
  2384. for (int j = 0; j < columnNum - 3; j++)
  2385. {
  2386. if (j == 0)
  2387. {
  2388. row1.CreateCell(j);
  2389. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_title"].Caption + ":" + FirstDT.Rows[i]["pi_title"].ToString());
  2390. }
  2391. else if (j > 5 && j == columnNum - 5)
  2392. {
  2393. row1.CreateCell(j);
  2394. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_inoutno"].Caption + ":" + FirstDT.Rows[i]["pi_inoutno"].ToString());
  2395. }
  2396. else if (columnNum > 5 && j == columnNum - 5)
  2397. {
  2398. row1.CreateCell(j);
  2399. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_inoutno"].Caption + ":" + FirstDT.Rows[i]["pi_inoutno"].ToString());
  2400. }
  2401. else
  2402. {
  2403. row1.CreateCell(j);
  2404. }
  2405. row1.GetCell(j).CellStyle = style;
  2406. }
  2407. row1 = sheet2.CreateRow(PaintIndex);
  2408. PaintIndex = PaintIndex + 1;
  2409. //第二行添加型号
  2410. for (int j = 0; j < columnNum - 3; j++)
  2411. {
  2412. if (j == 0)
  2413. {
  2414. row1.CreateCell(j);
  2415. row1.Cells[j].SetCellValue(FirstDT.Columns["pr_orispeccode"].Caption + ":" + FirstDT.Rows[i]["pr_orispeccode"].ToString());
  2416. }
  2417. else if (j > 5 && j == columnNum - 5)
  2418. {
  2419. row1.CreateCell(j);
  2420. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_date"].Caption + ":" + FirstDT.Rows[i]["pi_date"].ToString());
  2421. }
  2422. else if (columnNum > 5 && j == columnNum - 5)
  2423. {
  2424. row1.CreateCell(j);
  2425. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_date"].Caption + ":" + FirstDT.Rows[i]["pi_date"].ToString());
  2426. }
  2427. else
  2428. {
  2429. row1.CreateCell(j);
  2430. }
  2431. row1.GetCell(j).CellStyle = style;
  2432. }
  2433. row1 = sheet2.CreateRow(PaintIndex);
  2434. PaintIndex = PaintIndex + 1;
  2435. //添加列名
  2436. for (int j = 4; j < columnNum; j++)
  2437. {
  2438. row1.CreateCell(j - 4);
  2439. row1.Cells[j - 4].CellStyle = styleborder;
  2440. row1.Cells[j - 4].SetCellValue(FirstDT.Columns[j].Caption);
  2441. }
  2442. row1 = sheet2.CreateRow(PaintIndex);
  2443. PaintIndex = PaintIndex + 1;
  2444. }
  2445. //添加数据内容
  2446. for (int j = 4; j < columnNum; j++)
  2447. {
  2448. string Data = FirstDT.Rows[i][j].ToString();
  2449. row1.CreateCell(j - 4);
  2450. row1.Cells[j - 4].SetCellValue(Data);
  2451. row1.GetCell(j - 4).CellStyle = styleborder;
  2452. if (FirstDT.Columns[j].ColumnName == "num")
  2453. {
  2454. sumCount += int.Parse(Data);
  2455. }
  2456. if (FirstDT.Columns[j].ColumnName == "io_qty")
  2457. {
  2458. totalCount += int.Parse(Data);
  2459. }
  2460. }
  2461. //添加总计行
  2462. if (i == rowNum - 1)
  2463. {
  2464. row1 = sheet2.CreateRow(PaintIndex);
  2465. PaintIndex = PaintIndex + 1;
  2466. for (int j = 0; j < columnNum - 4; j++)
  2467. {
  2468. if (j == 0)
  2469. {
  2470. row1.CreateCell(j);
  2471. row1.Cells[j].CellStyle = styleborder;
  2472. row1.Cells[j].SetCellValue(总计);
  2473. }
  2474. else if (j == columnNum - 6)
  2475. {
  2476. row1.CreateCell(j);
  2477. row1.Cells[j].CellStyle = styleborder;
  2478. row1.Cells[j].SetCellValue(sumCount);
  2479. }
  2480. else if (j == columnNum - 5)
  2481. {
  2482. row1.CreateCell(j);
  2483. row1.Cells[j].CellStyle = styleborder;
  2484. row1.Cells[j].SetCellValue(totalCount);
  2485. }
  2486. else
  2487. {
  2488. row1.CreateCell(j);
  2489. row1.Cells[j].CellStyle = styleborder;
  2490. }
  2491. }
  2492. }
  2493. }
  2494. for (int i = 0; i < sheet2.LastRowNum; i++)
  2495. {
  2496. if (i != 0)
  2497. {
  2498. sheet2.AutoSizeColumn(i);
  2499. sheet2.SetColumnWidth(i, sheet2.GetColumnWidth(i) + 1000);
  2500. }
  2501. }
  2502. //将book的内容写入内存流中返回
  2503. book.Write(ms);
  2504. return ms;
  2505. }
  2506. /// <summary>
  2507. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  2508. /// </summary>
  2509. /// <param name="DataTable"></param>
  2510. /// <returns></returns>
  2511. private MemoryStream DataTableToExcel_LIPUXIN(DataTable FirstDT, DataTable DataTable, string Type, string Inoutno, int PageSize, List<CheckBox> conditionbox)
  2512. {
  2513. string 小计 = "";
  2514. string 批小计 = "";
  2515. string 总计 = "";
  2516. string 片 = "";
  2517. string companyname = "";
  2518. if (dh.getFieldDataByCondition("ProdInout", "pi_exporttype", "pi_inoutno='" + Inoutno + "'").ToString() == "Chinese")
  2519. {
  2520. 小计 = "小计";
  2521. 总计 = "总计";
  2522. 批小计 = "批小计";
  2523. companyname = "深爱半导体股份有限公司芯片出货清单";
  2524. }
  2525. else
  2526. {
  2527. 批小计 = "BatchTotal";
  2528. 小计 = "total";
  2529. 总计 = "total";
  2530. companyname = "SHENZHEN SI SEMICONDUCTORS CO.,LTD";
  2531. }
  2532. //转换为序列
  2533. CheckBox[] box = conditionbox.ToArray();
  2534. //创建内存流
  2535. MemoryStream ms = new MemoryStream();
  2536. //创建一个Book,相当于一个Excel文件
  2537. HSSFWorkbook book = new HSSFWorkbook();
  2538. //Excel中的Sheet
  2539. ISheet sheet = book.CreateSheet("分页");
  2540. sheet.SetMargin(MarginType.TopMargin, 0.4);
  2541. sheet.SetMargin(MarginType.BottomMargin, 0.4);
  2542. sheet.SetMargin(MarginType.LeftMargin, 0.4);
  2543. sheet.SetMargin(MarginType.RightMargin, 0.4);
  2544. sheet.FitToPage = true;
  2545. sheet.PrintSetup.FitHeight = 200;
  2546. //芯片号需要作为更新盒号的条件
  2547. HSSFFont ffont = (HSSFFont)book.CreateFont();
  2548. ffont.FontName = "宋体";
  2549. bool ShowChcode = true;
  2550. //更新箱号
  2551. List<string> pib_id = new List<string>();
  2552. //系统打印箱号
  2553. List<string> pib_outboxcode1 = new List<string>();
  2554. int BoxCode = 1;
  2555. ICellStyle style = book.CreateCellStyle();
  2556. style.VerticalAlignment = VerticalAlignment.Center;
  2557. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  2558. style.SetFont(ffont);
  2559. ICellStyle styleborder = book.CreateCellStyle();
  2560. styleborder.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2561. styleborder.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2562. styleborder.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2563. styleborder.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2564. styleborder.VerticalAlignment = VerticalAlignment.Center;
  2565. styleborder.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
  2566. styleborder.SetFont(ffont);
  2567. string pi_inoutno = "";
  2568. HSSFFont ColumnTitle = (HSSFFont)book.CreateFont();
  2569. ColumnTitle.FontName = "宋体";
  2570. ColumnTitle.Boldweight = (short)FontBoldWeight.Bold;
  2571. ICellStyle ColumnTitleStyle = book.CreateCellStyle();
  2572. ColumnTitleStyle.SetFont(ColumnTitle);
  2573. ColumnTitleStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  2574. ColumnTitleStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  2575. ColumnTitleStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  2576. ColumnTitleStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  2577. List<string> NotShowColumn = new List<string>();
  2578. NotShowColumn.Add("ch_level");
  2579. NotShowColumn.Add("pib_id");
  2580. NotShowColumn.Add("chw_itemname1");
  2581. NotShowColumn.Add("chw_itemname2");
  2582. NotShowColumn.Add("pd_ordercode");
  2583. NotShowColumn.Add("pr_size");
  2584. NotShowColumn.Add("me_desc");
  2585. NotShowColumn.Add("pr_orispeccode");
  2586. NotShowColumn.Add("pr_orispeccode1");
  2587. NotShowColumn.Add("pi_title");
  2588. NotShowColumn.Add("pi_chipouttype");
  2589. NotShowColumn.Add("pi_date");
  2590. int ShowColumnCount = 0;
  2591. //展示的内容列
  2592. //设置列的宽度,根据首行的列的内容的长度来设置
  2593. for (int i = DataTable.Columns.Count - 1; i > 0; i--)
  2594. {
  2595. for (int j = 0; j < box.Length; j++)
  2596. {
  2597. if (box[j].Name.ToLower() == "ch_bluefilm" && !box[j].Checked)
  2598. {
  2599. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_bluefilm"))
  2600. {
  2601. DataTable.Columns.RemoveAt(i);
  2602. break;
  2603. }
  2604. }
  2605. if (box[j].Name.ToLower() == "ch_code" && !box[j].Checked)
  2606. {
  2607. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_code"))
  2608. {
  2609. ShowChcode = true;
  2610. break;
  2611. }
  2612. }
  2613. if (box[j].Name.ToLower() == "ch_splitbatch" && !box[j].Checked)
  2614. {
  2615. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_splitbatch"))
  2616. {
  2617. DataTable.Columns.RemoveAt(i);
  2618. break;
  2619. }
  2620. }
  2621. if (box[j].Name.ToLower() == "ch_waterid" && !box[j].Checked)
  2622. {
  2623. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  2624. {
  2625. DataTable.Columns.RemoveAt(i);
  2626. break;
  2627. }
  2628. }
  2629. if (box[j].Name.ToLower() == "ch_pbcode" && !box[j].Checked)
  2630. {
  2631. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_pbcode"))
  2632. {
  2633. DataTable.Columns.RemoveAt(i);
  2634. break;
  2635. }
  2636. }
  2637. if (box[j].Name.ToLower() == "ch_remark" && !box[j].Checked)
  2638. {
  2639. if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  2640. {
  2641. DataTable.Columns.RemoveAt(i);
  2642. break;
  2643. }
  2644. }
  2645. }
  2646. }
  2647. //获取行数量和列数量
  2648. int rowNum = DataTable.Rows.Count;
  2649. int columnNum = DataTable.Columns.Count;
  2650. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  2651. IRow row = sheet.CreateRow(0);
  2652. //冻结第一行
  2653. sheet.CreateFreezePane(0, 1, 0, 1);
  2654. row.HeightInPoints = RowHeight;
  2655. //固定第一行
  2656. //row.RowStyle.IsLocked=true;
  2657. //给第一行的标签赋值样式和值
  2658. // ffont.FontHeight = 13;
  2659. row.CreateCell(0);
  2660. row.Cells[0].SetCellValue(" " + companyname);
  2661. row.GetCell(0).CellStyle.SetFont((ffont));
  2662. //ffont.FontHeight = 10;
  2663. //开始绘制的Index
  2664. int PaintIndex = 0;
  2665. int sumCount = 0;
  2666. int totalCount = 0;
  2667. switch (Type)
  2668. {
  2669. case "FixRow":
  2670. //清理系统取出来的数据
  2671. BaseUtil.CleanDataTableData(FirstDT);
  2672. //首页参数拼接
  2673. string First_OrderCode = "";
  2674. string First_Prspec = "";
  2675. string First_Batch = "";
  2676. int NumIndex = 0;
  2677. ArrayList<string> First_WID = new ArrayList<string>();
  2678. for (int i = 0; i < rowNum; i++)
  2679. {
  2680. IRow row1 = sheet.CreateRow(PaintIndex);
  2681. PaintIndex = PaintIndex + 1;
  2682. row1.HeightInPoints = RowHeight;
  2683. //不包含的订单号
  2684. if (DataTable.Columns.Contains("pd_ordercode") && !First_OrderCode.Contains(DataTable.Rows[i]["pd_ordercode"].ToString()))
  2685. {
  2686. First_OrderCode += DataTable.Rows[i]["pd_ordercode"].ToString() + " ";
  2687. }
  2688. //不包含的物料型号
  2689. if (DataTable.Columns.Contains("pr_orispeccode") && !First_Prspec.Contains(DataTable.Rows[i]["pr_orispeccode"].ToString()))
  2690. {
  2691. First_Prspec += DataTable.Rows[i]["pr_orispeccode"].ToString() + " ";
  2692. }
  2693. //不包含扩撒批号
  2694. if (DataTable.Columns.Contains("ch_splitbatch") && !First_Batch.Contains(DataTable.Rows[i]["ch_splitbatch"].ToString()))
  2695. {
  2696. First_Batch += DataTable.Rows[i]["ch_splitbatch"].ToString() + " ";
  2697. }
  2698. //不包含Wafer_id
  2699. if (DataTable.Columns.Contains("Wafer_ID") && !First_WID.Contains(DataTable.Rows[i]["Wafer_ID"].ToString()))
  2700. {
  2701. First_WID.Add(DataTable.Rows[i]["Wafer_ID"].ToString());
  2702. }
  2703. if (i / PageSize >= 1 && i % PageSize == 0)
  2704. {
  2705. DataRow dr = FirstDT.NewRow();
  2706. dr["pr_orispeccode"] = DataTable.Rows[i]["pr_orispeccode"].ToString();
  2707. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  2708. pi_inoutno = DataTable.Rows[i]["pi_inoutno"].ToString();
  2709. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  2710. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  2711. dr["pd_ordercode"] = First_OrderCode;
  2712. dr["pr_orispeccode"] = First_Prspec;
  2713. dr["ch_splitbatch"] = First_Batch;
  2714. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  2715. dr["num"] = PageSize;
  2716. dr["io_qty"] = sumCount;
  2717. FirstDT.Rows.Add(dr);
  2718. First_OrderCode = "";
  2719. First_Prspec = "";
  2720. First_Batch = "";
  2721. First_WID.Clear();
  2722. BoxCode = BoxCode + 1;
  2723. for (int j = 0; j < ShowColumnCount; j++)
  2724. {
  2725. row1.CreateCell(j);
  2726. if (j == 0)
  2727. {
  2728. row1.Cells[j].SetCellValue(批小计);
  2729. }
  2730. else if (j == 1)
  2731. {
  2732. row1.Cells[1].SetCellValue((i % PageSize == 0 ? PageSize : i) + 片);
  2733. }
  2734. else if (j == 2)
  2735. {
  2736. row1.Cells[2].SetCellValue(sumCount);
  2737. }
  2738. row1.Cells[j].CellStyle = styleborder;
  2739. }
  2740. row1 = sheet.CreateRow(PaintIndex);
  2741. PaintIndex = PaintIndex + 1;
  2742. for (int j = 0; j < ShowColumnCount; j++)
  2743. {
  2744. row1.CreateCell(j);
  2745. if (j == 0)
  2746. {
  2747. row1.Cells[j].SetCellValue(小计);
  2748. }
  2749. else if (j == 1)
  2750. {
  2751. row1.Cells[1].SetCellValue((i % PageSize == 0 ? PageSize : i) + 片);
  2752. }
  2753. else if (j == 2)
  2754. {
  2755. row1.Cells[2].SetCellValue(sumCount);
  2756. }
  2757. row1.Cells[j].CellStyle = styleborder;
  2758. }
  2759. sumCount = 0;
  2760. row1 = sheet.CreateRow(PaintIndex);
  2761. sheet.SetRowBreak(PaintIndex - 1);
  2762. sheet.Footer.Center = "第&P页,共&N页";
  2763. PaintIndex = PaintIndex + 1;
  2764. }
  2765. //每次到了页数开始分页
  2766. if (i % PageSize == 0 || i == rowNum - 1)
  2767. {
  2768. //第一行添加客户信息 rownum只有一行的情
  2769. if (i != rowNum - 1 || rowNum == 1 || (rowNum - i == rowNum % PageSize))
  2770. {
  2771. for (int j = 0; j < columnNum - 3; j++)
  2772. {
  2773. if (j == 0)
  2774. {
  2775. row1.CreateCell(j);
  2776. row1.Cells[j].SetCellValue(DataTable.Columns["pi_title"].Caption + ":" + DataTable.Rows[i]["pi_title"].ToString() + " "
  2777. + DataTable.Columns["ch_level"].Caption + ":" + DataTable.Rows[i]["ch_level"].ToString());
  2778. }
  2779. else if (columnNum > 14 && j == columnNum - 14)
  2780. {
  2781. row1.CreateCell(j);
  2782. row1.Cells[j].SetCellValue(DataTable.Columns["pi_inoutno"].Caption + ":" + DataTable.Rows[i]["pi_inoutno"].ToString());
  2783. }
  2784. else
  2785. {
  2786. row1.CreateCell(j);
  2787. }
  2788. row1.GetCell(j).CellStyle = style;
  2789. }
  2790. row1 = sheet.CreateRow(PaintIndex);
  2791. PaintIndex = PaintIndex + 1;
  2792. //第二行添加型号
  2793. for (int j = 0; j < columnNum - 3; j++)
  2794. {
  2795. if (j == 0)
  2796. {
  2797. row1.CreateCell(j);
  2798. row1.Cells[j].SetCellValue(DataTable.Columns["pr_orispeccode"].Caption + ":" + DataTable.Rows[i]["pr_orispeccode"].ToString() + " "
  2799. + "(" + DataTable.Rows[i]["me_desc"].ToString() + ") " + DataTable.Columns["pi_chipouttype"].Caption + ":" + DataTable.Rows[i]["pi_chipouttype"].ToString());
  2800. }
  2801. else if (j == 3)
  2802. {
  2803. row1.CreateCell(j);
  2804. row1.Cells[j].SetCellValue(DataTable.Columns["pr_size"].Caption + ":" + DataTable.Rows[i]["pr_size"].ToString());
  2805. }
  2806. else if (columnNum > 14 && j == columnNum - 14)
  2807. {
  2808. row1.CreateCell(j);
  2809. row1.Cells[j].SetCellValue(DataTable.Columns["pi_date"].Caption + ":" + DataTable.Rows[i]["pi_date"].ToString());
  2810. }
  2811. else
  2812. {
  2813. row1.CreateCell(j);
  2814. }
  2815. row1.GetCell(j).CellStyle = style;
  2816. }
  2817. //添加列名
  2818. row1 = sheet.CreateRow(PaintIndex);
  2819. PaintIndex = PaintIndex + 1;
  2820. //计数列所在的索引
  2821. int showcount = 0;
  2822. for (int j = 4; j < columnNum; j++)
  2823. {
  2824. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  2825. {
  2826. showcount = showcount + 1;
  2827. row1.CreateCell(j - 4);
  2828. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  2829. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  2830. }
  2831. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  2832. {
  2833. showcount = showcount + 1;
  2834. row1.CreateCell(j - 4);
  2835. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  2836. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  2837. }
  2838. else if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  2839. (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")))
  2840. {
  2841. }
  2842. else
  2843. {
  2844. showcount = showcount + 1;
  2845. row1.CreateCell(j - 4);
  2846. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  2847. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  2848. }
  2849. if (DataTable.Columns[j].ColumnName.ToString() == "io_qty")
  2850. {
  2851. NumIndex = j;
  2852. }
  2853. }
  2854. ShowColumnCount = showcount;
  2855. row1 = sheet.CreateRow(PaintIndex);
  2856. PaintIndex = PaintIndex + 1;
  2857. }
  2858. }
  2859. //添加数据内容
  2860. for (int j = 4; j < columnNum; j++)
  2861. {
  2862. string Data = DataTable.Rows[i][j].ToString();
  2863. row1.CreateCell(j - 4);
  2864. if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  2865. (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")))
  2866. {
  2867. }
  2868. else
  2869. {
  2870. row1.Cells[j - 4].SetCellValue(Data);
  2871. row1.GetCell(j - 4).CellStyle = styleborder;
  2872. }
  2873. if (DataTable.Columns[j].ColumnName == "io_qty")
  2874. {
  2875. row1.Cells[j - 4].SetCellValue(int.Parse(Data));
  2876. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  2877. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  2878. }
  2879. if (DataTable.Columns[j].ColumnName == "rownum")
  2880. {
  2881. row1.Cells[j - 4].SetCellValue(i + 1);
  2882. }
  2883. }
  2884. //固定行号分组的时候自动拼接新的DataTable
  2885. if (i == rowNum - 1)
  2886. {
  2887. DataRow dr = FirstDT.NewRow();
  2888. dr["pr_orispeccode"] = DataTable.Rows[i]["pr_orispeccode"].ToString();
  2889. dr["pi_inoutno"] = DataTable.Rows[i]["pi_inoutno"].ToString();
  2890. dr["pi_title"] = DataTable.Rows[i]["pi_title"].ToString();
  2891. dr["pi_date"] = DataTable.Rows[i]["pi_date"].ToString();
  2892. dr["pd_ordercode"] = First_OrderCode;
  2893. dr["pr_orispeccode"] = First_Prspec;
  2894. dr["ch_splitbatch"] = First_Batch;
  2895. dr["ch_waterid"] = BaseUtil.GetArrStr(First_WID, " ");
  2896. dr["num"] = (i % PageSize) + 1;
  2897. dr["io_qty"] = sumCount;
  2898. FirstDT.Rows.Add(dr);
  2899. row1 = sheet.CreateRow(PaintIndex);
  2900. PaintIndex = PaintIndex + 1;
  2901. for (int j = 0; j < ShowColumnCount; j++)
  2902. {
  2903. row1.CreateCell(j);
  2904. if (j == 0)
  2905. {
  2906. row1.Cells[j].SetCellValue(批小计);
  2907. }
  2908. else if (j == 1)
  2909. {
  2910. row1.Cells[1].SetCellValue(rowNum % PageSize + 片);
  2911. }
  2912. else if (j == 2)
  2913. {
  2914. row1.Cells[2].SetCellValue(sumCount);
  2915. }
  2916. row1.Cells[j].CellStyle = styleborder;
  2917. }
  2918. row1 = sheet.CreateRow(PaintIndex);
  2919. PaintIndex = PaintIndex + 1;
  2920. for (int j = 0; j < ShowColumnCount; j++)
  2921. {
  2922. row1.CreateCell(j);
  2923. if (j == 0)
  2924. {
  2925. row1.Cells[j].SetCellValue(小计);
  2926. }
  2927. else if (j == 1)
  2928. {
  2929. row1.Cells[1].SetCellValue(rowNum % PageSize + 片);
  2930. }
  2931. else if (j == 2)
  2932. {
  2933. row1.Cells[2].SetCellValue(sumCount);
  2934. }
  2935. row1.Cells[j].CellStyle = styleborder;
  2936. }
  2937. row1 = sheet.CreateRow(PaintIndex);
  2938. for (int j = 0; j < columnNum - 3; j++)
  2939. {
  2940. if (j == 0)
  2941. {
  2942. row1.CreateCell(j);
  2943. row1.Cells[j].SetCellValue("备注");
  2944. }
  2945. else if (j == 2)
  2946. {
  2947. row1.CreateCell(j);
  2948. row1.Cells[j].SetCellValue(totalCount);
  2949. }
  2950. else if (j == 3)
  2951. {
  2952. row1.CreateCell(j);
  2953. row1.Cells[j].SetCellValue(rowNum);
  2954. }
  2955. else if (j == 4)
  2956. {
  2957. row1.CreateCell(j);
  2958. row1.Cells[j].SetCellValue(片);
  2959. }
  2960. else
  2961. {
  2962. row1.CreateCell(j);
  2963. }
  2964. row1.Cells[j].CellStyle = style;
  2965. }
  2966. sheet.SetRowBreak(PaintIndex);
  2967. sheet.Footer.Center = "第&P页,共&N页";
  2968. PaintIndex = PaintIndex + 1;
  2969. }
  2970. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  2971. pib_outboxcode1.Add(BoxCode.ToString());
  2972. }
  2973. for (int i = 0; i < sheet.LastRowNum; i++)
  2974. {
  2975. if (i != 0)
  2976. {
  2977. sheet.AutoSizeColumn(i);
  2978. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  2979. }
  2980. }
  2981. break;
  2982. case "BatchCode":
  2983. string LastBatchCode = "";
  2984. int PageNum = 0;
  2985. for (int i = 0; i < rowNum; i++)
  2986. {
  2987. IRow row1 = sheet.CreateRow(PaintIndex);
  2988. PaintIndex = PaintIndex + 1;
  2989. row1.HeightInPoints = RowHeight;
  2990. //如果批号不相等的时候
  2991. PageNum = PageNum + 1;
  2992. if (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString())
  2993. {
  2994. BoxCode = BoxCode + 1;
  2995. for (int j = 0; j < ShowColumnCount; j++)
  2996. {
  2997. row1.CreateCell(j);
  2998. if (j == 0)
  2999. {
  3000. row1.Cells[j].SetCellValue(批小计);
  3001. row1.Cells[j].CellStyle = styleborder;
  3002. }
  3003. else if (j == 1)
  3004. {
  3005. row1.Cells[1].SetCellValue(PageNum - 1 + 片);
  3006. }
  3007. else if (j == 2)
  3008. {
  3009. row1.Cells[2].SetCellValue(sumCount);
  3010. row1.Cells[j].CellStyle = styleborder;
  3011. }
  3012. else
  3013. {
  3014. row1.Cells[j].CellStyle = styleborder;
  3015. }
  3016. }
  3017. row1 = sheet.CreateRow(PaintIndex);
  3018. PaintIndex = PaintIndex + 1;
  3019. for (int j = 0; j < ShowColumnCount; j++)
  3020. {
  3021. row1.CreateCell(j);
  3022. if (j == 0)
  3023. {
  3024. row1.Cells[j].SetCellValue(小计);
  3025. row1.Cells[j].CellStyle = styleborder;
  3026. }
  3027. else if (j == 1)
  3028. {
  3029. row1.Cells[1].SetCellValue(PageNum - 1 + 片);
  3030. }
  3031. else if (j == 2)
  3032. {
  3033. row1.Cells[2].SetCellValue(sumCount);
  3034. row1.Cells[j].CellStyle = styleborder;
  3035. }
  3036. else
  3037. {
  3038. row1.Cells[j].CellStyle = styleborder;
  3039. }
  3040. }
  3041. sumCount = 0;
  3042. row1 = sheet.CreateRow(PaintIndex);
  3043. sheet.SetRowBreak(PaintIndex - 1);
  3044. sheet.Footer.Center = "第&P页,共&N页";
  3045. PaintIndex = PaintIndex + 1;
  3046. PageNum = 1;
  3047. }
  3048. //每次到了页数开始分页
  3049. if (LastBatchCode == "" || (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["ch_splitbatch"].ToString()) || i == rowNum - 1)
  3050. {
  3051. LastBatchCode = DataTable.Rows[i]["ch_splitbatch"].ToString();
  3052. //第一行添加客户信息
  3053. //if (i != rowNum - 1)
  3054. {
  3055. for (int j = 0; j < columnNum - 3; j++)
  3056. {
  3057. if (j == 0)
  3058. {
  3059. row1.CreateCell(j);
  3060. row1.Cells[j].SetCellValue(DataTable.Columns["pi_title"].Caption + ":" + DataTable.Rows[i]["pi_title"].ToString() + " "
  3061. + DataTable.Columns["ch_level"].Caption + ":" + DataTable.Rows[i]["ch_level"].ToString());
  3062. }
  3063. else if (columnNum > 14 && j == columnNum - 14)
  3064. {
  3065. row1.CreateCell(j);
  3066. row1.Cells[j].SetCellValue(DataTable.Columns["pi_inoutno"].Caption + ":" + DataTable.Rows[i]["pi_inoutno"].ToString());
  3067. }
  3068. else
  3069. {
  3070. row1.CreateCell(j);
  3071. }
  3072. row1.GetCell(j).CellStyle = style;
  3073. }
  3074. row1 = sheet.CreateRow(PaintIndex);
  3075. PaintIndex = PaintIndex + 1;
  3076. //第二行添加型号
  3077. for (int j = 0; j < columnNum - 3; j++)
  3078. {
  3079. if (j == 0)
  3080. {
  3081. row1.CreateCell(j);
  3082. row1.Cells[j].SetCellValue(DataTable.Columns["pr_orispeccode"].Caption + ":" + DataTable.Rows[i]["pr_orispeccode"].ToString() + " "
  3083. + "(" + DataTable.Rows[i]["me_desc"].ToString() + ") " + DataTable.Columns["pi_chipouttype"].Caption + ":" + DataTable.Rows[i]["pi_chipouttype"].ToString());
  3084. }
  3085. else if (j == 3)
  3086. {
  3087. row1.CreateCell(j);
  3088. row1.Cells[j].SetCellValue(DataTable.Columns["pr_size"].Caption + ":" + DataTable.Rows[i]["pr_size"].ToString());
  3089. }
  3090. else if (columnNum > 14 && j == columnNum - 14)
  3091. {
  3092. row1.CreateCell(j);
  3093. row1.Cells[j].SetCellValue(DataTable.Columns["pi_date"].Caption + ":" + DataTable.Rows[i]["pi_date"].ToString());
  3094. }
  3095. else
  3096. {
  3097. row1.CreateCell(j);
  3098. }
  3099. row1.GetCell(j).CellStyle = style;
  3100. }
  3101. //添加列名
  3102. row1 = sheet.CreateRow(PaintIndex);
  3103. PaintIndex = PaintIndex + 1;
  3104. int showcount = 0;
  3105. for (int j = 4; j < columnNum; j++)
  3106. {
  3107. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  3108. {
  3109. showcount = showcount + 1;
  3110. row1.CreateCell(j - 4);
  3111. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  3112. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  3113. }
  3114. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  3115. {
  3116. showcount = showcount + 1;
  3117. row1.CreateCell(j - 4);
  3118. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  3119. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  3120. }
  3121. else if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  3122. (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")))
  3123. {
  3124. }
  3125. else
  3126. {
  3127. showcount = showcount + 1;
  3128. row1.CreateCell(j - 4);
  3129. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  3130. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  3131. }
  3132. }
  3133. ShowColumnCount = showcount;
  3134. row1 = sheet.CreateRow(PaintIndex);
  3135. PaintIndex = PaintIndex + 1;
  3136. }
  3137. }
  3138. //添加数据内容
  3139. for (int j = 4; j < columnNum; j++)
  3140. {
  3141. string Data = DataTable.Rows[i][j].ToString();
  3142. row1.CreateCell(j - 4);
  3143. if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  3144. (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")))
  3145. {
  3146. }
  3147. else
  3148. {
  3149. row1.Cells[j - 4].SetCellValue(Data);
  3150. row1.GetCell(j - 4).CellStyle = styleborder;
  3151. }
  3152. if (DataTable.Columns[j].ColumnName == "io_qty")
  3153. {
  3154. row1.Cells[j - 4].SetCellValue(int.Parse(Data));
  3155. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  3156. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  3157. }
  3158. if (DataTable.Columns[j].ColumnName == "rownum")
  3159. {
  3160. row1.Cells[j - 4].SetCellValue(i + 1);
  3161. }
  3162. }
  3163. if (i == rowNum - 1)
  3164. {
  3165. row1 = sheet.CreateRow(PaintIndex);
  3166. PaintIndex = PaintIndex + 1;
  3167. for (int j = 0; j < ShowColumnCount; j++)
  3168. {
  3169. row1.CreateCell(j);
  3170. if (j == 0)
  3171. {
  3172. row1.Cells[j].SetCellValue(批小计);
  3173. row1.Cells[j].CellStyle = styleborder;
  3174. }
  3175. else if (j == 1)
  3176. {
  3177. row1.Cells[1].SetCellValue(PageNum + 片);
  3178. }
  3179. else if (j == 2)
  3180. {
  3181. row1.Cells[2].SetCellValue(sumCount);
  3182. row1.Cells[j].CellStyle = styleborder;
  3183. }
  3184. else
  3185. {
  3186. row1.Cells[j].CellStyle = styleborder;
  3187. }
  3188. }
  3189. row1 = sheet.CreateRow(PaintIndex);
  3190. PaintIndex = PaintIndex + 1;
  3191. for (int j = 0; j < ShowColumnCount; j++)
  3192. {
  3193. row1.CreateCell(j);
  3194. if (j == 0)
  3195. {
  3196. row1.Cells[j].SetCellValue(小计);
  3197. row1.Cells[j].CellStyle = styleborder;
  3198. }
  3199. else if (j == 1)
  3200. {
  3201. row1.Cells[1].SetCellValue(PageNum + 片);
  3202. }
  3203. else if (j == 2)
  3204. {
  3205. row1.Cells[2].SetCellValue(sumCount);
  3206. row1.Cells[j].CellStyle = styleborder;
  3207. }
  3208. else
  3209. {
  3210. row1.Cells[j].CellStyle = styleborder;
  3211. }
  3212. }
  3213. //创建备注内容
  3214. row1 = sheet.CreateRow(PaintIndex);
  3215. for (int j = 0; j < columnNum - 3; j++)
  3216. {
  3217. if (j == 0)
  3218. {
  3219. row1.CreateCell(j);
  3220. row1.Cells[j].SetCellValue("备注");
  3221. }
  3222. else if (j == 2)
  3223. {
  3224. row1.CreateCell(j);
  3225. row1.Cells[j].SetCellValue(totalCount);
  3226. }
  3227. else if (j == 3)
  3228. {
  3229. row1.CreateCell(j);
  3230. row1.Cells[j].SetCellValue(rowNum);
  3231. }
  3232. else if (j == 4)
  3233. {
  3234. row1.CreateCell(j);
  3235. row1.Cells[j].SetCellValue(片);
  3236. }
  3237. else
  3238. {
  3239. row1.CreateCell(j);
  3240. }
  3241. row1.Cells[j].CellStyle = style;
  3242. }
  3243. sheet.SetRowBreak(PaintIndex);
  3244. sheet.Footer.Center = "第&P页,共&N页";
  3245. PaintIndex = PaintIndex + 1;
  3246. PageNum = 1;
  3247. }
  3248. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  3249. pib_outboxcode1.Add(BoxCode.ToString());
  3250. }
  3251. for (int i = 0; i < sheet.LastRowNum; i++)
  3252. {
  3253. if (i != 0)
  3254. {
  3255. sheet.AutoSizeColumn(i);
  3256. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  3257. }
  3258. }
  3259. break;
  3260. case "BoxCode":
  3261. string LastBoxCode = "";
  3262. int PageNum1 = 0;
  3263. for (int i = 0; i < rowNum; i++)
  3264. {
  3265. IRow row1 = sheet.CreateRow(PaintIndex);
  3266. PaintIndex = PaintIndex + 1;
  3267. row1.HeightInPoints = RowHeight;
  3268. PageNum1 = PageNum1 + 1;
  3269. //如果批号不相等的时候
  3270. if (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["CH_PBCODE"].ToString())
  3271. {
  3272. BoxCode = BoxCode + 1;
  3273. for (int j = 0; j < ShowColumnCount; j++)
  3274. {
  3275. row1.CreateCell(j);
  3276. if (j == 0)
  3277. {
  3278. row1.Cells[j].SetCellValue(批小计);
  3279. row1.Cells[j].CellStyle = styleborder;
  3280. }
  3281. else if (j == 1)
  3282. {
  3283. row1.Cells[1].SetCellValue(PageNum1 + 片);
  3284. }
  3285. else if (j == 2)
  3286. {
  3287. row1.Cells[2].SetCellValue(sumCount);
  3288. row1.Cells[j].CellStyle = styleborder;
  3289. }
  3290. else
  3291. {
  3292. row1.Cells[j].CellStyle = styleborder;
  3293. }
  3294. }
  3295. row1 = sheet.CreateRow(PaintIndex);
  3296. PaintIndex = PaintIndex + 1;
  3297. for (int j = 0; j < ShowColumnCount; j++)
  3298. {
  3299. row1.CreateCell(j);
  3300. if (j == 0)
  3301. {
  3302. row1.Cells[j].SetCellValue(小计);
  3303. row1.Cells[j].CellStyle = styleborder;
  3304. }
  3305. else if (j == 1)
  3306. {
  3307. row1.Cells[1].SetCellValue(PageNum1 + 片);
  3308. }
  3309. else if (j == 2)
  3310. {
  3311. row1.Cells[2].SetCellValue(sumCount);
  3312. row1.Cells[j].CellStyle = styleborder;
  3313. }
  3314. else
  3315. {
  3316. row1.Cells[j].CellStyle = styleborder;
  3317. }
  3318. }
  3319. sumCount = 0;
  3320. row1 = sheet.CreateRow(PaintIndex);
  3321. sheet.SetRowBreak(PaintIndex - 1);
  3322. sheet.Footer.Center = "第&P页,共&N页";
  3323. PaintIndex = PaintIndex + 1;
  3324. }
  3325. //每次到了页数开始分页
  3326. if (LastBoxCode == "" || (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["ch_pbcode"].ToString()) || i == rowNum - 1)
  3327. {
  3328. LastBoxCode = DataTable.Rows[i]["CH_PBCODE"].ToString();
  3329. //第一行添加客户信息
  3330. //if (i != rowNum - 1)
  3331. {
  3332. for (int j = 0; j < columnNum - 3; j++)
  3333. {
  3334. if (j == 0)
  3335. {
  3336. row1.CreateCell(j);
  3337. row1.Cells[j].SetCellValue(DataTable.Columns["pi_title"].Caption + ":" + DataTable.Rows[i]["pi_title"].ToString() + " "
  3338. + DataTable.Columns["ch_level"].Caption + ":" + DataTable.Rows[i]["ch_level"].ToString());
  3339. }
  3340. else if (columnNum > 14 && j == columnNum - 14)
  3341. {
  3342. row1.CreateCell(j);
  3343. row1.Cells[j].SetCellValue(DataTable.Columns["pi_inoutno"].Caption + ":" + DataTable.Rows[i]["pi_inoutno"].ToString());
  3344. }
  3345. else
  3346. {
  3347. row1.CreateCell(j);
  3348. }
  3349. row1.GetCell(j).CellStyle = style;
  3350. }
  3351. row1 = sheet.CreateRow(PaintIndex);
  3352. PaintIndex = PaintIndex + 1;
  3353. //第二行添加型号
  3354. for (int j = 0; j < columnNum - 3; j++)
  3355. {
  3356. if (j == 0)
  3357. {
  3358. row1.CreateCell(j);
  3359. row1.Cells[j].SetCellValue(DataTable.Columns["pr_orispeccode"].Caption + ":" + DataTable.Rows[i]["pr_orispeccode"].ToString() + " "
  3360. + "(" + DataTable.Rows[i]["me_desc"].ToString() + ") " + DataTable.Columns["pi_chipouttype"].Caption + ":" + DataTable.Rows[i]["pi_chipouttype"].ToString());
  3361. }
  3362. else if (j == 3)
  3363. {
  3364. row1.CreateCell(j);
  3365. row1.Cells[j].SetCellValue(DataTable.Columns["pr_size"].Caption + ":" + DataTable.Rows[i]["pr_size"].ToString());
  3366. }
  3367. else if (columnNum > 14 && j == columnNum - 14)
  3368. {
  3369. row1.CreateCell(j);
  3370. row1.Cells[j].SetCellValue(DataTable.Columns["pi_date"].Caption + ":" + DataTable.Rows[i]["pi_date"].ToString());
  3371. }
  3372. else
  3373. {
  3374. row1.CreateCell(j);
  3375. }
  3376. row1.GetCell(j).CellStyle = style;
  3377. }
  3378. //添加列名
  3379. row1 = sheet.CreateRow(PaintIndex);
  3380. PaintIndex = PaintIndex + 1;
  3381. int showcount = 0;
  3382. for (int j = 4; j < columnNum; j++)
  3383. {
  3384. if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
  3385. {
  3386. showcount = showcount + 1;
  3387. row1.CreateCell(j - 4);
  3388. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  3389. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
  3390. }
  3391. else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
  3392. {
  3393. showcount = showcount + 1;
  3394. row1.CreateCell(j - 4);
  3395. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  3396. row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
  3397. }
  3398. else if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  3399. (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")))
  3400. {
  3401. }
  3402. else
  3403. {
  3404. showcount = showcount + 1;
  3405. row1.CreateCell(j - 4);
  3406. row1.Cells[j - 4].CellStyle = ColumnTitleStyle;
  3407. row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].Caption);
  3408. }
  3409. }
  3410. ShowColumnCount = showcount;
  3411. row1 = sheet.CreateRow(PaintIndex);
  3412. PaintIndex = PaintIndex + 1;
  3413. }
  3414. }
  3415. //添加数据内容
  3416. for (int j = 4; j < columnNum; j++)
  3417. {
  3418. string Data = DataTable.Rows[i][j].ToString();
  3419. row1.CreateCell(j - 4);
  3420. if ((DataTable.Rows[i]["chw_itemname1"].ToString() == "" && DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1") ||
  3421. (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")))
  3422. {
  3423. }
  3424. else
  3425. {
  3426. row1.Cells[j - 4].SetCellValue(Data);
  3427. row1.GetCell(j - 4).CellStyle = styleborder;
  3428. }
  3429. if (DataTable.Columns[j].ColumnName == "io_qty")
  3430. {
  3431. row1.Cells[j - 4].SetCellValue(int.Parse(Data));
  3432. sumCount += int.Parse(DataTable.Rows[i][j].ToString());
  3433. totalCount += int.Parse(DataTable.Rows[i][j].ToString());
  3434. }
  3435. if (DataTable.Columns[j].ColumnName == "rownum")
  3436. {
  3437. row1.Cells[j - 4].SetCellValue(i + 1);
  3438. }
  3439. }
  3440. if (i == rowNum - 1)
  3441. {
  3442. row1 = sheet.CreateRow(PaintIndex);
  3443. PaintIndex = PaintIndex + 1;
  3444. for (int j = 0; j < ShowColumnCount; j++)
  3445. {
  3446. row1.CreateCell(j);
  3447. if (j == 0)
  3448. {
  3449. row1.Cells[j].SetCellValue(批小计);
  3450. row1.Cells[j].CellStyle = styleborder;
  3451. }
  3452. else if (j == 1)
  3453. {
  3454. row1.Cells[1].SetCellValue(PageNum1 + 片);
  3455. }
  3456. else if (j == 2)
  3457. {
  3458. row1.Cells[2].SetCellValue(sumCount);
  3459. row1.Cells[j].CellStyle = styleborder;
  3460. }
  3461. else
  3462. {
  3463. row1.Cells[j].CellStyle = styleborder;
  3464. }
  3465. }
  3466. row1 = sheet.CreateRow(PaintIndex);
  3467. PaintIndex = PaintIndex + 1;
  3468. for (int j = 0; j < ShowColumnCount; j++)
  3469. {
  3470. row1.CreateCell(j);
  3471. if (j == 0)
  3472. {
  3473. row1.Cells[j].SetCellValue(小计);
  3474. row1.Cells[j].CellStyle = styleborder;
  3475. }
  3476. else if (j == 1)
  3477. {
  3478. row1.Cells[1].SetCellValue(PageNum1 + 片);
  3479. }
  3480. else if (j == 2)
  3481. {
  3482. row1.Cells[2].SetCellValue(sumCount);
  3483. row1.Cells[j].CellStyle = styleborder;
  3484. }
  3485. else
  3486. {
  3487. row1.Cells[j].CellStyle = styleborder;
  3488. }
  3489. }
  3490. row1 = sheet.CreateRow(PaintIndex);
  3491. for (int j = 0; j < columnNum - 3; j++)
  3492. {
  3493. if (j == 0)
  3494. {
  3495. row1.CreateCell(j);
  3496. row1.Cells[j].SetCellValue("备注");
  3497. }
  3498. else if (j == 2)
  3499. {
  3500. row1.CreateCell(j);
  3501. row1.Cells[j].SetCellValue(totalCount);
  3502. }
  3503. else if (j == 3)
  3504. {
  3505. row1.CreateCell(j);
  3506. row1.Cells[j].SetCellValue(rowNum);
  3507. }
  3508. else if (j == 4)
  3509. {
  3510. row1.CreateCell(j);
  3511. row1.Cells[j].SetCellValue(片);
  3512. }
  3513. else
  3514. {
  3515. row1.CreateCell(j);
  3516. }
  3517. row1.Cells[j].CellStyle = style;
  3518. }
  3519. sheet.SetRowBreak(PaintIndex);
  3520. sheet.Footer.Center = "第&P页,共&N页";
  3521. PaintIndex = PaintIndex + 1;
  3522. PageNum1 = 1;
  3523. }
  3524. pib_id.Add(DataTable.Rows[i]["pib_id"].ToString());
  3525. pib_outboxcode1.Add(BoxCode.ToString());
  3526. }
  3527. for (int i = 0; i < sheet.LastRowNum; i++)
  3528. {
  3529. if (i != 0)
  3530. {
  3531. sheet.AutoSizeColumn(i);
  3532. sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
  3533. }
  3534. }
  3535. break;
  3536. default:
  3537. break;
  3538. }
  3539. 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());
  3540. //删除下载链接再重新插入
  3541. HttpHandler.GenDownLoadLinK(Inoutno);
  3542. //填充首页
  3543. sumCount = 0;
  3544. totalCount = 0;
  3545. PaintIndex = 1;
  3546. ISheet sheet2 = book.CreateSheet("首页");
  3547. row = sheet2.CreateRow(0);
  3548. row.CreateCell(0);
  3549. row.Cells[0].SetCellValue(" " + companyname);
  3550. row.GetCell(0).CellStyle = style;
  3551. rowNum = FirstDT.Rows.Count;
  3552. //不需要显示的列移除
  3553. for (int i = FirstDT.Columns.Count - 1; i > 0; i--)
  3554. {
  3555. for (int j = 0; j < box.Length; j++)
  3556. {
  3557. if (box[j].Name == "FirstPage_WID" && !box[j].Checked)
  3558. {
  3559. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
  3560. {
  3561. FirstDT.Columns.RemoveAt(i);
  3562. }
  3563. }
  3564. if (box[j].Name == "FirstPage_YIELD" && !box[j].Checked)
  3565. {
  3566. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_yeild"))
  3567. {
  3568. FirstDT.Columns.RemoveAt(i);
  3569. }
  3570. }
  3571. if (box[j].Name == "FirstPage_REMARK" && !box[j].Checked)
  3572. {
  3573. if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_remark"))
  3574. {
  3575. FirstDT.Columns.RemoveAt(i);
  3576. }
  3577. }
  3578. }
  3579. }
  3580. columnNum = FirstDT.Columns.Count;
  3581. for (int i = 0; i < rowNum; i++)
  3582. {
  3583. IRow row1 = sheet2.CreateRow(PaintIndex);
  3584. PaintIndex = PaintIndex + 1;
  3585. row1.HeightInPoints = RowHeight;
  3586. //只需要绘制一行
  3587. if (i == 0)
  3588. {
  3589. for (int j = 0; j < columnNum - 3; j++)
  3590. {
  3591. if (j == 0)
  3592. {
  3593. row1.CreateCell(j);
  3594. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_title"].Caption + ":" + FirstDT.Rows[i]["pi_title"].ToString());
  3595. }
  3596. else if (j > 5 && j == columnNum - 5)
  3597. {
  3598. row1.CreateCell(j);
  3599. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_inoutno"].Caption + ":" + FirstDT.Rows[i]["pi_inoutno"].ToString());
  3600. }
  3601. else if (columnNum > 5 && j == columnNum - 5)
  3602. {
  3603. row1.CreateCell(j);
  3604. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_inoutno"].Caption + ":" + FirstDT.Rows[i]["pi_inoutno"].ToString());
  3605. }
  3606. else
  3607. {
  3608. row1.CreateCell(j);
  3609. }
  3610. row1.GetCell(j).CellStyle = style;
  3611. }
  3612. row1 = sheet2.CreateRow(PaintIndex);
  3613. PaintIndex = PaintIndex + 1;
  3614. //第二行添加型号
  3615. for (int j = 0; j < columnNum - 3; j++)
  3616. {
  3617. if (j == 0)
  3618. {
  3619. row1.CreateCell(j);
  3620. row1.Cells[j].SetCellValue(FirstDT.Columns["pr_orispeccode"].Caption + ":" + FirstDT.Rows[i]["pr_orispeccode"].ToString());
  3621. }
  3622. else if (j > 5 && j == columnNum - 5)
  3623. {
  3624. row1.CreateCell(j);
  3625. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_date"].Caption + ":" + FirstDT.Rows[i]["pi_date"].ToString());
  3626. }
  3627. else if (columnNum > 5 && j == columnNum - 5)
  3628. {
  3629. row1.CreateCell(j);
  3630. row1.Cells[j].SetCellValue(FirstDT.Columns["pi_date"].Caption + ":" + FirstDT.Rows[i]["pi_date"].ToString());
  3631. }
  3632. else
  3633. {
  3634. row1.CreateCell(j);
  3635. }
  3636. row1.GetCell(j).CellStyle = style;
  3637. }
  3638. row1 = sheet2.CreateRow(PaintIndex);
  3639. PaintIndex = PaintIndex + 1;
  3640. //添加列名
  3641. for (int j = 4; j < columnNum; j++)
  3642. {
  3643. row1.CreateCell(j - 4);
  3644. row1.Cells[j - 4].CellStyle = styleborder;
  3645. row1.Cells[j - 4].SetCellValue(FirstDT.Columns[j].Caption);
  3646. }
  3647. row1 = sheet2.CreateRow(PaintIndex);
  3648. PaintIndex = PaintIndex + 1;
  3649. }
  3650. //添加数据内容
  3651. for (int j = 4; j < columnNum; j++)
  3652. {
  3653. string Data = FirstDT.Rows[i][j].ToString();
  3654. row1.CreateCell(j - 4);
  3655. row1.Cells[j - 4].SetCellValue(Data);
  3656. row1.GetCell(j - 4).CellStyle = styleborder;
  3657. if (FirstDT.Columns[j].ColumnName == "num")
  3658. {
  3659. sumCount += int.Parse(Data);
  3660. }
  3661. if (FirstDT.Columns[j].ColumnName == "io_qty")
  3662. {
  3663. totalCount += int.Parse(Data);
  3664. }
  3665. }
  3666. //添加总计行
  3667. if (i == rowNum - 1)
  3668. {
  3669. row1 = sheet2.CreateRow(PaintIndex);
  3670. PaintIndex = PaintIndex + 1;
  3671. for (int j = 0; j < columnNum - 4; j++)
  3672. {
  3673. if (j == 0)
  3674. {
  3675. row1.CreateCell(j);
  3676. row1.Cells[j].CellStyle = styleborder;
  3677. row1.Cells[j].SetCellValue(总计);
  3678. }
  3679. else if (j == columnNum - 6)
  3680. {
  3681. row1.CreateCell(j);
  3682. row1.Cells[j].CellStyle = styleborder;
  3683. row1.Cells[j].SetCellValue(sumCount);
  3684. }
  3685. else if (j == columnNum - 5)
  3686. {
  3687. row1.CreateCell(j);
  3688. row1.Cells[j].CellStyle = styleborder;
  3689. row1.Cells[j].SetCellValue(totalCount);
  3690. }
  3691. else
  3692. {
  3693. row1.CreateCell(j);
  3694. row1.Cells[j].CellStyle = styleborder;
  3695. }
  3696. }
  3697. }
  3698. }
  3699. for (int i = 0; i < sheet2.LastRowNum; i++)
  3700. {
  3701. if (i != 0)
  3702. {
  3703. sheet2.AutoSizeColumn(i);
  3704. sheet2.SetColumnWidth(i, sheet2.GetColumnWidth(i) + 1000);
  3705. }
  3706. }
  3707. //将book的内容写入内存流中返回
  3708. book.Write(ms);
  3709. return ms;
  3710. }
  3711. }
  3712. }