No Description

NPOIHelper.cs 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. if (row != null)
  366. {
  367. for (int j = row.FirstCellNum; j < cellCount; j++)
  368. {
  369. if (row.GetCell(j) != null)
  370. {
  371. dr[j] = row.GetCell(j).ToString();
  372. }
  373. }
  374. dt.Rows.Add(dr);
  375. }
  376. }
  377. sheet = null;
  378. workbook = null;
  379. return dt;
  380. }
  381. /// <summary>
  382. /// 导入excel转换为datatable
  383. /// </summary>
  384. /// <param name="upfile"></param>
  385. /// <param name="headrow"></param>
  386. /// <returns></returns>
  387. public DataTable ExcelToTable(string fileName, int headrow)
  388. {
  389. DataTable dt = new DataTable();
  390. IWorkbook workbook = null;
  391. Stream stream = new FileStream(fileName, FileMode.OpenOrCreate);
  392. string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
  393. if (suffix == "xlsx") // 2007版本
  394. {
  395. workbook = new XSSFWorkbook(stream);
  396. }
  397. else if (suffix == "xls") // 2003版本
  398. {
  399. workbook = new HSSFWorkbook(stream);
  400. }
  401. //获取excel的第一个sheet
  402. ISheet sheet = workbook.GetSheetAt(0);
  403. //获取sheet的第一行
  404. IRow headerRow = sheet.GetRow(headrow);
  405. //一行最后一个方格的编号 即总的列数
  406. int cellCount = headerRow.LastCellNum;
  407. //最后一列的标号 即总的行数
  408. int rowCount = sheet.LastRowNum;
  409. //列名
  410. for (int i = 0; i < cellCount; i++)
  411. {
  412. dt.Columns.Add(headerRow.GetCell(i).ToString());
  413. }
  414. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  415. {
  416. DataRow dr = dt.NewRow();
  417. IRow row = sheet.GetRow(i);
  418. for (int j = row.FirstCellNum; j < cellCount; j++)
  419. {
  420. if (row.GetCell(j) != null)
  421. {
  422. dr[j] = row.GetCell(j).ToString();
  423. }
  424. }
  425. dt.Rows.Add(dr);
  426. }
  427. sheet = null;
  428. workbook = null;
  429. return dt;
  430. }
  431. #region 弹出下载框导出excel(数据第一行为标题)
  432. /// <summary>
  433. /// 弹出下载框导出excel(数据第一行为标题)
  434. /// </summary>
  435. /// <param name="Name"></param>
  436. /// <param name="dt"></param>
  437. /// <returns></returns>
  438. public string ExportToExcel2(string Name, DataTable dt, string[] cols = null)
  439. {
  440. try
  441. {
  442. //if (dt.Rows.Count > 0)
  443. //{
  444. HSSFWorkbook workbook = new HSSFWorkbook();
  445. ISheet sheet = workbook.CreateSheet("Sheet1");
  446. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  447. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  448. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  449. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  450. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  451. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  452. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  453. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  454. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  455. //字体
  456. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  457. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  458. headerfont.FontHeightInPoints = 12;
  459. HeadercellStyle.SetFont(headerfont);
  460. //用column name 作为列名
  461. int icolIndex = 0;
  462. IRow headerRow = sheet.CreateRow(0);
  463. if (cols == null || (cols != null && cols.Length == 0))
  464. {
  465. //foreach (DataColumn dc in dt.Columns)
  466. //{
  467. // ICell cell = headerRow.CreateCell(icolIndex);
  468. // cell.SetCellValue(dc.ColumnName);
  469. // cell.CellStyle = HeadercellStyle;
  470. // icolIndex++;
  471. //}
  472. for (int i = 0; i < dt.Columns.Count; i++)
  473. {
  474. ICell cell = headerRow.CreateCell(icolIndex);
  475. cell.SetCellValue(dt.Rows[0][i].ToString());
  476. cell.CellStyle = HeadercellStyle;
  477. icolIndex++;
  478. }
  479. }
  480. else
  481. {
  482. foreach (string dc in cols)
  483. {
  484. ICell cell = headerRow.CreateCell(icolIndex);
  485. cell.SetCellValue(dc);
  486. cell.CellStyle = HeadercellStyle;
  487. icolIndex++;
  488. }
  489. }
  490. ICellStyle cellStyle = workbook.CreateCellStyle();
  491. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  492. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  493. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  494. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  495. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  496. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  497. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  498. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  499. cellStyle.SetFont(cellfont);
  500. //建立内容行
  501. int iRowIndex = 0;
  502. foreach (DataRow dr in dt.Rows)
  503. {
  504. if (iRowIndex > 0)
  505. {
  506. int iCellIndex = 0;
  507. IRow irow = sheet.CreateRow(iRowIndex);
  508. for (int i = 0; i < dt.Columns.Count; i++)
  509. {
  510. string strsj = string.Empty;
  511. if (dr[i] != null)
  512. {
  513. strsj = dr[i].ToString();
  514. }
  515. ICell cell = irow.CreateCell(iCellIndex);
  516. cell.SetCellValue(strsj);
  517. cell.CellStyle = cellStyle;
  518. iCellIndex++;
  519. }
  520. }
  521. iRowIndex++;
  522. }
  523. //自适应列宽度
  524. for (int i = 0; i < icolIndex; i++)
  525. {
  526. sheet.AutoSizeColumn(i);
  527. }
  528. using (MemoryStream ms = new MemoryStream())
  529. {
  530. workbook.Write(ms);
  531. HttpContext curContext = HttpContext.Current;
  532. // 设置编码和附件格式
  533. curContext.Response.ContentType = "application/vnd.ms-excel";
  534. curContext.Response.ContentEncoding = Encoding.UTF8;
  535. curContext.Response.Charset = "";
  536. curContext.Response.AppendHeader("Content-Disposition",
  537. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  538. curContext.Response.BinaryWrite(ms.GetBuffer());
  539. workbook = null;
  540. ms.Close();
  541. ms.Dispose();
  542. curContext.Response.End();
  543. }
  544. //}
  545. return "";
  546. }
  547. catch
  548. {
  549. return "导出失败!";
  550. }
  551. }
  552. #endregion
  553. }
  554. }