ExcelHandler.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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-SCW(写号)", "7-SCK(验号)" };
  263. string[] StepCode = new[] { "B_MT1", "B_MT2", "B_MMI", "B_RSA", "B_AUD", "B_WRITE", "B_SN", "B_OUTLOOK" };
  264. string[] Kind = new[] { "测试数", "通过数", "不良数", "误测通过数", "误测数", "FPY", "RPY" };
  265. string[] TotalKind = new[] { "总投入数", "总不良数", "FPY", "RPY" };
  266. string[] OutLook = new[] { "测试数", "不良数", "FPY" };
  267. //每行的内容
  268. int ContentRow = 7;
  269. //外观的展示的行
  270. int OutLookRow = 55;
  271. MemoryStream ms = new MemoryStream();
  272. //创建一个Book,相当于一个Excel文件
  273. HSSFWorkbook book = new HSSFWorkbook();
  274. ICellStyle NONE = book.CreateCellStyle();
  275. NONE.VerticalAlignment = VerticalAlignment.CENTER;
  276. NONE.Alignment = HorizontalAlignment.CENTER;
  277. NONE.BorderBottom = BorderStyle.THIN;
  278. NONE.BorderLeft = BorderStyle.THIN;
  279. NONE.BorderRight = BorderStyle.THIN;
  280. NONE.BorderTop = BorderStyle.THIN;
  281. ICellStyle TAN = book.CreateCellStyle();
  282. TAN.VerticalAlignment = VerticalAlignment.CENTER;
  283. TAN.Alignment = HorizontalAlignment.CENTER;
  284. TAN.FillForegroundColor = HSSFColor.TAN.index;
  285. TAN.FillPattern = FillPatternType.SOLID_FOREGROUND;
  286. TAN.BorderBottom = BorderStyle.THIN;
  287. TAN.BorderLeft = BorderStyle.THIN;
  288. TAN.BorderRight = BorderStyle.THIN;
  289. TAN.BorderTop = BorderStyle.THIN;
  290. ICellStyle PALE_BLUE = book.CreateCellStyle();
  291. PALE_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  292. PALE_BLUE.Alignment = HorizontalAlignment.CENTER;
  293. PALE_BLUE.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  294. PALE_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  295. PALE_BLUE.BorderBottom = BorderStyle.THIN;
  296. PALE_BLUE.BorderLeft = BorderStyle.THIN;
  297. PALE_BLUE.BorderRight = BorderStyle.THIN;
  298. PALE_BLUE.BorderTop = BorderStyle.THIN;
  299. ICellStyle LIME = book.CreateCellStyle();
  300. LIME.VerticalAlignment = VerticalAlignment.CENTER;
  301. LIME.Alignment = HorizontalAlignment.CENTER;
  302. LIME.FillForegroundColor = HSSFColor.LIME.index;
  303. LIME.FillPattern = FillPatternType.SOLID_FOREGROUND;
  304. LIME.BorderBottom = BorderStyle.THIN;
  305. LIME.BorderLeft = BorderStyle.THIN;
  306. LIME.BorderRight = BorderStyle.THIN;
  307. LIME.BorderTop = BorderStyle.THIN;
  308. ICellStyle LEMON_CHIFFON = book.CreateCellStyle();
  309. LEMON_CHIFFON.VerticalAlignment = VerticalAlignment.CENTER;
  310. LEMON_CHIFFON.Alignment = HorizontalAlignment.CENTER;
  311. LEMON_CHIFFON.FillForegroundColor = HSSFColor.LEMON_CHIFFON.index;
  312. LEMON_CHIFFON.FillPattern = FillPatternType.SOLID_FOREGROUND;
  313. LEMON_CHIFFON.BorderBottom = BorderStyle.THIN;
  314. LEMON_CHIFFON.BorderLeft = BorderStyle.THIN;
  315. LEMON_CHIFFON.BorderRight = BorderStyle.THIN;
  316. LEMON_CHIFFON.BorderTop = BorderStyle.THIN;
  317. LEMON_CHIFFON.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  318. ICellStyle GOLD = book.CreateCellStyle();
  319. GOLD.VerticalAlignment = VerticalAlignment.CENTER;
  320. GOLD.Alignment = HorizontalAlignment.CENTER;
  321. GOLD.FillForegroundColor = HSSFColor.GOLD.index;
  322. GOLD.FillPattern = FillPatternType.SOLID_FOREGROUND;
  323. GOLD.BorderBottom = BorderStyle.THIN;
  324. GOLD.BorderLeft = BorderStyle.THIN;
  325. GOLD.BorderRight = BorderStyle.THIN;
  326. GOLD.BorderTop = BorderStyle.THIN;
  327. ICellStyle LIGHT_GREEN = book.CreateCellStyle();
  328. LIGHT_GREEN.VerticalAlignment = VerticalAlignment.CENTER;
  329. LIGHT_GREEN.Alignment = HorizontalAlignment.CENTER;
  330. LIGHT_GREEN.FillForegroundColor = HSSFColor.LIGHT_GREEN.index;
  331. LIGHT_GREEN.FillPattern = FillPatternType.SOLID_FOREGROUND;
  332. LIGHT_GREEN.BorderBottom = BorderStyle.THIN;
  333. LIGHT_GREEN.BorderLeft = BorderStyle.THIN;
  334. LIGHT_GREEN.BorderRight = BorderStyle.THIN;
  335. LIGHT_GREEN.BorderTop = BorderStyle.THIN;
  336. //LIGHT_GREEN.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  337. ICellStyle DARK_BLUE = book.CreateCellStyle();
  338. DARK_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  339. DARK_BLUE.Alignment = HorizontalAlignment.CENTER;
  340. DARK_BLUE.FillForegroundColor = HSSFColor.LIGHT_BLUE.index;
  341. DARK_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  342. DARK_BLUE.BorderBottom = BorderStyle.THIN;
  343. DARK_BLUE.BorderLeft = BorderStyle.THIN;
  344. DARK_BLUE.BorderRight = BorderStyle.THIN;
  345. DARK_BLUE.BorderTop = BorderStyle.THIN;
  346. DARK_BLUE.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  347. ICellStyle LIGHT_CORNFLOWER_BLUE = book.CreateCellStyle();
  348. LIGHT_CORNFLOWER_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  349. LIGHT_CORNFLOWER_BLUE.Alignment = HorizontalAlignment.CENTER;
  350. LIGHT_CORNFLOWER_BLUE.FillForegroundColor = HSSFColor.LIGHT_CORNFLOWER_BLUE.index;
  351. LIGHT_CORNFLOWER_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  352. LIGHT_CORNFLOWER_BLUE.BorderBottom = BorderStyle.THIN;
  353. LIGHT_CORNFLOWER_BLUE.BorderLeft = BorderStyle.THIN;
  354. LIGHT_CORNFLOWER_BLUE.BorderRight = BorderStyle.THIN;
  355. LIGHT_CORNFLOWER_BLUE.BorderTop = BorderStyle.THIN;
  356. LIGHT_CORNFLOWER_BLUE.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  357. ICellStyle GREY_25_PERCENT = book.CreateCellStyle();
  358. GREY_25_PERCENT.VerticalAlignment = VerticalAlignment.CENTER;
  359. GREY_25_PERCENT.Alignment = HorizontalAlignment.CENTER;
  360. GREY_25_PERCENT.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
  361. GREY_25_PERCENT.FillPattern = FillPatternType.SOLID_FOREGROUND;
  362. GREY_25_PERCENT.BorderBottom = BorderStyle.THIN;
  363. GREY_25_PERCENT.BorderLeft = BorderStyle.THIN;
  364. GREY_25_PERCENT.BorderRight = BorderStyle.THIN;
  365. GREY_25_PERCENT.BorderTop = BorderStyle.THIN;
  366. ICellStyle PINK = book.CreateCellStyle();
  367. PINK.VerticalAlignment = VerticalAlignment.CENTER;
  368. PINK.Alignment = HorizontalAlignment.CENTER;
  369. PINK.FillForegroundColor = HSSFColor.LIGHT_ORANGE.index;
  370. PINK.FillPattern = FillPatternType.SOLID_FOREGROUND;
  371. PINK.BorderBottom = BorderStyle.THIN;
  372. PINK.BorderLeft = BorderStyle.THIN;
  373. PINK.BorderRight = BorderStyle.THIN;
  374. PINK.BorderTop = BorderStyle.THIN;
  375. //Excel中的Sheet
  376. ISheet sheet = book.CreateSheet("sheet1");
  377. IRow row = sheet.CreateRow(0);
  378. ICell cell = row.CreateCell(0);
  379. //画第一行的抬头
  380. cell.SetCellValue("组装制程品质数据");
  381. cell.CellStyle = NONE;
  382. CellRangeAddress region = new CellRangeAddress(0, 0, 0, DateNum + 1);
  383. sheet.AddMergedRegion(region);
  384. //第一行的日期标题
  385. row = sheet.CreateRow(1);
  386. cell = row.CreateCell(0);
  387. cell.CellStyle = NONE;
  388. cell.SetCellValue("站点");
  389. cell = row.CreateCell(1);
  390. cell.SetCellValue("类别");
  391. cell.CellStyle = NONE;
  392. for (int i = 0; i < DateNum; i++)
  393. {
  394. cell = row.CreateCell(i + 2);
  395. cell.SetCellValue(begindate.AddDays(i).ToString("MM/dd"));
  396. cell.CellStyle = NONE;
  397. }
  398. //画第一列的工序名称和第二列的类别
  399. //总良率数据
  400. row = sheet.CreateRow(2);
  401. cell = row.CreateCell(0);
  402. cell.SetCellValue("总良率");
  403. cell.CellStyle = LEMON_CHIFFON;
  404. region = new CellRangeAddress(2, 5, 0, 0);
  405. sheet.AddMergedRegion(region);
  406. //总良率的统计数据
  407. for (int i = 0; i < TotalKind.Length; i++)
  408. {
  409. row = sheet.GetRow(i + 2);
  410. if (row == null)
  411. {
  412. row = sheet.CreateRow(i + 2);
  413. }
  414. cell = row.CreateCell(1);
  415. cell.SetCellValue(TotalKind[i]);
  416. cell.CellStyle = LEMON_CHIFFON;
  417. switch (i)
  418. {
  419. case 0:
  420. cell.CellStyle = LIME;
  421. break;
  422. case 1:
  423. cell.CellStyle = TAN;
  424. break;
  425. case 2:
  426. cell.CellStyle = DARK_BLUE;
  427. break;
  428. case 3:
  429. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  430. break;
  431. default:
  432. break;
  433. }
  434. }
  435. //中间的设备测试工序
  436. for (int i = 0; i < Step.Length; i++)
  437. {
  438. //除去前面6行
  439. int rowindex = 6 + i * ContentRow;
  440. row = sheet.CreateRow(rowindex);
  441. cell = row.CreateCell(0);
  442. cell.SetCellValue(Step[i]);
  443. cell.CellStyle = PALE_BLUE;
  444. region = new CellRangeAddress(rowindex, rowindex + ContentRow - 1, 0, 0);
  445. sheet.AddMergedRegion(region);
  446. for (int j = rowindex; j < rowindex + ContentRow; j++)
  447. {
  448. row = sheet.GetRow(j);
  449. if (row == null)
  450. {
  451. row = sheet.CreateRow(j);
  452. }
  453. cell = row.CreateCell(1);
  454. cell.SetCellValue(Kind[j - rowindex]);
  455. switch (j - rowindex)
  456. {
  457. case 0:
  458. cell.CellStyle = GREY_25_PERCENT;
  459. break;
  460. case 1:
  461. cell.CellStyle = PINK;
  462. break;
  463. case 2:
  464. cell.CellStyle = TAN;
  465. break;
  466. case 3:
  467. cell.CellStyle = GOLD;
  468. break;
  469. case 4:
  470. cell.CellStyle = LIGHT_GREEN;
  471. break;
  472. case 5:
  473. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  474. break;
  475. case 6:
  476. cell.CellStyle = LEMON_CHIFFON;
  477. break;
  478. default:
  479. break;
  480. }
  481. }
  482. }
  483. //最后一行外观数据
  484. row = sheet.CreateRow(OutLookRow);
  485. cell = row.CreateCell(0);
  486. cell.SetCellValue("8-外观");
  487. cell.CellStyle = PALE_BLUE;
  488. region = new CellRangeAddress(OutLookRow, OutLookRow + 2, 0, 0);
  489. sheet.AddMergedRegion(region);
  490. //外观的统计数据
  491. for (int i = 0; i < OutLook.Length; i++)
  492. {
  493. row = sheet.GetRow(OutLookRow + i);
  494. if (row == null)
  495. {
  496. row = sheet.CreateRow(OutLookRow + i);
  497. }
  498. cell = row.CreateCell(1);
  499. cell.SetCellValue(OutLook[i]);
  500. cell.CellStyle = PALE_BLUE;
  501. switch (i)
  502. {
  503. case 0:
  504. cell.CellStyle = GREY_25_PERCENT;
  505. break;
  506. case 1:
  507. cell.CellStyle = TAN;
  508. break;
  509. case 2:
  510. cell.CellStyle = LIGHT_GREEN;
  511. break;
  512. default:
  513. break;
  514. }
  515. }
  516. sheet.SetColumnWidth(0, 3700);
  517. for (int i = 0; i < DateNum; i++)
  518. {
  519. double TotalFPY = -1;
  520. double TotalRPY = -1;
  521. double TotalNG = 0;
  522. double TotalIN = 0;
  523. for (int j = 0; j < StepCode.Length; j++)
  524. {
  525. int rowindex = 6 + j * ContentRow;
  526. DataTable dt = PublicMethod.BaseUtil.filterDataTable(DataTable, "sp_date='" + begindate.AddDays(i).ToString("yyyy-MM-dd") + "' and 工序编号='" + StepCode[j] + "'");
  527. if (StepCode[j] != "B_OUTLOOK")
  528. {
  529. for (int k = rowindex; k < rowindex + ContentRow; k++)
  530. {
  531. row = sheet.GetRow(k);
  532. if (row == null)
  533. {
  534. row = sheet.CreateRow(k);
  535. }
  536. cell = row.CreateCell(i + 2);
  537. switch (k - rowindex)
  538. {
  539. case 0:
  540. double 测试数;
  541. if (dt.Rows.Count > 0)
  542. {
  543. if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
  544. {
  545. cell.SetCellValue(测试数);
  546. }
  547. }
  548. cell.CellStyle = GREY_25_PERCENT;
  549. break;
  550. case 1:
  551. double 通过数;
  552. if (dt.Rows.Count > 0)
  553. {
  554. if (double.TryParse(dt.Rows[0]["通过总数"].ToString(), out 通过数))
  555. {
  556. cell.SetCellValue(通过数);
  557. }
  558. }
  559. cell.CellStyle = PINK;
  560. break;
  561. case 2:
  562. double 不良数;
  563. if (dt.Rows.Count > 0)
  564. {
  565. if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
  566. {
  567. cell.SetCellValue(不良数);
  568. TotalNG = TotalNG + 不良数;
  569. }
  570. }
  571. cell.CellStyle = TAN;
  572. break;
  573. case 3:
  574. double 误测通过数;
  575. if (dt.Rows.Count > 0)
  576. {
  577. if (double.TryParse(dt.Rows[0]["误测通过数"].ToString(), out 误测通过数))
  578. {
  579. cell.SetCellValue(误测通过数);
  580. }
  581. }
  582. cell.CellStyle = GOLD;
  583. break;
  584. case 4:
  585. double 误测数;
  586. if (dt.Rows.Count > 0)
  587. {
  588. if (double.TryParse(dt.Rows[0]["误测数"].ToString(), out 误测数))
  589. {
  590. cell.SetCellValue(误测数);
  591. }
  592. }
  593. cell.CellStyle = LIGHT_GREEN;
  594. break;
  595. case 5:
  596. double FPY;
  597. if (dt.Rows.Count > 0)
  598. {
  599. if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
  600. {
  601. cell.SetCellValue(FPY);
  602. //累计所有FPY
  603. if (TotalFPY == -1)
  604. {
  605. TotalFPY = FPY;
  606. }
  607. else
  608. {
  609. TotalFPY = TotalFPY * FPY;
  610. }
  611. }
  612. }
  613. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  614. break;
  615. case 6:
  616. double RPY;
  617. if (dt.Rows.Count > 0)
  618. {
  619. if (double.TryParse(dt.Rows[0]["RPY"].ToString(), out RPY))
  620. {
  621. cell.SetCellValue(RPY);
  622. //累计所有RPY
  623. if (TotalRPY == -1)
  624. {
  625. TotalRPY = RPY;
  626. }
  627. else
  628. {
  629. TotalRPY = TotalRPY * RPY;
  630. }
  631. }
  632. }
  633. cell.CellStyle = LEMON_CHIFFON;
  634. break;
  635. default:
  636. break;
  637. }
  638. }
  639. }
  640. else
  641. {
  642. for (int k = rowindex; k < rowindex + 3; k++)
  643. {
  644. row = sheet.GetRow(k);
  645. if (row == null)
  646. {
  647. row = sheet.CreateRow(k);
  648. }
  649. cell = row.CreateCell(i + 2);
  650. switch (k - rowindex)
  651. {
  652. case 0:
  653. double 测试数;
  654. if (dt.Rows.Count > 0)
  655. {
  656. if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
  657. {
  658. cell.SetCellValue(测试数);
  659. }
  660. }
  661. cell.CellStyle = GREY_25_PERCENT;
  662. break;
  663. case 1:
  664. double 不良数;
  665. if (dt.Rows.Count > 0)
  666. {
  667. if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
  668. {
  669. cell.SetCellValue(不良数);
  670. TotalNG = TotalNG + 不良数;
  671. }
  672. }
  673. cell.CellStyle = TAN;
  674. break;
  675. case 2:
  676. double FPY;
  677. if (dt.Rows.Count > 0)
  678. {
  679. if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
  680. {
  681. cell.SetCellValue(FPY);
  682. //累计所有FPY
  683. if (TotalFPY == -1)
  684. {
  685. TotalFPY = FPY;
  686. }
  687. else
  688. {
  689. TotalFPY = TotalFPY * FPY;
  690. }
  691. }
  692. }
  693. cell.CellStyle = LIGHT_GREEN;
  694. break;
  695. default:
  696. break;
  697. }
  698. }
  699. }
  700. }
  701. DataTable dt1 = PublicMethod.BaseUtil.filterDataTable(DataTable, "sp_date='" + begindate.AddDays(i).ToString("yyyy-MM-dd") + "' and 工序编号='B_LCDBA1'");
  702. double 投入数;
  703. if (dt1.Rows.Count > 0)
  704. {
  705. if (double.TryParse(dt1.Rows[0]["测试数"].ToString(), out 投入数))
  706. {
  707. TotalIN = 投入数;
  708. }
  709. }
  710. //设置最上方的总计数量
  711. row = sheet.GetRow(2);
  712. cell = row.CreateCell(i + 2);
  713. cell.CellStyle = LIME;
  714. cell.SetCellValue(TotalIN);
  715. row = sheet.GetRow(3);
  716. cell = row.CreateCell(i + 2);
  717. cell.CellStyle = TAN;
  718. cell.SetCellValue(TotalNG);
  719. row = sheet.GetRow(4);
  720. cell = row.CreateCell(i + 2);
  721. cell.CellStyle = DARK_BLUE;
  722. cell.SetCellValue(TotalFPY == -1 ? 0 : TotalFPY);
  723. row = sheet.GetRow(5);
  724. cell = row.CreateCell(i + 2);
  725. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  726. cell.SetCellValue(TotalRPY == -1 ? 0 : TotalRPY);
  727. }
  728. for (int i = 0; i < sheet.PhysicalNumberOfRows; i++)
  729. {
  730. sheet.GetRow(i).Height = 300;
  731. }
  732. //将book的内容写入内存流中返回
  733. book.Write(ms);
  734. return ms;
  735. }
  736. }
  737. }