説明なし

NPOIHelper.cs 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. 
  2. using System;
  3. using System.Data;
  4. using System.IO;
  5. using System.Text;
  6. using System.Web;
  7. using NPOI.HSSF.UserModel;
  8. using NPOI.SS.UserModel;
  9. using NPOI.SS.Util;
  10. using NPOI.XSSF.UserModel;
  11. using System.Collections.Generic;
  12. namespace CallCenter.Utility
  13. {
  14. public class NPOIHelper
  15. {
  16. private string _title;
  17. private string _sheetName;
  18. private string _filePath;
  19. /// <summary>
  20. /// 导出到Excel
  21. /// </summary>
  22. /// <param name="table"></param>
  23. /// <returns></returns>
  24. public bool ToExcel(DataTable table, string[] columns = null)
  25. {
  26. FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  27. IWorkbook workBook = new HSSFWorkbook();
  28. if (string.IsNullOrWhiteSpace(this._sheetName))
  29. {
  30. this._sheetName = "sheet1";
  31. }
  32. ISheet sheet = workBook.CreateSheet(this._sheetName);
  33. //处理表格标题
  34. IRow row = sheet.CreateRow(0);
  35. row.CreateCell(0).SetCellValue(this._title);
  36. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1));
  37. row.Height = 500;
  38. ICellStyle cellStyle = workBook.CreateCellStyle();
  39. IFont font = workBook.CreateFont();
  40. font.FontName = "微软雅黑";
  41. font.FontHeightInPoints = 17;
  42. cellStyle.SetFont(font);
  43. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  44. cellStyle.Alignment = HorizontalAlignment.Center;
  45. row.Cells[0].CellStyle = cellStyle;
  46. //处理表格列头
  47. row = sheet.CreateRow(1);
  48. if (columns == null)
  49. {
  50. for (int i = 0; i < table.Columns.Count; i++)
  51. {
  52. row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
  53. row.Height = 350;
  54. sheet.AutoSizeColumn(i);
  55. }
  56. }
  57. else
  58. {
  59. for (int i = 0; i < columns.Length; i++)
  60. {
  61. row.CreateCell(i).SetCellValue(columns[i]);
  62. row.Height = 350;
  63. sheet.AutoSizeColumn(i);
  64. }
  65. }
  66. //处理数据内容
  67. for (int i = 0; i < table.Rows.Count; i++)
  68. {
  69. row = sheet.CreateRow(2 + i);
  70. row.Height = 250;
  71. for (int j = 0; j < table.Columns.Count; j++)
  72. {
  73. row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
  74. sheet.SetColumnWidth(j, 256 * 15);
  75. }
  76. }
  77. //写入数据流
  78. workBook.Write(fs);
  79. fs.Flush();
  80. fs.Close();
  81. return true;
  82. }
  83. /// <summary>
  84. /// 导出到Excel
  85. /// </summary>
  86. /// <param name="table"></param>
  87. /// <param name="title"></param>
  88. /// <param name="sheetName">空字符串或null的话默认sheet1</param>
  89. /// <param name="columns">自定义表格列头,默认null</param>
  90. /// <returns></returns>
  91. public bool ToExcel(DataTable table, string title, string sheetName, string filePath, string[] columns = null)
  92. {
  93. this._title = title;
  94. this._sheetName = sheetName;
  95. this._filePath = filePath;
  96. return ToExcel(table, columns);
  97. }
  98. /// <summary>
  99. /// 弹出下载框导出excel
  100. /// </summary>
  101. /// <param name="Name"></param>
  102. /// <param name="dt"></param>
  103. /// <returns></returns>
  104. public string ExportToExcel(string Name, DataTable dt, string[] cols = null)
  105. {
  106. try
  107. {
  108. //if (dt.Rows.Count > 0)
  109. //{
  110. HSSFWorkbook workbook = new HSSFWorkbook();
  111. ISheet sheet = workbook.CreateSheet("Sheet1");
  112. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  113. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  114. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  115. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  116. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  117. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  118. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  119. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  120. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  121. //字体
  122. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  123. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  124. headerfont.FontHeightInPoints = 12;
  125. HeadercellStyle.SetFont(headerfont);
  126. //用column name 作为列名
  127. int icolIndex = 0;
  128. IRow headerRow = sheet.CreateRow(0);
  129. if (cols == null || (cols != null && cols.Length == 0))
  130. {
  131. foreach (DataColumn dc in dt.Columns)
  132. {
  133. ICell cell = headerRow.CreateCell(icolIndex);
  134. cell.SetCellValue(dc.ColumnName);
  135. cell.CellStyle = HeadercellStyle;
  136. icolIndex++;
  137. }
  138. }
  139. else
  140. {
  141. foreach (string dc in cols)
  142. {
  143. ICell cell = headerRow.CreateCell(icolIndex);
  144. cell.SetCellValue(dc);
  145. cell.CellStyle = HeadercellStyle;
  146. icolIndex++;
  147. }
  148. }
  149. ICellStyle cellStyle = workbook.CreateCellStyle();
  150. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  151. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  152. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  153. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  154. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  155. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  156. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  157. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  158. cellStyle.SetFont(cellfont);
  159. //建立内容行
  160. int iRowIndex = 0;
  161. foreach (DataRow dr in dt.Rows)
  162. {
  163. int iCellIndex = 0;
  164. IRow irow = sheet.CreateRow(iRowIndex + 1);
  165. for (int i = 0; i < dt.Columns.Count; i++)
  166. {
  167. string strsj = string.Empty;
  168. if (dr[i] != null)
  169. {
  170. strsj = dr[i].ToString();
  171. }
  172. ICell cell = irow.CreateCell(iCellIndex);
  173. cell.SetCellValue(strsj);
  174. cell.CellStyle = cellStyle;
  175. iCellIndex++;
  176. }
  177. iRowIndex++;
  178. }
  179. //自适应列宽度
  180. for (int i = 0; i < icolIndex; i++)
  181. {
  182. sheet.AutoSizeColumn(i);
  183. }
  184. using (MemoryStream ms = new MemoryStream())
  185. {
  186. workbook.Write(ms);
  187. HttpContext curContext = HttpContext.Current;
  188. // 设置编码和附件格式
  189. curContext.Response.ContentType = "application/vnd.ms-excel";
  190. curContext.Response.ContentEncoding = Encoding.UTF8;
  191. curContext.Response.Charset = "";
  192. curContext.Response.AppendHeader("Content-Disposition",
  193. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  194. curContext.Response.BinaryWrite(ms.GetBuffer());
  195. workbook = null;
  196. ms.Close();
  197. ms.Dispose();
  198. curContext.Response.End();
  199. }
  200. //}
  201. return "";
  202. }
  203. catch
  204. {
  205. return "导出失败!";
  206. }
  207. }
  208. /// <summary>
  209. /// 合并单元格
  210. /// </summary>
  211. /// <param name="sheet">要合并单元格所在的sheet</param>
  212. /// <param name="rowstart">开始行的索引</param>
  213. /// <param name="rowend">结束行的索引</param>
  214. /// <param name="colstart">开始列的索引</param>
  215. /// <param name="colend">结束列的索引</param>
  216. public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
  217. {
  218. CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
  219. sheet.AddMergedRegion(cellRangeAddress);
  220. }
  221. /// <summary>
  222. /// 工单类型弹出下载框导出excel
  223. /// </summary>
  224. /// <param name="Name"></param>
  225. /// <param name="dt"></param>
  226. /// <param name="typeclass">仪器或试剂</param>
  227. /// <returns></returns>
  228. public string ExportToExcelForGDLX(string Name, DataTable dt, string typeclass, List<string> colnames, List<int> erows, List<string> secolnames)
  229. {
  230. try
  231. {
  232. HSSFWorkbook workbook = new HSSFWorkbook();
  233. ISheet sheet = workbook.CreateSheet("Sheet1");
  234. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  235. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  236. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  237. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  238. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  239. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  240. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  241. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  242. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  243. //字体
  244. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  245. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  246. headerfont.FontHeightInPoints = 12;
  247. HeadercellStyle.SetFont(headerfont);
  248. //用column name 作为列名
  249. int icolIndex = 0;
  250. IRow headerRow = sheet.CreateRow(0);
  251. for (int i = 0; i < colnames.Count; i++)
  252. {
  253. ICell cell = headerRow.CreateCell(i);
  254. cell.SetCellValue(colnames[i]);
  255. cell.CellStyle = HeadercellStyle;
  256. //SetCellRangeAddress(sheet, 0, 0, erows[i * 2], erows[i * 2 + 1]);
  257. }
  258. for (int k = 0; k < erows.Count / 2; k++)
  259. {
  260. SetCellRangeAddress(sheet, 0, 0, erows[k * 2], erows[k * 2 + 1]);
  261. }
  262. //添加第二行标题
  263. IRow SecRow = sheet.CreateRow(1);
  264. for (int i = 0; i < secolnames.Count; i++)
  265. {
  266. ICell cell = SecRow.CreateCell(i);
  267. cell.SetCellValue(secolnames[i].ToString());
  268. }
  269. ICellStyle cellStyle = workbook.CreateCellStyle();
  270. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  271. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  272. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  273. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  274. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  275. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  276. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  277. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  278. cellStyle.SetFont(cellfont);
  279. //建立内容行
  280. int iRowIndex = 0;
  281. foreach (DataRow dr in dt.Rows)
  282. {
  283. int iCellIndex = 0;
  284. IRow irow = sheet.CreateRow(iRowIndex + 2);
  285. for (int i = 0; i < dt.Columns.Count; i++)
  286. {
  287. string strsj = string.Empty;
  288. if (dr[i] != null)
  289. {
  290. strsj = dr[i].ToString();
  291. }
  292. ICell cell = irow.CreateCell(iCellIndex);
  293. cell.SetCellValue(strsj);
  294. cell.CellStyle = cellStyle;
  295. iCellIndex++;
  296. }
  297. iRowIndex++;
  298. }
  299. //自适应列宽度
  300. for (int i = 0; i < icolIndex; i++)
  301. {
  302. sheet.AutoSizeColumn(i);
  303. }
  304. using (MemoryStream ms = new MemoryStream())
  305. {
  306. workbook.Write(ms);
  307. HttpContext curContext = HttpContext.Current;
  308. // 设置编码和附件格式
  309. curContext.Response.ContentType = "application/vnd.ms-excel";
  310. curContext.Response.ContentEncoding = Encoding.UTF8;
  311. curContext.Response.Charset = "";
  312. curContext.Response.AppendHeader("Content-Disposition",
  313. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  314. curContext.Response.BinaryWrite(ms.GetBuffer());
  315. workbook = null;
  316. ms.Close();
  317. ms.Dispose();
  318. curContext.Response.End();
  319. }
  320. return "";
  321. }
  322. catch (Exception e)
  323. {
  324. return "导出失败!" + e.Message;
  325. }
  326. }
  327. /// <summary>
  328. /// 导入excel转换为datatable
  329. /// </summary>
  330. /// <param name="upfile"></param>
  331. /// <param name="headrow"></param>
  332. /// <returns></returns>
  333. public DataTable ExcelToTable(HttpPostedFile upfile, int headrow)
  334. {
  335. DataTable dt = new DataTable();
  336. IWorkbook workbook = null;
  337. Stream stream = upfile.InputStream;
  338. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  339. if (suffix == "xlsx") // 2007版本
  340. {
  341. workbook = new XSSFWorkbook(stream);
  342. }
  343. else if (suffix == "xls") // 2003版本
  344. {
  345. workbook = new HSSFWorkbook(stream);
  346. }
  347. //获取excel的第一个sheet
  348. ISheet sheet = workbook.GetSheetAt(0);
  349. //获取sheet的第一行
  350. IRow headerRow = sheet.GetRow(headrow);
  351. //一行最后一个方格的编号 即总的列数
  352. int cellCount = headerRow.LastCellNum;
  353. //最后一列的标号 即总的行数
  354. int rowCount = sheet.LastRowNum;
  355. //列名
  356. for (int i = 0; i < cellCount; i++)
  357. {
  358. dt.Columns.Add(headerRow.GetCell(i).ToString());
  359. }
  360. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  361. {
  362. DataRow dr = dt.NewRow();
  363. IRow row = sheet.GetRow(i);
  364. if (row != null)
  365. {
  366. for (int j = row.FirstCellNum; j < cellCount; j++)
  367. {
  368. if (row.GetCell(j) != null)
  369. {
  370. dr[j] = row.GetCell(j).ToString();
  371. }
  372. }
  373. dt.Rows.Add(dr);
  374. }
  375. }
  376. sheet = null;
  377. workbook = null;
  378. return dt;
  379. }
  380. /// <summary>
  381. /// 导入excel转换为datatable
  382. /// </summary>
  383. /// <param name="upfile"></param>
  384. /// <param name="headrow"></param>
  385. /// <returns></returns>
  386. public DataTable ExcelToTable(string fileName, int headrow)
  387. {
  388. DataTable dt = new DataTable();
  389. IWorkbook workbook = null;
  390. Stream stream = new FileStream(fileName, FileMode.OpenOrCreate);
  391. string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
  392. if (suffix == "xlsx") // 2007版本
  393. {
  394. workbook = new XSSFWorkbook(stream);
  395. }
  396. else if (suffix == "xls") // 2003版本
  397. {
  398. workbook = new HSSFWorkbook(stream);
  399. }
  400. //获取excel的第一个sheet
  401. ISheet sheet = workbook.GetSheetAt(0);
  402. //获取sheet的第一行
  403. IRow headerRow = sheet.GetRow(headrow);
  404. //一行最后一个方格的编号 即总的列数
  405. int cellCount = headerRow.LastCellNum;
  406. //最后一列的标号 即总的行数
  407. int rowCount = sheet.LastRowNum;
  408. //列名
  409. for (int i = 0; i < cellCount; i++)
  410. {
  411. dt.Columns.Add(headerRow.GetCell(i).ToString());
  412. }
  413. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  414. {
  415. DataRow dr = dt.NewRow();
  416. IRow row = sheet.GetRow(i);
  417. for (int j = row.FirstCellNum; j < cellCount; j++)
  418. {
  419. if (row.GetCell(j) != null)
  420. {
  421. dr[j] = row.GetCell(j).ToString();
  422. }
  423. }
  424. dt.Rows.Add(dr);
  425. }
  426. sheet = null;
  427. workbook = null;
  428. return dt;
  429. }
  430. #region 弹出下载框导出excel(数据第一行为标题)
  431. /// <summary>
  432. /// 弹出下载框导出excel(数据第一行为标题)
  433. /// </summary>
  434. /// <param name="Name"></param>
  435. /// <param name="dt"></param>
  436. /// <returns></returns>
  437. public string ExportToExcel2(string Name, DataTable dt, string[] cols = null)
  438. {
  439. try
  440. {
  441. //if (dt.Rows.Count > 0)
  442. //{
  443. HSSFWorkbook workbook = new HSSFWorkbook();
  444. ISheet sheet = workbook.CreateSheet("Sheet1");
  445. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  446. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  447. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  448. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  449. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  450. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  451. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  452. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  453. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  454. //字体
  455. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  456. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  457. headerfont.FontHeightInPoints = 12;
  458. HeadercellStyle.SetFont(headerfont);
  459. //用column name 作为列名
  460. int icolIndex = 0;
  461. IRow headerRow = sheet.CreateRow(0);
  462. if (cols == null || (cols != null && cols.Length == 0))
  463. {
  464. //foreach (DataColumn dc in dt.Columns)
  465. //{
  466. // ICell cell = headerRow.CreateCell(icolIndex);
  467. // cell.SetCellValue(dc.ColumnName);
  468. // cell.CellStyle = HeadercellStyle;
  469. // icolIndex++;
  470. //}
  471. for (int i = 0; i < dt.Columns.Count; i++)
  472. {
  473. ICell cell = headerRow.CreateCell(icolIndex);
  474. cell.SetCellValue(dt.Rows[0][i].ToString());
  475. cell.CellStyle = HeadercellStyle;
  476. icolIndex++;
  477. }
  478. }
  479. else
  480. {
  481. foreach (string dc in cols)
  482. {
  483. ICell cell = headerRow.CreateCell(icolIndex);
  484. cell.SetCellValue(dc);
  485. cell.CellStyle = HeadercellStyle;
  486. icolIndex++;
  487. }
  488. }
  489. ICellStyle cellStyle = workbook.CreateCellStyle();
  490. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  491. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  492. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  493. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  494. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  495. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  496. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  497. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  498. cellStyle.SetFont(cellfont);
  499. //建立内容行
  500. int iRowIndex = 0;
  501. foreach (DataRow dr in dt.Rows)
  502. {
  503. if (iRowIndex > 0)
  504. {
  505. int iCellIndex = 0;
  506. IRow irow = sheet.CreateRow(iRowIndex);
  507. for (int i = 0; i < dt.Columns.Count; i++)
  508. {
  509. string strsj = string.Empty;
  510. if (dr[i] != null)
  511. {
  512. strsj = dr[i].ToString();
  513. }
  514. ICell cell = irow.CreateCell(iCellIndex);
  515. cell.SetCellValue(strsj);
  516. cell.CellStyle = cellStyle;
  517. iCellIndex++;
  518. }
  519. }
  520. iRowIndex++;
  521. }
  522. //自适应列宽度
  523. for (int i = 0; i < icolIndex; i++)
  524. {
  525. sheet.AutoSizeColumn(i);
  526. }
  527. using (MemoryStream ms = new MemoryStream())
  528. {
  529. workbook.Write(ms);
  530. HttpContext curContext = HttpContext.Current;
  531. // 设置编码和附件格式
  532. curContext.Response.ContentType = "application/vnd.ms-excel";
  533. curContext.Response.ContentEncoding = Encoding.UTF8;
  534. curContext.Response.Charset = "";
  535. curContext.Response.AppendHeader("Content-Disposition",
  536. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  537. curContext.Response.BinaryWrite(ms.GetBuffer());
  538. workbook = null;
  539. ms.Close();
  540. ms.Dispose();
  541. curContext.Response.End();
  542. }
  543. //}
  544. return "";
  545. }
  546. catch
  547. {
  548. return "导出失败!";
  549. }
  550. }
  551. #endregion
  552. }
  553. }