ExcelHandler.cs 51 KB

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