ExcelHandler.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. namespace UAS_MES_NEW.DataOperate
  13. {
  14. class ExcelHandler
  15. {
  16. DataHelper dh = new DataHelper();
  17. /// <summary>
  18. /// 导出Excel,返回文件在客户端的路径
  19. /// </summary>
  20. public string ExportExcel(DataTable dt, string FolderPath)
  21. {
  22. //创建一个内存流,用来接收转换成Excel的内容
  23. MemoryStream ms;
  24. ms = DataTableToExcel(dt);
  25. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  26. string filePath = @FolderPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
  27. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  28. byte[] data = ms.ToArray();
  29. fs.Write(data, 0, data.Length);
  30. fs.Flush();
  31. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  32. ms.Dispose();
  33. fs.Dispose();
  34. return filePath;
  35. }
  36. /// <summary>
  37. /// 导出Excel,返回文件在客户端的路径
  38. /// </summary>
  39. public string ExportExcel_BAIDU(DataTable dt, DateTime begindate, int DateNum, string FolderPath)
  40. {
  41. //创建一个内存流,用来接收转换成Excel的内容
  42. MemoryStream ms;
  43. ms = DataTableToExcel_BAIDU(dt, begindate, DateNum);
  44. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  45. string filePath = @FolderPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + "各工序直通率.xls";
  46. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  47. byte[] data = ms.ToArray();
  48. fs.Write(data, 0, data.Length);
  49. fs.Flush();
  50. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  51. ms.Dispose();
  52. fs.Dispose();
  53. return filePath;
  54. }
  55. /// <summary>
  56. /// 导入Excel
  57. /// </summary>
  58. public DataTable ImportExcel(string FolderPath, string TableName)
  59. {
  60. DataTable dt = new DataTable();
  61. dt.TableName = TableName;
  62. if (FolderPath.Contains(".xls") || FolderPath.Contains(".xlsx"))
  63. {
  64. using (FileStream file = new FileStream(FolderPath, FileMode.Open, FileAccess.Read))
  65. {
  66. //获取文件流
  67. HSSFWorkbook workbook = new HSSFWorkbook(file);
  68. ISheet sheet = workbook.GetSheetAt(0);
  69. //获取所有的列名
  70. foreach (ICell item in sheet.GetRow(sheet.FirstRowNum).Cells)
  71. {
  72. dt.Columns.Add(item.ToString(), typeof(string));
  73. }
  74. System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  75. while (rows.MoveNext())
  76. {
  77. IRow row = (HSSFRow)rows.Current;
  78. if (row.RowNum == sheet.FirstRowNum)
  79. {
  80. continue;
  81. }
  82. DataRow dr = dt.NewRow();
  83. foreach (ICell item in row.Cells)
  84. {
  85. switch (item.CellType)
  86. {
  87. case CellType.BOOLEAN:
  88. dr[item.ColumnIndex] = item.BooleanCellValue;
  89. break;
  90. case CellType.ERROR:
  91. dr[item.ColumnIndex] = ErrorEval.GetText(item.ErrorCellValue);
  92. break;
  93. case CellType.FORMULA:
  94. switch (item.CachedFormulaResultType)
  95. {
  96. case CellType.BOOLEAN:
  97. dr[item.ColumnIndex] = item.BooleanCellValue;
  98. break;
  99. case CellType.ERROR:
  100. dr[item.ColumnIndex] = ErrorEval.GetText(item.ErrorCellValue);
  101. break;
  102. case CellType.NUMERIC:
  103. if (DateUtil.IsCellDateFormatted(item))
  104. {
  105. dr[item.ColumnIndex] = item.DateCellValue.ToString("yyyy-MM-dd hh:MM:ss");
  106. }
  107. else
  108. {
  109. dr[item.ColumnIndex] = item.NumericCellValue;
  110. }
  111. break;
  112. case CellType.STRING:
  113. string str = item.StringCellValue;
  114. if (!string.IsNullOrEmpty(str))
  115. {
  116. dr[item.ColumnIndex] = str.ToString();
  117. }
  118. else
  119. {
  120. dr[item.ColumnIndex] = null;
  121. }
  122. break;
  123. case CellType.Unknown:
  124. case CellType.BLANK:
  125. default:
  126. dr[item.ColumnIndex] = string.Empty;
  127. break;
  128. }
  129. break;
  130. case CellType.NUMERIC:
  131. if (DateUtil.IsCellDateFormatted(item))
  132. {
  133. dr[item.ColumnIndex] = item.DateCellValue.ToString("yyyy-MM-dd hh:MM:ss");
  134. }
  135. else
  136. {
  137. dr[item.ColumnIndex] = item.NumericCellValue;
  138. }
  139. break;
  140. case CellType.STRING:
  141. string strValue = item.StringCellValue;
  142. if (string.IsNullOrEmpty(strValue))
  143. {
  144. dr[item.ColumnIndex] = strValue.ToString();
  145. }
  146. else
  147. {
  148. dr[item.ColumnIndex] = null;
  149. }
  150. break;
  151. case CellType.Unknown:
  152. case CellType.BLANK:
  153. default:
  154. dr[item.ColumnIndex] = string.Empty;
  155. break;
  156. }
  157. }
  158. dt.Rows.Add(dr);
  159. }
  160. }
  161. }
  162. return dt;
  163. }
  164. public void WriteTxt(DataTable dt, string FolderPath, string FileName)
  165. {
  166. StreamWriter sr;
  167. string filePath = @FolderPath;
  168. if (File.Exists(filePath + "\\" + FileName)) //如果文件存在,则创建File.AppendText对象
  169. {
  170. File.Delete(filePath + "\\" + FileName);
  171. }
  172. sr = File.CreateText(filePath + "\\" + FileName);
  173. StringBuilder sb = new StringBuilder();
  174. string Title = "";
  175. foreach (DataColumn dc in dt.Columns)
  176. {
  177. Title += string.Format("{0,10}", dc.ColumnName.ToString());
  178. }
  179. sr.WriteLine(Title + "\r");
  180. foreach (DataRow dr in dt.Rows)
  181. {
  182. string text = "";
  183. for (int i = 0; i < dt.Columns.Count; i++)
  184. {
  185. text += String.Format("{0,10}", dr[i].ToString());
  186. }
  187. sr.WriteLine(text + "\r");
  188. }
  189. sr.Close();
  190. }
  191. /// <summary>
  192. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  193. /// </summary>
  194. /// <param name="DataTable"></param>
  195. /// <returns></returns>
  196. public MemoryStream DataTableToExcel(DataTable DataTable)
  197. {
  198. //创建内存流
  199. MemoryStream ms = new MemoryStream();
  200. //创建一个Book,相当于一个Excel文件
  201. HSSFWorkbook book = new HSSFWorkbook();
  202. //Excel中的Sheet
  203. ISheet sheet = book.CreateSheet("sheet1");
  204. //获取行数量和列数量
  205. int rowNum = DataTable.Rows.Count;
  206. int columnNum = DataTable.Columns.Count;
  207. //设置列的宽度,根据首行的列的内容的长度来设置
  208. for (int i = 0; i < columnNum; i++)
  209. {
  210. int dataLength = DataTable.Columns[i].ColumnName.Length;
  211. sheet.SetColumnWidth(i, dataLength * 700);
  212. }
  213. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  214. IRow row = sheet.CreateRow(0);
  215. //冻结第一行
  216. sheet.CreateFreezePane(0, 1, 0, 1);
  217. ICellStyle style = book.CreateCellStyle();
  218. style.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  219. style.FillPattern = FillPatternType.BIG_SPOTS;
  220. style.FillBackgroundColor = HSSFColor.LIGHT_GREEN.index;
  221. //设置边框
  222. style.BorderBottom = BorderStyle.THICK;
  223. style.BorderLeft = BorderStyle.THICK;
  224. style.BorderRight = BorderStyle.THICK;
  225. style.BorderTop = BorderStyle.THICK;
  226. row.HeightInPoints = 20;
  227. //固定第一行
  228. //row.RowStyle.IsLocked=true;
  229. //给第一行的标签赋值样式和值
  230. for (int j = 0; j < columnNum; j++)
  231. {
  232. row.CreateCell(j);
  233. row.Cells[j].CellStyle = style;
  234. row.Cells[j].CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  235. row.Cells[j].CellStyle.Alignment = HorizontalAlignment.CENTER;
  236. row.Cells[j].SetCellValue(DataTable.Columns[j].ColumnName);
  237. }
  238. //将DataTable的值循环赋值给book,Aligment设置居中
  239. //之前已经画了带颜色的第一行,所以从i=1开始画
  240. for (int i = 0; i < rowNum; i++)
  241. {
  242. IRow row1 = sheet.CreateRow(i + 1);
  243. row1.HeightInPoints = 20;
  244. for (int j = 0; j < columnNum; j++)
  245. {
  246. row1.CreateCell(j);
  247. row1.Cells[j].SetCellValue(DataTable.Rows[i][j].ToString());
  248. row1.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  249. }
  250. }
  251. //将book的内容写入内存流中返回
  252. book.Write(ms);
  253. return ms;
  254. }
  255. /// <summary>
  256. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  257. /// </summary>
  258. /// <param name="DataTable"></param>
  259. /// <returns></returns>
  260. public MemoryStream DataTableToExcel_BAIDU(DataTable DataTable, DateTime begindate, int DateNum)
  261. {
  262. string[] Step = new[] { "1-MT1", "2-MT2", "3-MMI", "4-RSA(耦合)", "5-AUD(曲线)", "6-THD(自播自录)", "7-SCK(验号)" };
  263. string[] StepCode = new[] { "B_MT1", "B_MT2", "B_MMI", "B_RSA", "B_AUD", "B_THD", "B_SN", "B_OUTLOOK" };
  264. string[] Kind = new[] { "测试数", "不良数", "误测数", "FPY", "RPY" };
  265. string[] TotalKind = new[] { "总投入数", "总不良数", "FPY", "RPY" };
  266. string[] OutLook = new[] { "测试数", "不良数", "FPY" };
  267. MemoryStream ms = new MemoryStream();
  268. //创建一个Book,相当于一个Excel文件
  269. HSSFWorkbook book = new HSSFWorkbook();
  270. ICellStyle NONE = book.CreateCellStyle();
  271. NONE.VerticalAlignment = VerticalAlignment.CENTER;
  272. NONE.Alignment = HorizontalAlignment.CENTER;
  273. NONE.BorderBottom = BorderStyle.THIN;
  274. NONE.BorderLeft = BorderStyle.THIN;
  275. NONE.BorderRight = BorderStyle.THIN;
  276. NONE.BorderTop = BorderStyle.THIN;
  277. ICellStyle TAN = book.CreateCellStyle();
  278. TAN.VerticalAlignment = VerticalAlignment.CENTER;
  279. TAN.Alignment = HorizontalAlignment.CENTER;
  280. TAN.FillForegroundColor = HSSFColor.TAN.index;
  281. TAN.FillPattern = FillPatternType.SOLID_FOREGROUND;
  282. TAN.BorderBottom = BorderStyle.THIN;
  283. TAN.BorderLeft = BorderStyle.THIN;
  284. TAN.BorderRight = BorderStyle.THIN;
  285. TAN.BorderTop = BorderStyle.THIN;
  286. ICellStyle PALE_BLUE = book.CreateCellStyle();
  287. PALE_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  288. PALE_BLUE.Alignment = HorizontalAlignment.CENTER;
  289. PALE_BLUE.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  290. PALE_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  291. PALE_BLUE.BorderBottom = BorderStyle.THIN;
  292. PALE_BLUE.BorderLeft = BorderStyle.THIN;
  293. PALE_BLUE.BorderRight = BorderStyle.THIN;
  294. PALE_BLUE.BorderTop = BorderStyle.THIN;
  295. ICellStyle LIME = book.CreateCellStyle();
  296. LIME.VerticalAlignment = VerticalAlignment.CENTER;
  297. LIME.Alignment = HorizontalAlignment.CENTER;
  298. LIME.FillForegroundColor = HSSFColor.LIME.index;
  299. LIME.FillPattern = FillPatternType.SOLID_FOREGROUND;
  300. LIME.BorderBottom = BorderStyle.THIN;
  301. LIME.BorderLeft = BorderStyle.THIN;
  302. LIME.BorderRight = BorderStyle.THIN;
  303. LIME.BorderTop = BorderStyle.THIN;
  304. ICellStyle LEMON_CHIFFON = book.CreateCellStyle();
  305. LEMON_CHIFFON.VerticalAlignment = VerticalAlignment.CENTER;
  306. LEMON_CHIFFON.Alignment = HorizontalAlignment.CENTER;
  307. LEMON_CHIFFON.FillForegroundColor = HSSFColor.LEMON_CHIFFON.index;
  308. LEMON_CHIFFON.FillPattern = FillPatternType.SOLID_FOREGROUND;
  309. LEMON_CHIFFON.BorderBottom = BorderStyle.THIN;
  310. LEMON_CHIFFON.BorderLeft = BorderStyle.THIN;
  311. LEMON_CHIFFON.BorderRight = BorderStyle.THIN;
  312. LEMON_CHIFFON.BorderTop = BorderStyle.THIN;
  313. ICellStyle GOLD = book.CreateCellStyle();
  314. GOLD.VerticalAlignment = VerticalAlignment.CENTER;
  315. GOLD.Alignment = HorizontalAlignment.CENTER;
  316. GOLD.FillForegroundColor = HSSFColor.GOLD.index;
  317. GOLD.FillPattern = FillPatternType.SOLID_FOREGROUND;
  318. GOLD.BorderBottom = BorderStyle.THIN;
  319. GOLD.BorderLeft = BorderStyle.THIN;
  320. GOLD.BorderRight = BorderStyle.THIN;
  321. GOLD.BorderTop = BorderStyle.THIN;
  322. ICellStyle LIGHT_GREEN = book.CreateCellStyle();
  323. LIGHT_GREEN.VerticalAlignment = VerticalAlignment.CENTER;
  324. LIGHT_GREEN.Alignment = HorizontalAlignment.CENTER;
  325. LIGHT_GREEN.FillForegroundColor = HSSFColor.LIGHT_GREEN.index;
  326. LIGHT_GREEN.FillPattern = FillPatternType.SOLID_FOREGROUND;
  327. LIGHT_GREEN.BorderBottom = BorderStyle.THIN;
  328. LIGHT_GREEN.BorderLeft = BorderStyle.THIN;
  329. LIGHT_GREEN.BorderRight = BorderStyle.THIN;
  330. LIGHT_GREEN.BorderTop = BorderStyle.THIN;
  331. LIGHT_GREEN.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  332. ICellStyle LIGHT_CORNFLOWER_BLUE = book.CreateCellStyle();
  333. LIGHT_CORNFLOWER_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  334. LIGHT_CORNFLOWER_BLUE.Alignment = HorizontalAlignment.CENTER;
  335. LIGHT_CORNFLOWER_BLUE.FillForegroundColor = HSSFColor.LIGHT_CORNFLOWER_BLUE.index;
  336. LIGHT_CORNFLOWER_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  337. LIGHT_CORNFLOWER_BLUE.BorderBottom = BorderStyle.THIN;
  338. LIGHT_CORNFLOWER_BLUE.BorderLeft = BorderStyle.THIN;
  339. LIGHT_CORNFLOWER_BLUE.BorderRight = BorderStyle.THIN;
  340. LIGHT_CORNFLOWER_BLUE.BorderTop = BorderStyle.THIN;
  341. LIGHT_CORNFLOWER_BLUE.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  342. ICellStyle GREY_25_PERCENT = book.CreateCellStyle();
  343. GREY_25_PERCENT.VerticalAlignment = VerticalAlignment.CENTER;
  344. GREY_25_PERCENT.Alignment = HorizontalAlignment.CENTER;
  345. GREY_25_PERCENT.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
  346. GREY_25_PERCENT.FillPattern = FillPatternType.SOLID_FOREGROUND;
  347. GREY_25_PERCENT.BorderBottom = BorderStyle.THIN;
  348. GREY_25_PERCENT.BorderLeft = BorderStyle.THIN;
  349. GREY_25_PERCENT.BorderRight = BorderStyle.THIN;
  350. GREY_25_PERCENT.BorderTop = BorderStyle.THIN;
  351. //Excel中的Sheet
  352. ISheet sheet = book.CreateSheet("sheet1");
  353. IRow row = sheet.CreateRow(0);
  354. ICell cell = row.CreateCell(0);
  355. //画第一行的抬头
  356. cell.SetCellValue("组装制程品质数据");
  357. cell.CellStyle = NONE;
  358. CellRangeAddress region = new CellRangeAddress(0, 0, 0, DateNum + 1);
  359. sheet.AddMergedRegion(region);
  360. //第一行的日期标题
  361. row = sheet.CreateRow(1);
  362. cell = row.CreateCell(0);
  363. cell.CellStyle = NONE;
  364. cell.SetCellValue("站点");
  365. cell = row.CreateCell(1);
  366. cell.SetCellValue("类别");
  367. cell.CellStyle = NONE;
  368. for (int i = 0; i < DateNum; i++)
  369. {
  370. cell = row.CreateCell(i + 2);
  371. cell.SetCellValue(begindate.AddDays(i).ToString("MM/dd"));
  372. cell.CellStyle = NONE;
  373. }
  374. //画第一列的工序名称和第二列的类别
  375. //总良率数据
  376. row = sheet.CreateRow(2);
  377. cell = row.CreateCell(0);
  378. cell.SetCellValue("总良率");
  379. cell.CellStyle = LEMON_CHIFFON;
  380. region = new CellRangeAddress(2, 5, 0, 0);
  381. sheet.AddMergedRegion(region);
  382. //总良率的统计数据
  383. for (int i = 0; i < TotalKind.Length; i++)
  384. {
  385. row = sheet.GetRow(i + 2);
  386. if (row == null)
  387. {
  388. row = sheet.CreateRow(i + 2);
  389. }
  390. cell = row.CreateCell(1);
  391. cell.SetCellValue(TotalKind[i]);
  392. cell.CellStyle = LEMON_CHIFFON;
  393. switch (i)
  394. {
  395. case 0:
  396. cell.CellStyle = LIME;
  397. break;
  398. case 1:
  399. cell.CellStyle = TAN;
  400. break;
  401. case 2:
  402. cell.CellStyle = LIGHT_GREEN;
  403. break;
  404. case 3:
  405. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  406. break;
  407. default:
  408. break;
  409. }
  410. }
  411. //中间的设备测试工序
  412. for (int i = 0; i < Step.Length; i++)
  413. {
  414. //除去前面6行
  415. int rowindex = 6 + i * 5;
  416. row = sheet.CreateRow(rowindex);
  417. cell = row.CreateCell(0);
  418. cell.SetCellValue(Step[i]);
  419. cell.CellStyle = PALE_BLUE;
  420. region = new CellRangeAddress(rowindex, rowindex + 4, 0, 0);
  421. sheet.AddMergedRegion(region);
  422. for (int j = rowindex; j < rowindex + 5; j++)
  423. {
  424. row = sheet.GetRow(j);
  425. if (row == null)
  426. {
  427. row = sheet.CreateRow(j);
  428. }
  429. cell = row.CreateCell(1);
  430. cell.SetCellValue(Kind[j - rowindex]);
  431. switch (j - rowindex)
  432. {
  433. case 0:
  434. cell.CellStyle = GREY_25_PERCENT;
  435. break;
  436. case 1:
  437. cell.CellStyle = TAN;
  438. break;
  439. case 2:
  440. cell.CellStyle = GOLD;
  441. break;
  442. case 3:
  443. cell.CellStyle = LIGHT_GREEN;
  444. break;
  445. case 4:
  446. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  447. break;
  448. default:
  449. break;
  450. }
  451. }
  452. }
  453. //最后一行外观数据
  454. row = sheet.CreateRow(41);
  455. cell = row.CreateCell(0);
  456. cell.SetCellValue("8-外观");
  457. cell.CellStyle = PALE_BLUE;
  458. region = new CellRangeAddress(41, 43, 0, 0);
  459. sheet.AddMergedRegion(region);
  460. //外观的统计数据
  461. for (int i = 0; i < OutLook.Length; i++)
  462. {
  463. row = sheet.GetRow(41 + i);
  464. if (row == null)
  465. {
  466. row = sheet.CreateRow(41 + i);
  467. }
  468. cell = row.CreateCell(1);
  469. cell.SetCellValue(OutLook[i]);
  470. cell.CellStyle = PALE_BLUE;
  471. switch (i)
  472. {
  473. case 0:
  474. cell.CellStyle = GREY_25_PERCENT;
  475. break;
  476. case 1:
  477. cell.CellStyle = TAN;
  478. break;
  479. case 2:
  480. cell.CellStyle = LIGHT_GREEN;
  481. break;
  482. default:
  483. break;
  484. }
  485. }
  486. sheet.SetColumnWidth(0, 3700);
  487. for (int i = 0; i < DateNum; i++)
  488. {
  489. double TotalFPY = -1;
  490. double TotalRPY = -1;
  491. double TotalNG = 0;
  492. double TotalIN = 0;
  493. for (int j = 0; j < StepCode.Length; j++)
  494. {
  495. int rowindex = 6 + j * 5;
  496. DataTable dt = PublicMethod.BaseUtil.filterDataTable(DataTable, "sp_date='" + begindate.AddDays(i).ToString("yyyy-MM-dd") + "' and 工序编号='" + StepCode[j] + "'");
  497. if (StepCode[j] != "B_OUTLOOK")
  498. {
  499. for (int k = rowindex; k < rowindex + 5; k++)
  500. {
  501. row = sheet.GetRow(k);
  502. if (row == null)
  503. {
  504. row = sheet.CreateRow(k);
  505. }
  506. cell = row.CreateCell(i + 2);
  507. switch (k - rowindex)
  508. {
  509. case 0:
  510. double 测试数;
  511. if (dt.Rows.Count > 0)
  512. {
  513. if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
  514. {
  515. cell.SetCellValue(测试数);
  516. }
  517. }
  518. cell.CellStyle = GREY_25_PERCENT;
  519. break;
  520. case 1:
  521. double 不良数;
  522. if (dt.Rows.Count > 0)
  523. {
  524. if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
  525. {
  526. cell.SetCellValue(不良数);
  527. TotalNG = TotalNG + 不良数;
  528. }
  529. }
  530. cell.CellStyle = TAN;
  531. break;
  532. case 2:
  533. double 误测数;
  534. if (dt.Rows.Count > 0)
  535. {
  536. if (double.TryParse(dt.Rows[0]["误测数"].ToString(), out 误测数))
  537. {
  538. cell.SetCellValue(误测数);
  539. }
  540. }
  541. cell.CellStyle = GOLD;
  542. break;
  543. case 3:
  544. double FPY;
  545. if (dt.Rows.Count > 0)
  546. {
  547. if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
  548. {
  549. cell.SetCellValue(FPY);
  550. //累计所有FPY
  551. if (TotalFPY == -1)
  552. {
  553. TotalFPY = FPY;
  554. }
  555. else
  556. {
  557. TotalFPY = TotalFPY * FPY;
  558. }
  559. }
  560. }
  561. cell.CellStyle = LIGHT_GREEN;
  562. break;
  563. case 4:
  564. double RPY;
  565. if (dt.Rows.Count > 0)
  566. {
  567. if (double.TryParse(dt.Rows[0]["RPY"].ToString(), out RPY))
  568. {
  569. cell.SetCellValue(RPY);
  570. //累计所有RPY
  571. if (TotalRPY == -1)
  572. {
  573. TotalRPY = RPY;
  574. }
  575. else
  576. {
  577. TotalRPY = TotalRPY * RPY;
  578. }
  579. }
  580. }
  581. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  582. break;
  583. default:
  584. break;
  585. }
  586. }
  587. }
  588. else
  589. {
  590. for (int k = rowindex; k < rowindex + 3; k++)
  591. {
  592. row = sheet.GetRow(k);
  593. if (row == null)
  594. {
  595. row = sheet.CreateRow(k);
  596. }
  597. cell = row.CreateCell(i + 2);
  598. switch (k - rowindex)
  599. {
  600. case 0:
  601. double 测试数;
  602. if (dt.Rows.Count > 0)
  603. {
  604. if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
  605. {
  606. cell.SetCellValue(测试数);
  607. }
  608. }
  609. cell.CellStyle = GREY_25_PERCENT;
  610. break;
  611. case 1:
  612. double 不良数;
  613. if (dt.Rows.Count > 0)
  614. {
  615. if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
  616. {
  617. cell.SetCellValue(不良数);
  618. TotalNG = TotalNG + 不良数;
  619. }
  620. }
  621. cell.CellStyle = TAN;
  622. break;
  623. case 2:
  624. double FPY;
  625. if (dt.Rows.Count > 0)
  626. {
  627. if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
  628. {
  629. cell.SetCellValue(FPY);
  630. //累计所有FPY
  631. if (TotalFPY == -1)
  632. {
  633. TotalFPY = FPY;
  634. }
  635. else
  636. {
  637. TotalFPY = TotalFPY * FPY;
  638. }
  639. }
  640. }
  641. cell.CellStyle = LIGHT_GREEN;
  642. break;
  643. default:
  644. break;
  645. }
  646. }
  647. }
  648. }
  649. DataTable dt1 = PublicMethod.BaseUtil.filterDataTable(DataTable, "sp_date='" + begindate.AddDays(i).ToString("yyyy-MM-dd") + "' and 工序编号='B_LCDBA1'");
  650. double 投入数;
  651. if (dt1.Rows.Count > 0)
  652. {
  653. if (double.TryParse(dt1.Rows[0]["测试数"].ToString(), out 投入数))
  654. {
  655. TotalIN = 投入数;
  656. }
  657. }
  658. //设置最上方的总计数量
  659. row = sheet.GetRow(2);
  660. cell = row.CreateCell(i + 2);
  661. cell.CellStyle = LIME;
  662. cell.SetCellValue(TotalIN);
  663. row = sheet.GetRow(3);
  664. cell = row.CreateCell(i + 2);
  665. cell.CellStyle = TAN;
  666. cell.SetCellValue(TotalNG);
  667. row = sheet.GetRow(4);
  668. cell = row.CreateCell(i + 2);
  669. cell.CellStyle = LIGHT_GREEN;
  670. cell.SetCellValue(TotalFPY == -1 ? 0 : TotalFPY);
  671. row = sheet.GetRow(5);
  672. cell = row.CreateCell(i + 2);
  673. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  674. cell.SetCellValue(TotalRPY == -1 ? 0 : TotalRPY);
  675. }
  676. for (int i = 0; i < sheet.PhysicalNumberOfRows; i++)
  677. {
  678. sheet.GetRow(i).Height = 300;
  679. }
  680. //将book的内容写入内存流中返回
  681. book.Write(ms);
  682. return ms;
  683. }
  684. }
  685. }