ExcelHandler.cs 35 KB

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