ExcelHandler.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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.HSSF.UserModel;
  9. using NPOI.SS.Formula.Eval;
  10. using NPOI.HSSF.Util;
  11. using NPOI.SS.Util;
  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(System.Windows.Forms.DataGridView 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") || 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().Trim(), 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(System.Windows.Forms.DataGridView DataTable)
  280. {
  281. //创建内存流
  282. MemoryStream ms = new MemoryStream();
  283. //创建一个Book,相当于一个Excel文件
  284. HSSFWorkbook book = new HSSFWorkbook();
  285. //Excel中的Sheet
  286. ISheet sheet = book.CreateSheet("sheet1");
  287. //获取行数量和列数量
  288. int rowNum = DataTable.Rows.Count;
  289. int columnNum = DataTable.Columns.Count;
  290. //设置列的宽度,根据首行的列的内容的长度来设置
  291. for (int i = 0; i < columnNum; i++)
  292. {
  293. int dataLength = DataTable.Columns[i].Width;
  294. if (dataLength == 5)
  295. {
  296. sheet.SetColumnWidth(i, 0);
  297. }
  298. else
  299. {
  300. sheet.SetColumnWidth(i, dataLength*25);
  301. }
  302. }
  303. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  304. IRow row = sheet.CreateRow(0);
  305. //冻结第一行
  306. sheet.CreateFreezePane(0, 1, 0, 1);
  307. ICellStyle style = book.CreateCellStyle();
  308. style.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  309. style.FillPattern = FillPatternType.BIG_SPOTS;
  310. style.FillBackgroundColor = HSSFColor.LIGHT_GREEN.index;
  311. //设置边框
  312. style.BorderBottom = BorderStyle.THICK;
  313. style.BorderLeft = BorderStyle.THICK;
  314. style.BorderRight = BorderStyle.THICK;
  315. style.BorderTop = BorderStyle.THICK;
  316. row.HeightInPoints = 20;
  317. //固定第一行
  318. //row.RowStyle.IsLocked=true;
  319. //给第一行的标签赋值样式和值
  320. for (int j = 0; j < columnNum; j++)
  321. {
  322. row.CreateCell(j);
  323. row.Cells[j].CellStyle = style;
  324. row.Cells[j].CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  325. row.Cells[j].CellStyle.Alignment = HorizontalAlignment.CENTER;
  326. row.Cells[j].SetCellValue(DataTable.Columns[j].HeaderText);
  327. }
  328. //将DataTable的值循环赋值给book,Aligment设置居中
  329. //之前已经画了带颜色的第一行,所以从i=1开始画
  330. for (int i = 0; i < rowNum; i++)
  331. {
  332. IRow row1 = sheet.CreateRow(i + 1);
  333. row1.HeightInPoints = 20;
  334. for (int j = 0; j < columnNum; j++)
  335. {
  336. row1.CreateCell(j);
  337. row1.Cells[j].SetCellValue(DataTable.Rows[i].Cells[j].Value==null?"":DataTable.Rows[i].Cells[j].Value.ToString());
  338. row1.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  339. }
  340. }
  341. //将book的内容写入内存流中返回
  342. book.Write(ms);
  343. return ms;
  344. }
  345. /// <summary>
  346. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  347. /// </summary>
  348. /// <param name="DataTable"></param>
  349. /// <returns></returns>
  350. public MemoryStream DataTableToExcel_BAIDU(DataTable DataTable, DateTime begindate, int DateNum)
  351. {
  352. string[] Step = new[] { "1-MT1", "2-MT2", "3-MMI", "4-RSA(耦合)", "5-AUD(曲线)", "6-SCW(写号)", "7-SCK(验号)" };
  353. string[] StepCode = new[] { "B_MT1", "B_MT2", "B_MMI", "B_RSA", "B_AUD", "B_WRITE", "B_SN", "B_OUTLOOK" };
  354. string[] Kind = new[] { "测试数", "通过数", "不良数", "误测通过数", "误测数", "FPY", "RPY" };
  355. string[] TotalKind = new[] { "总投入数", "总不良数", "FPY", "RPY" };
  356. string[] OutLook = new[] { "测试数", "不良数", "FPY" };
  357. //每行的内容
  358. int ContentRow = 7;
  359. //外观的展示的行
  360. int OutLookRow = 55;
  361. MemoryStream ms = new MemoryStream();
  362. //创建一个Book,相当于一个Excel文件
  363. HSSFWorkbook book = new HSSFWorkbook();
  364. ICellStyle NONE = book.CreateCellStyle();
  365. NONE.VerticalAlignment = VerticalAlignment.CENTER;
  366. NONE.Alignment = HorizontalAlignment.CENTER;
  367. NONE.BorderBottom = BorderStyle.THIN;
  368. NONE.BorderLeft = BorderStyle.THIN;
  369. NONE.BorderRight = BorderStyle.THIN;
  370. NONE.BorderTop = BorderStyle.THIN;
  371. ICellStyle TAN = book.CreateCellStyle();
  372. TAN.VerticalAlignment = VerticalAlignment.CENTER;
  373. TAN.Alignment = HorizontalAlignment.CENTER;
  374. TAN.FillForegroundColor = HSSFColor.TAN.index;
  375. TAN.FillPattern = FillPatternType.SOLID_FOREGROUND;
  376. TAN.BorderBottom = BorderStyle.THIN;
  377. TAN.BorderLeft = BorderStyle.THIN;
  378. TAN.BorderRight = BorderStyle.THIN;
  379. TAN.BorderTop = BorderStyle.THIN;
  380. ICellStyle PALE_BLUE = book.CreateCellStyle();
  381. PALE_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  382. PALE_BLUE.Alignment = HorizontalAlignment.CENTER;
  383. PALE_BLUE.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  384. PALE_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  385. PALE_BLUE.BorderBottom = BorderStyle.THIN;
  386. PALE_BLUE.BorderLeft = BorderStyle.THIN;
  387. PALE_BLUE.BorderRight = BorderStyle.THIN;
  388. PALE_BLUE.BorderTop = BorderStyle.THIN;
  389. ICellStyle LIME = book.CreateCellStyle();
  390. LIME.VerticalAlignment = VerticalAlignment.CENTER;
  391. LIME.Alignment = HorizontalAlignment.CENTER;
  392. LIME.FillForegroundColor = HSSFColor.LIME.index;
  393. LIME.FillPattern = FillPatternType.SOLID_FOREGROUND;
  394. LIME.BorderBottom = BorderStyle.THIN;
  395. LIME.BorderLeft = BorderStyle.THIN;
  396. LIME.BorderRight = BorderStyle.THIN;
  397. LIME.BorderTop = BorderStyle.THIN;
  398. ICellStyle LEMON_CHIFFON = book.CreateCellStyle();
  399. LEMON_CHIFFON.VerticalAlignment = VerticalAlignment.CENTER;
  400. LEMON_CHIFFON.Alignment = HorizontalAlignment.CENTER;
  401. LEMON_CHIFFON.FillForegroundColor = HSSFColor.LEMON_CHIFFON.index;
  402. LEMON_CHIFFON.FillPattern = FillPatternType.SOLID_FOREGROUND;
  403. LEMON_CHIFFON.BorderBottom = BorderStyle.THIN;
  404. LEMON_CHIFFON.BorderLeft = BorderStyle.THIN;
  405. LEMON_CHIFFON.BorderRight = BorderStyle.THIN;
  406. LEMON_CHIFFON.BorderTop = BorderStyle.THIN;
  407. LEMON_CHIFFON.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  408. ICellStyle GOLD = book.CreateCellStyle();
  409. GOLD.VerticalAlignment = VerticalAlignment.CENTER;
  410. GOLD.Alignment = HorizontalAlignment.CENTER;
  411. GOLD.FillForegroundColor = HSSFColor.GOLD.index;
  412. GOLD.FillPattern = FillPatternType.SOLID_FOREGROUND;
  413. GOLD.BorderBottom = BorderStyle.THIN;
  414. GOLD.BorderLeft = BorderStyle.THIN;
  415. GOLD.BorderRight = BorderStyle.THIN;
  416. GOLD.BorderTop = BorderStyle.THIN;
  417. ICellStyle LIGHT_GREEN = book.CreateCellStyle();
  418. LIGHT_GREEN.VerticalAlignment = VerticalAlignment.CENTER;
  419. LIGHT_GREEN.Alignment = HorizontalAlignment.CENTER;
  420. LIGHT_GREEN.FillForegroundColor = HSSFColor.LIGHT_GREEN.index;
  421. LIGHT_GREEN.FillPattern = FillPatternType.SOLID_FOREGROUND;
  422. LIGHT_GREEN.BorderBottom = BorderStyle.THIN;
  423. LIGHT_GREEN.BorderLeft = BorderStyle.THIN;
  424. LIGHT_GREEN.BorderRight = BorderStyle.THIN;
  425. LIGHT_GREEN.BorderTop = BorderStyle.THIN;
  426. //LIGHT_GREEN.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  427. ICellStyle DARK_BLUE = book.CreateCellStyle();
  428. DARK_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  429. DARK_BLUE.Alignment = HorizontalAlignment.CENTER;
  430. DARK_BLUE.FillForegroundColor = HSSFColor.LIGHT_BLUE.index;
  431. DARK_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  432. DARK_BLUE.BorderBottom = BorderStyle.THIN;
  433. DARK_BLUE.BorderLeft = BorderStyle.THIN;
  434. DARK_BLUE.BorderRight = BorderStyle.THIN;
  435. DARK_BLUE.BorderTop = BorderStyle.THIN;
  436. DARK_BLUE.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  437. ICellStyle LIGHT_CORNFLOWER_BLUE = book.CreateCellStyle();
  438. LIGHT_CORNFLOWER_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
  439. LIGHT_CORNFLOWER_BLUE.Alignment = HorizontalAlignment.CENTER;
  440. LIGHT_CORNFLOWER_BLUE.FillForegroundColor = HSSFColor.LIGHT_CORNFLOWER_BLUE.index;
  441. LIGHT_CORNFLOWER_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
  442. LIGHT_CORNFLOWER_BLUE.BorderBottom = BorderStyle.THIN;
  443. LIGHT_CORNFLOWER_BLUE.BorderLeft = BorderStyle.THIN;
  444. LIGHT_CORNFLOWER_BLUE.BorderRight = BorderStyle.THIN;
  445. LIGHT_CORNFLOWER_BLUE.BorderTop = BorderStyle.THIN;
  446. LIGHT_CORNFLOWER_BLUE.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  447. ICellStyle GREY_25_PERCENT = book.CreateCellStyle();
  448. GREY_25_PERCENT.VerticalAlignment = VerticalAlignment.CENTER;
  449. GREY_25_PERCENT.Alignment = HorizontalAlignment.CENTER;
  450. GREY_25_PERCENT.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
  451. GREY_25_PERCENT.FillPattern = FillPatternType.SOLID_FOREGROUND;
  452. GREY_25_PERCENT.BorderBottom = BorderStyle.THIN;
  453. GREY_25_PERCENT.BorderLeft = BorderStyle.THIN;
  454. GREY_25_PERCENT.BorderRight = BorderStyle.THIN;
  455. GREY_25_PERCENT.BorderTop = BorderStyle.THIN;
  456. ICellStyle PINK = book.CreateCellStyle();
  457. PINK.VerticalAlignment = VerticalAlignment.CENTER;
  458. PINK.Alignment = HorizontalAlignment.CENTER;
  459. PINK.FillForegroundColor = HSSFColor.LIGHT_ORANGE.index;
  460. PINK.FillPattern = FillPatternType.SOLID_FOREGROUND;
  461. PINK.BorderBottom = BorderStyle.THIN;
  462. PINK.BorderLeft = BorderStyle.THIN;
  463. PINK.BorderRight = BorderStyle.THIN;
  464. PINK.BorderTop = BorderStyle.THIN;
  465. //Excel中的Sheet
  466. ISheet sheet = book.CreateSheet("sheet1");
  467. IRow row = sheet.CreateRow(0);
  468. ICell cell = row.CreateCell(0);
  469. //画第一行的抬头
  470. cell.SetCellValue("组装制程品质数据");
  471. cell.CellStyle = NONE;
  472. CellRangeAddress region = new CellRangeAddress(0, 0, 0, DateNum + 1);
  473. sheet.AddMergedRegion(region);
  474. //第一行的日期标题
  475. row = sheet.CreateRow(1);
  476. cell = row.CreateCell(0);
  477. cell.CellStyle = NONE;
  478. cell.SetCellValue("站点");
  479. cell = row.CreateCell(1);
  480. cell.SetCellValue("类别");
  481. cell.CellStyle = NONE;
  482. for (int i = 0; i < DateNum; i++)
  483. {
  484. cell = row.CreateCell(i + 2);
  485. cell.SetCellValue(begindate.AddDays(i).ToString("MM/dd"));
  486. cell.CellStyle = NONE;
  487. }
  488. //画第一列的工序名称和第二列的类别
  489. //总良率数据
  490. row = sheet.CreateRow(2);
  491. cell = row.CreateCell(0);
  492. cell.SetCellValue("总良率");
  493. cell.CellStyle = LEMON_CHIFFON;
  494. region = new CellRangeAddress(2, 5, 0, 0);
  495. sheet.AddMergedRegion(region);
  496. //总良率的统计数据
  497. for (int i = 0; i < TotalKind.Length; i++)
  498. {
  499. row = sheet.GetRow(i + 2);
  500. if (row == null)
  501. {
  502. row = sheet.CreateRow(i + 2);
  503. }
  504. cell = row.CreateCell(1);
  505. cell.SetCellValue(TotalKind[i]);
  506. cell.CellStyle = LEMON_CHIFFON;
  507. switch (i)
  508. {
  509. case 0:
  510. cell.CellStyle = LIME;
  511. break;
  512. case 1:
  513. cell.CellStyle = TAN;
  514. break;
  515. case 2:
  516. cell.CellStyle = DARK_BLUE;
  517. break;
  518. case 3:
  519. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  520. break;
  521. default:
  522. break;
  523. }
  524. }
  525. //中间的设备测试工序
  526. for (int i = 0; i < Step.Length; i++)
  527. {
  528. //除去前面6行
  529. int rowindex = 6 + i * ContentRow;
  530. row = sheet.CreateRow(rowindex);
  531. cell = row.CreateCell(0);
  532. cell.SetCellValue(Step[i]);
  533. cell.CellStyle = PALE_BLUE;
  534. region = new CellRangeAddress(rowindex, rowindex + ContentRow - 1, 0, 0);
  535. sheet.AddMergedRegion(region);
  536. for (int j = rowindex; j < rowindex + ContentRow; j++)
  537. {
  538. row = sheet.GetRow(j);
  539. if (row == null)
  540. {
  541. row = sheet.CreateRow(j);
  542. }
  543. cell = row.CreateCell(1);
  544. cell.SetCellValue(Kind[j - rowindex]);
  545. switch (j - rowindex)
  546. {
  547. case 0:
  548. cell.CellStyle = GREY_25_PERCENT;
  549. break;
  550. case 1:
  551. cell.CellStyle = PINK;
  552. break;
  553. case 2:
  554. cell.CellStyle = TAN;
  555. break;
  556. case 3:
  557. cell.CellStyle = GOLD;
  558. break;
  559. case 4:
  560. cell.CellStyle = LIGHT_GREEN;
  561. break;
  562. case 5:
  563. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  564. break;
  565. case 6:
  566. cell.CellStyle = LEMON_CHIFFON;
  567. break;
  568. default:
  569. break;
  570. }
  571. }
  572. }
  573. //最后一行外观数据
  574. row = sheet.CreateRow(OutLookRow);
  575. cell = row.CreateCell(0);
  576. cell.SetCellValue("8-外观");
  577. cell.CellStyle = PALE_BLUE;
  578. region = new CellRangeAddress(OutLookRow, OutLookRow + 2, 0, 0);
  579. sheet.AddMergedRegion(region);
  580. //外观的统计数据
  581. for (int i = 0; i < OutLook.Length; i++)
  582. {
  583. row = sheet.GetRow(OutLookRow + i);
  584. if (row == null)
  585. {
  586. row = sheet.CreateRow(OutLookRow + i);
  587. }
  588. cell = row.CreateCell(1);
  589. cell.SetCellValue(OutLook[i]);
  590. cell.CellStyle = PALE_BLUE;
  591. switch (i)
  592. {
  593. case 0:
  594. cell.CellStyle = GREY_25_PERCENT;
  595. break;
  596. case 1:
  597. cell.CellStyle = TAN;
  598. break;
  599. case 2:
  600. cell.CellStyle = LIGHT_GREEN;
  601. break;
  602. default:
  603. break;
  604. }
  605. }
  606. sheet.SetColumnWidth(0, 3700);
  607. for (int i = 0; i < DateNum; i++)
  608. {
  609. double TotalFPY = -1;
  610. double TotalRPY = -1;
  611. double TotalNG = 0;
  612. double TotalIN = 0;
  613. for (int j = 0; j < StepCode.Length; j++)
  614. {
  615. int rowindex = 6 + j * ContentRow;
  616. DataTable dt = PublicMethod.BaseUtil.filterDataTable(DataTable, "sp_date='" + begindate.AddDays(i).ToString("yyyy-MM-dd") + "' and 工序编号='" + StepCode[j] + "'");
  617. if (StepCode[j] != "B_OUTLOOK")
  618. {
  619. for (int k = rowindex; k < rowindex + ContentRow; k++)
  620. {
  621. row = sheet.GetRow(k);
  622. if (row == null)
  623. {
  624. row = sheet.CreateRow(k);
  625. }
  626. cell = row.CreateCell(i + 2);
  627. switch (k - rowindex)
  628. {
  629. case 0:
  630. double 测试数;
  631. if (dt.Rows.Count > 0)
  632. {
  633. if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
  634. {
  635. cell.SetCellValue(测试数);
  636. }
  637. }
  638. cell.CellStyle = GREY_25_PERCENT;
  639. break;
  640. case 1:
  641. double 通过数;
  642. if (dt.Rows.Count > 0)
  643. {
  644. if (double.TryParse(dt.Rows[0]["通过总数"].ToString(), out 通过数))
  645. {
  646. cell.SetCellValue(通过数);
  647. }
  648. }
  649. cell.CellStyle = PINK;
  650. break;
  651. case 2:
  652. double 不良数;
  653. if (dt.Rows.Count > 0)
  654. {
  655. if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
  656. {
  657. cell.SetCellValue(不良数);
  658. TotalNG = TotalNG + 不良数;
  659. }
  660. }
  661. cell.CellStyle = TAN;
  662. break;
  663. case 3:
  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 = GOLD;
  673. break;
  674. case 4:
  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 = LIGHT_GREEN;
  684. break;
  685. case 5:
  686. double FPY;
  687. if (dt.Rows.Count > 0)
  688. {
  689. if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
  690. {
  691. cell.SetCellValue(FPY);
  692. //累计所有FPY
  693. if (TotalFPY == -1)
  694. {
  695. TotalFPY = FPY;
  696. }
  697. else
  698. {
  699. TotalFPY = TotalFPY * FPY;
  700. }
  701. }
  702. }
  703. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  704. break;
  705. case 6:
  706. double RPY;
  707. if (dt.Rows.Count > 0)
  708. {
  709. if (double.TryParse(dt.Rows[0]["RPY"].ToString(), out RPY))
  710. {
  711. cell.SetCellValue(RPY);
  712. //累计所有RPY
  713. if (TotalRPY == -1)
  714. {
  715. TotalRPY = RPY;
  716. }
  717. else
  718. {
  719. TotalRPY = TotalRPY * RPY;
  720. }
  721. }
  722. }
  723. cell.CellStyle = LEMON_CHIFFON;
  724. break;
  725. default:
  726. break;
  727. }
  728. }
  729. }
  730. else
  731. {
  732. for (int k = rowindex; k < rowindex + 3; k++)
  733. {
  734. row = sheet.GetRow(k);
  735. if (row == null)
  736. {
  737. row = sheet.CreateRow(k);
  738. }
  739. cell = row.CreateCell(i + 2);
  740. switch (k - rowindex)
  741. {
  742. case 0:
  743. double 测试数;
  744. if (dt.Rows.Count > 0)
  745. {
  746. if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
  747. {
  748. cell.SetCellValue(测试数);
  749. }
  750. }
  751. cell.CellStyle = GREY_25_PERCENT;
  752. break;
  753. case 1:
  754. double 不良数;
  755. if (dt.Rows.Count > 0)
  756. {
  757. if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
  758. {
  759. cell.SetCellValue(不良数);
  760. TotalNG = TotalNG + 不良数;
  761. }
  762. }
  763. cell.CellStyle = TAN;
  764. break;
  765. case 2:
  766. double FPY;
  767. if (dt.Rows.Count > 0)
  768. {
  769. if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
  770. {
  771. cell.SetCellValue(FPY);
  772. //累计所有FPY
  773. if (TotalFPY == -1)
  774. {
  775. TotalFPY = FPY;
  776. }
  777. else
  778. {
  779. TotalFPY = TotalFPY * FPY;
  780. }
  781. }
  782. }
  783. cell.CellStyle = LIGHT_GREEN;
  784. break;
  785. default:
  786. break;
  787. }
  788. }
  789. }
  790. }
  791. DataTable dt1 = PublicMethod.BaseUtil.filterDataTable(DataTable, "sp_date='" + begindate.AddDays(i).ToString("yyyy-MM-dd") + "' and 工序编号='B_LCDBA1'");
  792. double 投入数;
  793. if (dt1.Rows.Count > 0)
  794. {
  795. if (double.TryParse(dt1.Rows[0]["测试数"].ToString(), out 投入数))
  796. {
  797. TotalIN = 投入数;
  798. }
  799. }
  800. //设置最上方的总计数量
  801. row = sheet.GetRow(2);
  802. cell = row.CreateCell(i + 2);
  803. cell.CellStyle = LIME;
  804. cell.SetCellValue(TotalIN);
  805. row = sheet.GetRow(3);
  806. cell = row.CreateCell(i + 2);
  807. cell.CellStyle = TAN;
  808. cell.SetCellValue(TotalNG);
  809. row = sheet.GetRow(4);
  810. cell = row.CreateCell(i + 2);
  811. cell.CellStyle = DARK_BLUE;
  812. cell.SetCellValue(TotalFPY == -1 ? 0 : TotalFPY);
  813. row = sheet.GetRow(5);
  814. cell = row.CreateCell(i + 2);
  815. cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
  816. cell.SetCellValue(TotalRPY == -1 ? 0 : TotalRPY);
  817. }
  818. for (int i = 0; i < sheet.PhysicalNumberOfRows; i++)
  819. {
  820. sheet.GetRow(i).Height = 300;
  821. }
  822. //将book的内容写入内存流中返回
  823. book.Write(ms);
  824. return ms;
  825. }
  826. }
  827. }