ExcelHandler.cs 38 KB

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