ExcelHandler.cs 39 KB

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