ExcelHandler.cs 200 KB

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