ExcelHandler.cs 49 KB

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