ExcelHandler.cs 52 KB

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