ExcelHandler.cs 39 KB

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