ExcelHandler.cs 44 KB

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