ExcelHandler.cs 51 KB

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