ExcelHandler.cs 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. using System.IO;
  2. using System.Data;
  3. using System;
  4. using NPOI.HSSF.UserModel;
  5. using NPOI.SS.UserModel;
  6. using NPOI.HSSF.Util;
  7. using NPOI.SS.Formula.Eval;
  8. using System.Text;
  9. using NPOI.SS.Util;
  10. using System.Drawing;
  11. using System.Collections.Generic;
  12. using Seagull.BarTender.Print;
  13. using DevExpress.XtraPrinting.Native.LayoutAdjustment;
  14. using DevExpress.XtraExport.Implementation;
  15. using NPOI.HSSF.Record.CF;
  16. namespace UAS_MES_NEW.DataOperate
  17. {
  18. class ExcelHandler
  19. {
  20. DataHelper dh = new DataHelper();
  21. /// <summary>
  22. /// 导出Excel,返回文件在客户端的路径
  23. /// </summary>
  24. public string ExportExcel(DataTable dt, string FolderPath)
  25. {
  26. //创建一个内存流,用来接收转换成Excel的内容
  27. MemoryStream ms;
  28. ms = DataTableToExcel(dt);
  29. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  30. string filePath = FolderPath;
  31. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  32. byte[] data = ms.ToArray();
  33. fs.Write(data, 0, data.Length);
  34. fs.Flush();
  35. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  36. ms.Dispose();
  37. fs.Dispose();
  38. return filePath;
  39. }
  40. public string ExportExcel_LIANGAN(DataTable dt, string FolderPath)
  41. {
  42. //创建一个内存流,用来接收转换成Excel的内容
  43. MemoryStream ms;
  44. ms = DataTableToExcel_LIANGAN(dt, FolderPath);
  45. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  46. string filePath = FolderPath;
  47. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  48. byte[] data = ms.ToArray();
  49. fs.Write(data, 0, data.Length);
  50. fs.Flush();
  51. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  52. ms.Dispose();
  53. fs.Dispose();
  54. return filePath;
  55. }
  56. public MemoryStream DataTableToExcel_LIANGAN(DataTable DataTable, string FolderPath)
  57. {
  58. //创建内存流
  59. MemoryStream ms = new MemoryStream();
  60. //创建一个Book,相当于一个Excel文件
  61. HSSFWorkbook book = new HSSFWorkbook();
  62. ICellStyle style = book.CreateCellStyle();
  63. style.BorderTop = BorderStyle.THIN;
  64. style.BorderLeft = BorderStyle.THIN;
  65. style.BorderRight = BorderStyle.THIN;
  66. style.BorderBottom = BorderStyle.THIN;
  67. //Excel中的Sheet
  68. //获取行数量和列数量
  69. int rowNum = DataTable.Rows.Count;
  70. int columnNum = DataTable.Columns.Count;
  71. //设置列的宽度,根据首行的列的内容的长度来设置
  72. string SheetName = "";
  73. //表格数据游标
  74. int DataRowCount = 8;
  75. string sumqty = "";
  76. for (int i = 0; i < 10; i++)
  77. {
  78. if (sumqty == "")
  79. sumqty = DataTable.Rows[i]["sumqty"].ToString();
  80. }
  81. for (int i = 0; i < DataTable.Rows.Count; i++)
  82. {
  83. if (DataTable.Rows[i]["STF_MACHINE"].ToString() != "" && DataTable.Rows[i]["STF_MACHINE"].ToString() != "***" && i != DataTable.Rows.Count - 1)
  84. {
  85. Console.WriteLine(DataTable.Rows[i]["STF_MACHINE"].ToString());
  86. Console.WriteLine(DataTable.Rows[i + 1]["STF_MACHINE"].ToString());
  87. ISheet sheet1 = book.CreateSheet(DataTable.Rows[i]["STF_MACHINE"].ToString() + "-" + DataTable.Rows[i + 1]["STF_MACHINE"].ToString().Substring(0, 1));
  88. SheetName = DataTable.Rows[i]["STF_MACHINE"].ToString() + "-" + DataTable.Rows[i + 1]["STF_MACHINE"].ToString().Substring(0, 1);
  89. IRow row1 = sheet1.CreateRow(0);
  90. row1.CreateCell(0); row1.CreateCell(1); row1.Cells[0].SetCellValue(" SMT程式料表 ");
  91. row1.Cells[0].CellStyle = style; row1.Cells[1].CellStyle = style;
  92. row1.CreateCell(2); row1.CreateCell(3);
  93. row1.Cells[2].CellStyle = style;
  94. row1.Cells[2].CellStyle = style; row1.Cells[3].CellStyle = style;
  95. row1.CreateCell(4); row1.CreateCell(5); row1.Cells[4].SetCellValue("点数:" + sumqty); row1.Cells[4].CellStyle = style;
  96. row1.Cells[4].CellStyle = style; row1.Cells[5].CellStyle = style;
  97. row1.CreateCell(6); row1.CreateCell(7); row1.Cells[6].CellStyle = style;
  98. row1.Cells[6].CellStyle = style; row1.Cells[7].CellStyle = style;
  99. //客户抬头
  100. IRow row2 = sheet1.CreateRow(1);
  101. CellRangeAddress cellRangeAddress = new CellRangeAddress(1, 1, 0, 1);
  102. CellRangeAddress cellRangeAddress1 = new CellRangeAddress(1, 1, 2, 3);
  103. CellRangeAddress cellRangeAddress2 = new CellRangeAddress(1, 1, 4, 5);
  104. CellRangeAddress cellRangeAddress3 = new CellRangeAddress(1, 1, 6, 7);
  105. sheet1.AddMergedRegion(cellRangeAddress);
  106. sheet1.AddMergedRegion(cellRangeAddress1);
  107. sheet1.AddMergedRegion(cellRangeAddress2);
  108. sheet1.AddMergedRegion(cellRangeAddress3);
  109. row2.CreateCell(0); row2.CreateCell(1); row2.Cells[0].SetCellValue("客户名称:");
  110. row2.Cells[0].CellStyle = style; row2.Cells[1].CellStyle = style;
  111. row2.CreateCell(2); row2.CreateCell(3); row2.Cells[2].SetCellValue("CY"); row2.Cells[2].CellStyle = style;
  112. row2.Cells[2].CellStyle = style; row2.Cells[3].CellStyle = style;
  113. row2.CreateCell(4); row2.CreateCell(5); row2.Cells[4].SetCellValue("文件编号://"); row2.Cells[4].CellStyle = style;
  114. row2.Cells[4].CellStyle = style; row2.Cells[5].CellStyle = style;
  115. row2.CreateCell(6); row2.CreateCell(7); row2.Cells[6].SetCellValue("发行日期:" + DateTime.Now.ToString("yyyy-MM-dd")); row2.Cells[6].CellStyle = style;
  116. row2.Cells[6].CellStyle = style; row2.Cells[7].CellStyle = style;
  117. //产品型号
  118. IRow row3 = sheet1.CreateRow(2);
  119. CellRangeAddress cellRangeAddress4 = new CellRangeAddress(2, 2, 0, 1);
  120. CellRangeAddress cellRangeAddress5 = new CellRangeAddress(2, 2, 2, 3);
  121. CellRangeAddress cellRangeAddress6 = new CellRangeAddress(2, 2, 4, 5);
  122. CellRangeAddress cellRangeAddress7 = new CellRangeAddress(2, 2, 6, 7);
  123. sheet1.AddMergedRegion(cellRangeAddress4);
  124. sheet1.AddMergedRegion(cellRangeAddress5);
  125. sheet1.AddMergedRegion(cellRangeAddress6);
  126. sheet1.AddMergedRegion(cellRangeAddress7);
  127. row3.CreateCell(0); row3.CreateCell(1); row3.Cells[0].SetCellValue("产品型号:");
  128. row3.Cells[0].CellStyle = style; row3.Cells[1].CellStyle = style;
  129. row3.CreateCell(2); row3.CreateCell(3); row3.Cells[2].SetCellValue("it9220-LS2404-B1-V2.0-MB");
  130. row3.Cells[2].CellStyle = style; row3.Cells[3].CellStyle = style;
  131. row3.CreateCell(4); row3.CreateCell(5); row3.Cells[4].SetCellValue("BOM编号:EN-CY-BOM-143(A0)");
  132. row3.Cells[4].CellStyle = style; row3.Cells[5].CellStyle = style;
  133. row3.CreateCell(6); row3.CreateCell(7); row3.Cells[6].SetCellValue("生效日期:" + DateTime.Now.ToString("yyyy-MM-dd"));
  134. row3.Cells[6].CellStyle = style; row3.Cells[7].CellStyle = style;
  135. //程序名称
  136. IRow row4 = sheet1.CreateRow(3);
  137. CellRangeAddress cellRangeAddress8 = new CellRangeAddress(3, 3, 0, 1);
  138. CellRangeAddress cellRangeAddress9 = new CellRangeAddress(3, 3, 2, 3);
  139. CellRangeAddress cellRangeAddress10 = new CellRangeAddress(3, 3, 4, 5);
  140. sheet1.AddMergedRegion(cellRangeAddress8);
  141. sheet1.AddMergedRegion(cellRangeAddress9);
  142. sheet1.AddMergedRegion(cellRangeAddress10);
  143. row4.CreateCell(0); row4.CreateCell(1); row4.Cells[0].SetCellValue("程序名称\t\r\n");
  144. row4.Cells[0].CellStyle = style; row4.Cells[1].CellStyle = style;
  145. row4.CreateCell(2); row4.CreateCell(3); row4.Cells[2].SetCellValue(DataTable.Rows[i]["STF_CODE"].ToString() + "\t\r\n");
  146. row4.Cells[2].CellStyle = style; row4.Cells[3].CellStyle = style;
  147. row4.CreateCell(4); row4.CreateCell(5); row4.Cells[4].SetCellValue("面别: AB面\t\r\n");
  148. row4.Cells[4].CellStyle = style; row4.Cells[5].CellStyle = style;
  149. //机器名称
  150. IRow row5 = sheet1.CreateRow(4);
  151. CellRangeAddress cellRangeAddress14 = new CellRangeAddress(5, 5, 0, 1);
  152. CellRangeAddress cellRangeAddress15 = new CellRangeAddress(5, 5, 2, 3);
  153. CellRangeAddress cellRangeAddress16 = new CellRangeAddress(5, 5, 4, 5);
  154. CellRangeAddress cellRangeAddress20 = new CellRangeAddress(5, 6, 6, 7);
  155. sheet1.AddMergedRegion(cellRangeAddress20);
  156. sheet1.AddMergedRegion(cellRangeAddress14);
  157. sheet1.AddMergedRegion(cellRangeAddress15);
  158. sheet1.AddMergedRegion(cellRangeAddress16);
  159. sheet1.AddMergedRegion(cellRangeAddress20);
  160. row5.CreateCell(0); row5.CreateCell(1); row5.Cells[0].SetCellValue("机器名称\t\r\n");
  161. row5.Cells[0].CellStyle = style; row5.Cells[1].CellStyle = style;
  162. row5.CreateCell(2); row5.CreateCell(3); row5.Cells[2].SetCellValue("TX2I+TX2I+TX2I+TX2+SX1\t\r\n");
  163. row5.Cells[2].CellStyle = style; row5.Cells[3].CellStyle = style;
  164. row5.CreateCell(4); row5.CreateCell(5); row5.Cells[4].SetCellValue("QA确认:\t\r\n");
  165. row5.Cells[4].CellStyle = style; row5.Cells[5].CellStyle = style;
  166. row5.CreateCell(6); row5.CreateCell(7); row5.Cells[6].SetCellValue("文控签章:");
  167. row5.Cells[6].CellStyle = style; row5.Cells[7].CellStyle = style;
  168. //制作
  169. IRow row6 = sheet1.CreateRow(5);
  170. CellRangeAddress cellRangeAddress17 = new CellRangeAddress(6, 6, 0, 1);
  171. CellRangeAddress cellRangeAddress18 = new CellRangeAddress(6, 6, 2, 3);
  172. CellRangeAddress cellRangeAddress19 = new CellRangeAddress(6, 6, 4, 5);
  173. sheet1.AddMergedRegion(cellRangeAddress17);
  174. sheet1.AddMergedRegion(cellRangeAddress18);
  175. sheet1.AddMergedRegion(cellRangeAddress19);
  176. row6.CreateCell(0); row6.CreateCell(1); row6.Cells[0].SetCellValue("制作:");
  177. row6.Cells[0].CellStyle = style; row6.Cells[1].CellStyle = style;
  178. row6.CreateCell(2); row6.CreateCell(3); row6.Cells[2].SetCellValue("贺瑞华");
  179. row6.Cells[2].CellStyle = style; row6.Cells[3].CellStyle = style;
  180. row6.CreateCell(4); row6.CreateCell(5); row6.Cells[4].SetCellValue("QA审核:");
  181. row6.Cells[4].CellStyle = style; row6.Cells[5].CellStyle = style;
  182. //制作
  183. IRow row7 = sheet1.CreateRow(6);
  184. CellRangeAddress cellRangeAddress21 = new CellRangeAddress(6, 6, 0, 7);
  185. sheet1.AddMergedRegion(cellRangeAddress21);
  186. row7.CreateCell(0); row7.CreateCell(1); row7.Cells[0].SetCellValue(DataTable.Rows[i]["STF_MACHINE"].ToString() + "-" + DataTable.Rows[i + 1]["STF_MACHINE"].ToString().Substring(0, 1));
  187. IRow row8 = sheet1.CreateRow(7);
  188. row8.CreateCell(0);
  189. row8.Cells[0].SetCellValue("站位");
  190. row8.Cells[0].CellStyle = style;
  191. row8.CreateCell(1);
  192. row8.Cells[1].SetCellValue("Feeder类型");
  193. row8.Cells[1].CellStyle = style;
  194. row8.CreateCell(2);
  195. row8.Cells[2].SetCellValue("位置");
  196. row8.Cells[2].CellStyle = style;
  197. row8.CreateCell(3);
  198. row8.Cells[3].SetCellValue("物料编码");
  199. row8.Cells[3].CellStyle = style;
  200. row8.CreateCell(4);
  201. row8.Cells[4].SetCellValue("物料描述");
  202. row8.Cells[4].CellStyle = style;
  203. row8.CreateCell(5);
  204. row8.Cells[5].SetCellValue("用量");
  205. row8.Cells[5].CellStyle = style;
  206. row8.CreateCell(6);
  207. row8.Cells[6].SetCellValue("位号");
  208. row8.Cells[6].CellStyle = style;
  209. row8.CreateCell(7);
  210. row8.Cells[7].SetCellValue("备注");
  211. row8.Cells[7].CellStyle = style;
  212. i = i + 1;
  213. DataRowCount = 8;
  214. sheet1.SetColumnWidth(4, sheet1.GetColumnWidth(4) + 3500);
  215. }
  216. else
  217. {
  218. if (SheetName != "")
  219. {
  220. ISheet sheet = book.GetSheet(SheetName);
  221. IRow row = sheet.CreateRow(DataRowCount);
  222. row.CreateCell(0);
  223. row.CreateCell(1);
  224. row.CreateCell(2);
  225. row.CreateCell(3);
  226. row.CreateCell(4);
  227. row.CreateCell(5);
  228. row.CreateCell(6);
  229. row.CreateCell(7);
  230. //位置编号
  231. CellRangeAddress cellRangeAddress = new CellRangeAddress(DataRowCount, DataRowCount + 1, 0, 0);
  232. sheet.AddMergedRegion(cellRangeAddress);
  233. //飞达规格
  234. CellRangeAddress cellRangeAddress1 = new CellRangeAddress(DataRowCount, DataRowCount + 1, 1, 1);
  235. sheet.AddMergedRegion(cellRangeAddress1);
  236. row.Cells[0].SetCellValue(DataTable.Rows[i]["STF_MODEL"].ToString());
  237. row.Cells[0].CellStyle = style;
  238. row.Cells[1].SetCellValue(DataTable.Rows[i]["STF_FEEDER"].ToString());
  239. row.Cells[1].CellStyle = style;
  240. //物料编号
  241. row.Cells[2].SetCellValue(DataTable.Rows[i]["STF_FEEDERNO"].ToString());
  242. row.Cells[2].CellStyle = style;
  243. row.Cells[3].SetCellValue(DataTable.Rows[i]["STF_LOCATION"].ToString());
  244. row.Cells[3].CellStyle = style;
  245. row.Cells[4].SetCellValue(DataTable.Rows[i]["pr_orispeccode"].ToString());
  246. row.Cells[4].CellStyle = style;
  247. row.Cells[4].CellStyle.WrapText = true;
  248. row.Cells[5].SetCellValue(DataTable.Rows[i]["bd_baseqty"].ToString());
  249. row.Cells[5].CellStyle = style;
  250. row.Cells[6].SetCellValue(DataTable.Rows[i]["BD_LOCATION"].ToString());
  251. row.Cells[6].CellStyle = style;
  252. row.Cells[7].SetCellValue("");
  253. row.Cells[7].CellStyle = style;
  254. row.Cells[4].CellStyle.ShrinkToFit = true;
  255. DataRowCount = DataRowCount + 1;
  256. }
  257. }
  258. }
  259. //将book的内容写入内存流中返回
  260. book.Write(ms);
  261. return ms;
  262. }
  263. /// <summary>
  264. /// 导出Excel,返回文件在客户端的路径
  265. /// </summary>
  266. public string ExportExcel_BAIDU(DataTable dt, DateTime begindate, int DateNum, string FolderPath)
  267. {
  268. //创建一个内存流,用来接收转换成Excel的内容
  269. MemoryStream ms;
  270. ms = DataTableToExcel_BAIDU(dt, begindate, DateNum);
  271. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  272. string filePath = @FolderPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + "各工序直通率.xls";
  273. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  274. byte[] data = ms.ToArray();
  275. fs.Write(data, 0, data.Length);
  276. fs.Flush();
  277. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  278. ms.Dispose();
  279. fs.Dispose();
  280. return filePath;
  281. }
  282. /// <summary>
  283. /// 导入Excel
  284. /// </summary>
  285. public DataTable ImportExcel(string FolderPath, string TableName)
  286. {
  287. DataTable dt = new DataTable();
  288. dt.TableName = TableName;
  289. if (FolderPath.Contains(".xls") || FolderPath.Contains(".xlsx"))
  290. {
  291. using (FileStream file = new FileStream(FolderPath, FileMode.Open, FileAccess.Read))
  292. {
  293. //获取文件流
  294. HSSFWorkbook workbook = new HSSFWorkbook(file);
  295. ISheet sheet = workbook.GetSheetAt(0);
  296. //获取所有的列名
  297. foreach (ICell item in sheet.GetRow(sheet.FirstRowNum).Cells)
  298. {
  299. dt.Columns.Add(item.ToString(), typeof(string));
  300. }
  301. System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  302. while (rows.MoveNext())
  303. {
  304. IRow row = (HSSFRow)rows.Current;
  305. if (row.RowNum == sheet.FirstRowNum)
  306. {
  307. continue;
  308. }
  309. DataRow dr = dt.NewRow();
  310. foreach (ICell item in row.Cells)
  311. {
  312. switch (item.CellType)
  313. {
  314. case CellType.BOOLEAN:
  315. dr[item.ColumnIndex] = item.BooleanCellValue;
  316. break;
  317. case CellType.ERROR:
  318. dr[item.ColumnIndex] = ErrorEval.GetText(item.ErrorCellValue);
  319. break;
  320. case CellType.FORMULA:
  321. switch (item.CachedFormulaResultType)
  322. {
  323. case CellType.BOOLEAN:
  324. dr[item.ColumnIndex] = item.BooleanCellValue;
  325. break;
  326. case CellType.ERROR:
  327. dr[item.ColumnIndex] = ErrorEval.GetText(item.ErrorCellValue);
  328. break;
  329. case CellType.NUMERIC:
  330. if (DateUtil.IsCellDateFormatted(item))
  331. {
  332. dr[item.ColumnIndex] = item.DateCellValue.ToString("yyyy-MM-dd hh:MM:ss");
  333. }
  334. else
  335. {
  336. dr[item.ColumnIndex] = item.NumericCellValue;
  337. }
  338. break;
  339. case CellType.STRING:
  340. string str = item.StringCellValue;
  341. if (!string.IsNullOrEmpty(str))
  342. {
  343. dr[item.ColumnIndex] = str.ToString();
  344. }
  345. else
  346. {
  347. dr[item.ColumnIndex] = null;
  348. }
  349. break;
  350. case CellType.Unknown:
  351. case CellType.BLANK:
  352. default:
  353. dr[item.ColumnIndex] = string.Empty;
  354. break;
  355. }
  356. break;
  357. case CellType.NUMERIC:
  358. if (DateUtil.IsCellDateFormatted(item))
  359. {
  360. dr[item.ColumnIndex] = item.DateCellValue.ToString("yyyy-MM-dd hh:MM:ss");
  361. }
  362. else
  363. {
  364. dr[item.ColumnIndex] = item.NumericCellValue;
  365. }
  366. break;
  367. case CellType.STRING:
  368. string strValue = item.StringCellValue;
  369. if (string.IsNullOrEmpty(strValue))
  370. {
  371. dr[item.ColumnIndex] = strValue.ToString();
  372. }
  373. else
  374. {
  375. dr[item.ColumnIndex] = null;
  376. }
  377. break;
  378. case CellType.Unknown:
  379. case CellType.BLANK:
  380. default:
  381. dr[item.ColumnIndex] = string.Empty;
  382. break;
  383. }
  384. }
  385. dt.Rows.Add(dr);
  386. }
  387. }
  388. }
  389. return dt;
  390. }
  391. public void WriteTxt(DataTable dt, string FolderPath, string FileName)
  392. {
  393. StreamWriter sr;
  394. string filePath = @FolderPath;
  395. if (File.Exists(filePath + "\\" + FileName)) //如果文件存在,则创建File.AppendText对象
  396. {
  397. File.Delete(filePath + "\\" + FileName);
  398. }
  399. sr = File.CreateText(filePath + "\\" + FileName);
  400. StringBuilder sb = new StringBuilder();
  401. string Title = "";
  402. foreach (DataColumn dc in dt.Columns)
  403. {
  404. Title += string.Format("{0,10}", dc.ColumnName.ToString());
  405. }
  406. sr.WriteLine(Title + "\r");
  407. foreach (DataRow dr in dt.Rows)
  408. {
  409. string text = "";
  410. for (int i = 0; i < dt.Columns.Count; i++)
  411. {
  412. text += String.Format("{0,10}", dr[i].ToString());
  413. }
  414. sr.WriteLine(text + "\r");
  415. }
  416. sr.Close();
  417. }
  418. /// <summary>
  419. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  420. /// </summary>
  421. /// <param name="DataTable"></param>
  422. /// <returns></returns>
  423. public MemoryStream DataTableToExcel(DataTable DataTable)
  424. {
  425. //创建内存流
  426. MemoryStream ms = new MemoryStream();
  427. //创建一个Book,相当于一个Excel文件
  428. HSSFWorkbook book = new HSSFWorkbook();
  429. //Excel中的Sheet
  430. ISheet sheet = book.CreateSheet("sheet1");
  431. //获取行数量和列数量
  432. int rowNum = DataTable.Rows.Count;
  433. int columnNum = DataTable.Columns.Count;
  434. //设置列的宽度,根据首行的列的内容的长度来设置
  435. for (int i = 0; i < columnNum; i++)
  436. {
  437. int dataLength = DataTable.Columns[i].ColumnName.Length;
  438. sheet.SetColumnWidth(i, dataLength * 700);
  439. }
  440. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  441. IRow row = sheet.CreateRow(0);
  442. //冻结第一行
  443. sheet.CreateFreezePane(0, 1, 0, 1);
  444. ICellStyle style = book.CreateCellStyle();
  445. style.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  446. style.FillPattern = FillPatternType.BIG_SPOTS;
  447. style.FillBackgroundColor = HSSFColor.LIGHT_GREEN.index;
  448. //设置边框
  449. style.BorderBottom = BorderStyle.THICK;
  450. style.BorderLeft = BorderStyle.THICK;
  451. style.BorderRight = BorderStyle.THICK;
  452. style.BorderTop = BorderStyle.THICK;
  453. row.HeightInPoints = 20;
  454. //固定第一行
  455. //row.RowStyle.IsLocked=true;
  456. //给第一行的标签赋值样式和值
  457. for (int j = 0; j < columnNum; j++)
  458. {
  459. row.CreateCell(j);
  460. row.Cells[j].CellStyle = style;
  461. row.Cells[j].CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  462. row.Cells[j].CellStyle.Alignment = HorizontalAlignment.CENTER;
  463. row.Cells[j].SetCellValue(DataTable.Columns[j].ColumnName);
  464. }
  465. //将DataTable的值循环赋值给book,Aligment设置居中
  466. //之前已经画了带颜色的第一行,所以从i=1开始画
  467. for (int i = 0; i < rowNum; i++)
  468. {
  469. IRow row1 = sheet.CreateRow(i + 1);
  470. row1.HeightInPoints = 20;
  471. for (int j = 0; j < columnNum; j++)
  472. {
  473. row1.CreateCell(j);
  474. row1.Cells[j].SetCellValue(DataTable.Rows[i][j].ToString());
  475. row1.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  476. }
  477. }
  478. //将book的内容写入内存流中返回
  479. book.Write(ms);
  480. return ms;
  481. }
  482. /// <summary>
  483. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  484. /// </summary>
  485. /// <param name="DataTable"></param>
  486. /// <returns></returns>
  487. public MemoryStream DataTableToExcel_BAIDU(DataTable DataTable, DateTime begindate, int DateNum)
  488. {
  489. string[] Step = new[] { "1-MT1", "2-MT2", "3-MMI", "4-RSA(耦合)", "5-AUD(曲线)", "6-SCW(写号)", "7-SCK(验号)" };
  490. string[] StepCode = new[] { "B_MT1", "B_MT2", "B_MMI", "B_RSA", "B_AUD", "B_WRITE", "B_SN", "B_OUTLOOK" };
  491. string[] Kind = new[] { "测试数", "通过数", "不良数", "误测通过数", "误测数", "FPY", "RPY" };
  492. string[] TotalKind = new[] { "总投入数", "总不良数", "FPY", "RPY" };
  493. string[] OutLook = new[] { "测试数", "不良数", "FPY" };
  494. //每行的内容
  495. int ContentRow = 7;
  496. //外观的展示的行
  497. int OutLookRow = 55;
  498. MemoryStream ms = new MemoryStream();
  499. //创建一个Book,相当于一个Excel文件
  500. HSSFWorkbook book = new HSSFWorkbook();
  501. ICellStyle NONE = book.CreateCellStyle();
  502. NONE.VerticalAlignment = VerticalAlignment.CENTER;
  503. NONE.Alignment = HorizontalAlignment.CENTER;
  504. NONE.BorderBottom = BorderStyle.THIN;
  505. NONE.BorderLeft = BorderStyle.THIN;
  506. NONE.BorderRight = BorderStyle.THIN;
  507. NONE.BorderTop = BorderStyle.THIN;
  508. ICellStyle TAN = book.CreateCellStyle();
  509. TAN.VerticalAlignment = VerticalAlignment.CENTER;
  510. TAN.Alignment = HorizontalAlignment.CENTER;
  511. TAN.FillForegroundColor = HSSFColor.TAN.index;
  512. TAN.FillPattern = FillPatternType.SOLID_FOREGROUND;
  513. TAN.BorderBottom = BorderStyle.THIN;
  514. TAN.BorderLeft = BorderStyle.THIN;
  515. TAN.BorderRight = BorderStyle.THIN;
  516. TAN.BorderTop = BorderStyle.THIN;
  517. ICellStyle PALE_BLUE = book.CreateCellStyle();
  518. PALE_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  519. PALE_BLUE.Alignment = HorizontalAlignment.CENTER;
  520. PALE_BLUE.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  521. PALE_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  522. PALE_BLUE.BorderBottom = BorderStyle.THIN;
  523. PALE_BLUE.BorderLeft = BorderStyle.THIN;
  524. PALE_BLUE.BorderRight = BorderStyle.THIN;
  525. PALE_BLUE.BorderTop = BorderStyle.THIN;
  526. ICellStyle LIME = book.CreateCellStyle();
  527. LIME.VerticalAlignment = VerticalAlignment.CENTER;
  528. LIME.Alignment = HorizontalAlignment.CENTER;
  529. LIME.FillForegroundColor = HSSFColor.LIME.index;
  530. LIME.FillPattern = FillPatternType.SOLID_FOREGROUND;
  531. LIME.BorderBottom = BorderStyle.THIN;
  532. LIME.BorderLeft = BorderStyle.THIN;
  533. LIME.BorderRight = BorderStyle.THIN;
  534. LIME.BorderTop = BorderStyle.THIN;
  535. ICellStyle LEMON_CHIFFON = book.CreateCellStyle();
  536. LEMON_CHIFFON.VerticalAlignment = VerticalAlignment.CENTER;
  537. LEMON_CHIFFON.Alignment = HorizontalAlignment.CENTER;
  538. LEMON_CHIFFON.FillForegroundColor = HSSFColor.LEMON_CHIFFON.index;
  539. LEMON_CHIFFON.FillPattern = FillPatternType.SOLID_FOREGROUND;
  540. LEMON_CHIFFON.BorderBottom = BorderStyle.THIN;
  541. LEMON_CHIFFON.BorderLeft = BorderStyle.THIN;
  542. LEMON_CHIFFON.BorderRight = BorderStyle.THIN;
  543. LEMON_CHIFFON.BorderTop = BorderStyle.THIN;
  544. LEMON_CHIFFON.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  545. ICellStyle GOLD = book.CreateCellStyle();
  546. GOLD.VerticalAlignment = VerticalAlignment.CENTER;
  547. GOLD.Alignment = HorizontalAlignment.CENTER;
  548. GOLD.FillForegroundColor = HSSFColor.GOLD.index;
  549. GOLD.FillPattern = FillPatternType.SOLID_FOREGROUND;
  550. GOLD.BorderBottom = BorderStyle.THIN;
  551. GOLD.BorderLeft = BorderStyle.THIN;
  552. GOLD.BorderRight = BorderStyle.THIN;
  553. GOLD.BorderTop = BorderStyle.THIN;
  554. ICellStyle LIGHT_GREEN = book.CreateCellStyle();
  555. LIGHT_GREEN.VerticalAlignment = VerticalAlignment.CENTER;
  556. LIGHT_GREEN.Alignment = HorizontalAlignment.CENTER;
  557. LIGHT_GREEN.FillForegroundColor = HSSFColor.LIGHT_GREEN.index;
  558. LIGHT_GREEN.FillPattern = FillPatternType.SOLID_FOREGROUND;
  559. LIGHT_GREEN.BorderBottom = BorderStyle.THIN;
  560. LIGHT_GREEN.BorderLeft = BorderStyle.THIN;
  561. LIGHT_GREEN.BorderRight = BorderStyle.THIN;
  562. LIGHT_GREEN.BorderTop = BorderStyle.THIN;
  563. //LIGHT_GREEN.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  564. ICellStyle DARK_BLUE = book.CreateCellStyle();
  565. DARK_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  566. DARK_BLUE.Alignment = HorizontalAlignment.CENTER;
  567. DARK_BLUE.FillForegroundColor = HSSFColor.LIGHT_BLUE.index;
  568. DARK_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  569. DARK_BLUE.BorderBottom = BorderStyle.THIN;
  570. DARK_BLUE.BorderLeft = BorderStyle.THIN;
  571. DARK_BLUE.BorderRight = BorderStyle.THIN;
  572. DARK_BLUE.BorderTop = BorderStyle.THIN;
  573. DARK_BLUE.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  574. ICellStyle LIGHT_CORNFLOWER_BLUE = book.CreateCellStyle();
  575. LIGHT_CORNFLOWER_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  576. LIGHT_CORNFLOWER_BLUE.Alignment = HorizontalAlignment.CENTER;
  577. LIGHT_CORNFLOWER_BLUE.FillForegroundColor = HSSFColor.LIGHT_CORNFLOWER_BLUE.index;
  578. LIGHT_CORNFLOWER_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  579. LIGHT_CORNFLOWER_BLUE.BorderBottom = BorderStyle.THIN;
  580. LIGHT_CORNFLOWER_BLUE.BorderLeft = BorderStyle.THIN;
  581. LIGHT_CORNFLOWER_BLUE.BorderRight = BorderStyle.THIN;
  582. LIGHT_CORNFLOWER_BLUE.BorderTop = BorderStyle.THIN;
  583. LIGHT_CORNFLOWER_BLUE.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  584. ICellStyle GREY_25_PERCENT = book.CreateCellStyle();
  585. GREY_25_PERCENT.VerticalAlignment = VerticalAlignment.CENTER;
  586. GREY_25_PERCENT.Alignment = HorizontalAlignment.CENTER;
  587. GREY_25_PERCENT.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
  588. GREY_25_PERCENT.FillPattern = FillPatternType.SOLID_FOREGROUND;
  589. GREY_25_PERCENT.BorderBottom = BorderStyle.THIN;
  590. GREY_25_PERCENT.BorderLeft = BorderStyle.THIN;
  591. GREY_25_PERCENT.BorderRight = BorderStyle.THIN;
  592. GREY_25_PERCENT.BorderTop = BorderStyle.THIN;
  593. ICellStyle PINK = book.CreateCellStyle();
  594. PINK.VerticalAlignment = VerticalAlignment.CENTER;
  595. PINK.Alignment = HorizontalAlignment.CENTER;
  596. PINK.FillForegroundColor = HSSFColor.LIGHT_ORANGE.index;
  597. PINK.FillPattern = FillPatternType.SOLID_FOREGROUND;
  598. PINK.BorderBottom = BorderStyle.THIN;
  599. PINK.BorderLeft = BorderStyle.THIN;
  600. PINK.BorderRight = BorderStyle.THIN;
  601. PINK.BorderTop = BorderStyle.THIN;
  602. //Excel中的Sheet
  603. ISheet sheet = book.CreateSheet("sheet1");
  604. IRow row = sheet.CreateRow(0);
  605. ICell cell = row.CreateCell(0);
  606. //画第一行的抬头
  607. cell.SetCellValue("组装制程品质数据");
  608. cell.CellStyle = NONE;
  609. CellRangeAddress region = new CellRangeAddress(0, 0, 0, DateNum + 1);
  610. sheet.AddMergedRegion(region);
  611. //第一行的日期标题
  612. row = sheet.CreateRow(1);
  613. cell = row.CreateCell(0);
  614. cell.CellStyle = NONE;
  615. cell.SetCellValue("站点");
  616. cell = row.CreateCell(1);
  617. cell.SetCellValue("类别");
  618. cell.CellStyle = NONE;
  619. for (int i = 0; i < DateNum; i++)
  620. {
  621. cell = row.CreateCell(i + 2);
  622. cell.SetCellValue(begindate.AddDays(i).ToString("MM/dd"));
  623. cell.CellStyle = NONE;
  624. }
  625. //画第一列的工序名称和第二列的类别
  626. //总良率数据
  627. row = sheet.CreateRow(2);
  628. cell = row.CreateCell(0);
  629. cell.SetCellValue("总良率");
  630. cell.CellStyle = LEMON_CHIFFON;
  631. region = new CellRangeAddress(2, 5, 0, 0);
  632. sheet.AddMergedRegion(region);
  633. //总良率的统计数据
  634. for (int i = 0; i < TotalKind.Length; i++)
  635. {
  636. row = sheet.GetRow(i + 2);
  637. if (row == null)
  638. {
  639. row = sheet.CreateRow(i + 2);
  640. }
  641. cell = row.CreateCell(1);
  642. cell.SetCellValue(TotalKind[i]);
  643. cell.CellStyle = LEMON_CHIFFON;
  644. switch (i)
  645. {
  646. case 0:
  647. cell.CellStyle = LIME;
  648. break;
  649. case 1:
  650. cell.CellStyle = TAN;
  651. break;
  652. case 2:
  653. cell.CellStyle = DARK_BLUE;
  654. break;
  655. case 3:
  656. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  657. break;
  658. default:
  659. break;
  660. }
  661. }
  662. //中间的设备测试工序
  663. for (int i = 0; i < Step.Length; i++)
  664. {
  665. //除去前面6行
  666. int rowindex = 6 + i * ContentRow;
  667. row = sheet.CreateRow(rowindex);
  668. cell = row.CreateCell(0);
  669. cell.SetCellValue(Step[i]);
  670. cell.CellStyle = PALE_BLUE;
  671. region = new CellRangeAddress(rowindex, rowindex + ContentRow - 1, 0, 0);
  672. sheet.AddMergedRegion(region);
  673. for (int j = rowindex; j < rowindex + ContentRow; j++)
  674. {
  675. row = sheet.GetRow(j);
  676. if (row == null)
  677. {
  678. row = sheet.CreateRow(j);
  679. }
  680. cell = row.CreateCell(1);
  681. cell.SetCellValue(Kind[j - rowindex]);
  682. switch (j - rowindex)
  683. {
  684. case 0:
  685. cell.CellStyle = GREY_25_PERCENT;
  686. break;
  687. case 1:
  688. cell.CellStyle = PINK;
  689. break;
  690. case 2:
  691. cell.CellStyle = TAN;
  692. break;
  693. case 3:
  694. cell.CellStyle = GOLD;
  695. break;
  696. case 4:
  697. cell.CellStyle = LIGHT_GREEN;
  698. break;
  699. case 5:
  700. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  701. break;
  702. case 6:
  703. cell.CellStyle = LEMON_CHIFFON;
  704. break;
  705. default:
  706. break;
  707. }
  708. }
  709. }
  710. //最后一行外观数据
  711. row = sheet.CreateRow(OutLookRow);
  712. cell = row.CreateCell(0);
  713. cell.SetCellValue("8-外观");
  714. cell.CellStyle = PALE_BLUE;
  715. region = new CellRangeAddress(OutLookRow, OutLookRow + 2, 0, 0);
  716. sheet.AddMergedRegion(region);
  717. //外观的统计数据
  718. for (int i = 0; i < OutLook.Length; i++)
  719. {
  720. row = sheet.GetRow(OutLookRow + i);
  721. if (row == null)
  722. {
  723. row = sheet.CreateRow(OutLookRow + i);
  724. }
  725. cell = row.CreateCell(1);
  726. cell.SetCellValue(OutLook[i]);
  727. cell.CellStyle = PALE_BLUE;
  728. switch (i)
  729. {
  730. case 0:
  731. cell.CellStyle = GREY_25_PERCENT;
  732. break;
  733. case 1:
  734. cell.CellStyle = TAN;
  735. break;
  736. case 2:
  737. cell.CellStyle = LIGHT_GREEN;
  738. break;
  739. default:
  740. break;
  741. }
  742. }
  743. sheet.SetColumnWidth(0, 3700);
  744. for (int i = 0; i < DateNum; i++)
  745. {
  746. double TotalFPY = -1;
  747. double TotalRPY = -1;
  748. double TotalNG = 0;
  749. double TotalIN = 0;
  750. for (int j = 0; j < StepCode.Length; j++)
  751. {
  752. int rowindex = 6 + j * ContentRow;
  753. DataTable dt = PublicMethod.BaseUtil.filterDataTable(DataTable, "sp_date='" + begindate.AddDays(i).ToString("yyyy-MM-dd") + "' and 工序编号='" + StepCode[j] + "'");
  754. if (StepCode[j] != "B_OUTLOOK")
  755. {
  756. for (int k = rowindex; k < rowindex + ContentRow; k++)
  757. {
  758. row = sheet.GetRow(k);
  759. if (row == null)
  760. {
  761. row = sheet.CreateRow(k);
  762. }
  763. cell = row.CreateCell(i + 2);
  764. switch (k - rowindex)
  765. {
  766. case 0:
  767. double 测试数;
  768. if (dt.Rows.Count > 0)
  769. {
  770. if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
  771. {
  772. cell.SetCellValue(测试数);
  773. }
  774. }
  775. cell.CellStyle = GREY_25_PERCENT;
  776. break;
  777. case 1:
  778. double 通过数;
  779. if (dt.Rows.Count > 0)
  780. {
  781. if (double.TryParse(dt.Rows[0]["通过总数"].ToString(), out 通过数))
  782. {
  783. cell.SetCellValue(通过数);
  784. }
  785. }
  786. cell.CellStyle = PINK;
  787. break;
  788. case 2:
  789. double 不良数;
  790. if (dt.Rows.Count > 0)
  791. {
  792. if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
  793. {
  794. cell.SetCellValue(不良数);
  795. TotalNG = TotalNG + 不良数;
  796. }
  797. }
  798. cell.CellStyle = TAN;
  799. break;
  800. case 3:
  801. double 误测通过数;
  802. if (dt.Rows.Count > 0)
  803. {
  804. if (double.TryParse(dt.Rows[0]["误测通过数"].ToString(), out 误测通过数))
  805. {
  806. cell.SetCellValue(误测通过数);
  807. }
  808. }
  809. cell.CellStyle = GOLD;
  810. break;
  811. case 4:
  812. double 误测数;
  813. if (dt.Rows.Count > 0)
  814. {
  815. if (double.TryParse(dt.Rows[0]["误测数"].ToString(), out 误测数))
  816. {
  817. cell.SetCellValue(误测数);
  818. }
  819. }
  820. cell.CellStyle = LIGHT_GREEN;
  821. break;
  822. case 5:
  823. double FPY;
  824. if (dt.Rows.Count > 0)
  825. {
  826. if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
  827. {
  828. cell.SetCellValue(FPY);
  829. //累计所有FPY
  830. if (TotalFPY == -1)
  831. {
  832. TotalFPY = FPY;
  833. }
  834. else
  835. {
  836. TotalFPY = TotalFPY * FPY;
  837. }
  838. }
  839. }
  840. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  841. break;
  842. case 6:
  843. double RPY;
  844. if (dt.Rows.Count > 0)
  845. {
  846. if (double.TryParse(dt.Rows[0]["RPY"].ToString(), out RPY))
  847. {
  848. cell.SetCellValue(RPY);
  849. //累计所有RPY
  850. if (TotalRPY == -1)
  851. {
  852. TotalRPY = RPY;
  853. }
  854. else
  855. {
  856. TotalRPY = TotalRPY * RPY;
  857. }
  858. }
  859. }
  860. cell.CellStyle = LEMON_CHIFFON;
  861. break;
  862. default:
  863. break;
  864. }
  865. }
  866. }
  867. else
  868. {
  869. for (int k = rowindex; k < rowindex + 3; k++)
  870. {
  871. row = sheet.GetRow(k);
  872. if (row == null)
  873. {
  874. row = sheet.CreateRow(k);
  875. }
  876. cell = row.CreateCell(i + 2);
  877. switch (k - rowindex)
  878. {
  879. case 0:
  880. double 测试数;
  881. if (dt.Rows.Count > 0)
  882. {
  883. if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
  884. {
  885. cell.SetCellValue(测试数);
  886. }
  887. }
  888. cell.CellStyle = GREY_25_PERCENT;
  889. break;
  890. case 1:
  891. double 不良数;
  892. if (dt.Rows.Count > 0)
  893. {
  894. if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
  895. {
  896. cell.SetCellValue(不良数);
  897. TotalNG = TotalNG + 不良数;
  898. }
  899. }
  900. cell.CellStyle = TAN;
  901. break;
  902. case 2:
  903. double FPY;
  904. if (dt.Rows.Count > 0)
  905. {
  906. if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
  907. {
  908. cell.SetCellValue(FPY);
  909. //累计所有FPY
  910. if (TotalFPY == -1)
  911. {
  912. TotalFPY = FPY;
  913. }
  914. else
  915. {
  916. TotalFPY = TotalFPY * FPY;
  917. }
  918. }
  919. }
  920. cell.CellStyle = LIGHT_GREEN;
  921. break;
  922. default:
  923. break;
  924. }
  925. }
  926. }
  927. }
  928. DataTable dt1 = PublicMethod.BaseUtil.filterDataTable(DataTable, "sp_date='" + begindate.AddDays(i).ToString("yyyy-MM-dd") + "' and 工序编号='B_LCDBA1'");
  929. double 投入数;
  930. if (dt1.Rows.Count > 0)
  931. {
  932. if (double.TryParse(dt1.Rows[0]["测试数"].ToString(), out 投入数))
  933. {
  934. TotalIN = 投入数;
  935. }
  936. }
  937. //设置最上方的总计数量
  938. row = sheet.GetRow(2);
  939. cell = row.CreateCell(i + 2);
  940. cell.CellStyle = LIME;
  941. cell.SetCellValue(TotalIN);
  942. row = sheet.GetRow(3);
  943. cell = row.CreateCell(i + 2);
  944. cell.CellStyle = TAN;
  945. cell.SetCellValue(TotalNG);
  946. row = sheet.GetRow(4);
  947. cell = row.CreateCell(i + 2);
  948. cell.CellStyle = DARK_BLUE;
  949. cell.SetCellValue(TotalFPY == -1 ? 0 : TotalFPY);
  950. row = sheet.GetRow(5);
  951. cell = row.CreateCell(i + 2);
  952. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  953. cell.SetCellValue(TotalRPY == -1 ? 0 : TotalRPY);
  954. }
  955. for (int i = 0; i < sheet.PhysicalNumberOfRows; i++)
  956. {
  957. sheet.GetRow(i).Height = 300;
  958. }
  959. //将book的内容写入内存流中返回
  960. book.Write(ms);
  961. return ms;
  962. }
  963. }
  964. }