郑州颐和随访系统

NPOIHelper.cs 24KB

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