ExcelHandler.cs 50 KB

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