ExcelHandler.cs 39 KB

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