ExcelHandler.cs 198 KB

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