ExcelHandler.cs 49 KB

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