ExcelHandler.cs 40 KB

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