ExcelHandler.cs 35 KB

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