新野县12345_后端

NPOIHelper.cs 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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. namespace CallCenter.Utility
  12. {
  13. public class NPOIHelper
  14. {
  15. private string _title;
  16. private string _sheetName;
  17. private string _filePath;
  18. /// <summary>
  19. /// 导出到Excel
  20. /// </summary>
  21. /// <param name="table"></param>
  22. /// <returns></returns>
  23. public bool ToExcel(DataTable table, string[] columns = null)
  24. {
  25. FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  26. IWorkbook workBook = new HSSFWorkbook();
  27. if (string.IsNullOrWhiteSpace(this._sheetName))
  28. {
  29. this._sheetName = "sheet1";
  30. }
  31. ISheet sheet = workBook.CreateSheet(this._sheetName);
  32. //处理表格标题
  33. IRow row = sheet.CreateRow(0);
  34. row.CreateCell(0).SetCellValue(this._title);
  35. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1));
  36. row.Height = 500;
  37. ICellStyle cellStyle = workBook.CreateCellStyle();
  38. IFont font = workBook.CreateFont();
  39. font.FontName = "微软雅黑";
  40. font.FontHeightInPoints = 17;
  41. cellStyle.SetFont(font);
  42. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  43. cellStyle.Alignment = HorizontalAlignment.Center;
  44. row.Cells[0].CellStyle = cellStyle;
  45. //处理表格列头
  46. row = sheet.CreateRow(1);
  47. if (columns == null)
  48. {
  49. for (int i = 0; i < table.Columns.Count; i++)
  50. {
  51. row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
  52. row.Height = 350;
  53. sheet.AutoSizeColumn(i);
  54. }
  55. }
  56. else
  57. {
  58. for (int i = 0; i < columns.Length; i++)
  59. {
  60. row.CreateCell(i).SetCellValue(columns[i]);
  61. row.Height = 350;
  62. sheet.AutoSizeColumn(i);
  63. }
  64. }
  65. //处理数据内容
  66. for (int i = 0; i < table.Rows.Count; i++)
  67. {
  68. row = sheet.CreateRow(2 + i);
  69. row.Height = 250;
  70. for (int j = 0; j < table.Columns.Count; j++)
  71. {
  72. row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
  73. sheet.SetColumnWidth(j, 256 * 15);
  74. }
  75. }
  76. //写入数据流
  77. workBook.Write(fs);
  78. fs.Flush();
  79. fs.Close();
  80. return true;
  81. }
  82. /// <summary>
  83. /// 导出到Excel
  84. /// </summary>
  85. /// <param name="table"></param>
  86. /// <param name="title"></param>
  87. /// <param name="sheetName">空字符串或null的话默认sheet1</param>
  88. /// <param name="columns">自定义表格列头,默认null</param>
  89. /// <returns></returns>
  90. public bool ToExcel(DataTable table, string title, string sheetName, string filePath, string[] columns = null)
  91. {
  92. this._title = title;
  93. this._sheetName = sheetName;
  94. this._filePath = filePath;
  95. return ToExcel(table, columns);
  96. }
  97. /// <summary>
  98. /// 弹出下载框导出excel
  99. /// </summary>
  100. /// <param name="Name"></param>
  101. /// <param name="dt"></param>
  102. /// <returns></returns>
  103. public string ExportToExcel(string Name, DataTable dt, string[] cols = null)
  104. {
  105. try
  106. {
  107. //if (dt.Rows.Count > 0)
  108. //{
  109. HSSFWorkbook workbook = new HSSFWorkbook();
  110. ISheet sheet = workbook.CreateSheet(Name);
  111. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  112. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  113. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  114. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  115. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  116. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  117. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  118. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  119. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  120. //字体
  121. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  122. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  123. headerfont.FontHeightInPoints = 12;
  124. HeadercellStyle.SetFont(headerfont);
  125. //用column name 作为列名
  126. int icolIndex = 0;
  127. IRow headerRow = sheet.CreateRow(0);
  128. if (cols == null || (cols != null && cols.Length == 0))
  129. {
  130. foreach (DataColumn dc in dt.Columns)
  131. {
  132. ICell cell = headerRow.CreateCell(icolIndex);
  133. cell.SetCellValue(dc.ColumnName);
  134. cell.CellStyle = HeadercellStyle;
  135. icolIndex++;
  136. }
  137. }
  138. else
  139. {
  140. foreach (string dc in cols)
  141. {
  142. ICell cell = headerRow.CreateCell(icolIndex);
  143. cell.SetCellValue(dc);
  144. cell.CellStyle = HeadercellStyle;
  145. icolIndex++;
  146. }
  147. }
  148. ICellStyle cellStyle = workbook.CreateCellStyle();
  149. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  150. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  151. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  152. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  153. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  154. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  155. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  156. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  157. cellStyle.SetFont(cellfont);
  158. //建立内容行
  159. int iRowIndex = 0;
  160. foreach (DataRow dr in dt.Rows)
  161. {
  162. int iCellIndex = 0;
  163. IRow irow = sheet.CreateRow(iRowIndex + 1);
  164. for (int i = 0; i < dt.Columns.Count; i++)
  165. {
  166. string strsj = string.Empty;
  167. if (dr[i] != null)
  168. {
  169. strsj = dr[i].ToString();
  170. }
  171. ICell cell = irow.CreateCell(iCellIndex);
  172. cell.SetCellValue(strsj);
  173. cell.CellStyle = cellStyle;
  174. iCellIndex++;
  175. }
  176. iRowIndex++;
  177. }
  178. //自适应列宽度
  179. for (int i = 0; i < icolIndex; i++)
  180. {
  181. sheet.AutoSizeColumn(i);
  182. }
  183. using (MemoryStream ms = new MemoryStream())
  184. {
  185. workbook.Write(ms);
  186. HttpContext curContext = HttpContext.Current;
  187. // 设置编码和附件格式
  188. curContext.Response.ContentType = "application/vnd.ms-excel";
  189. curContext.Response.ContentEncoding = Encoding.UTF8;
  190. curContext.Response.Charset = "";
  191. curContext.Response.AppendHeader("Content-Disposition",
  192. "attachment;filename=" + HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  193. curContext.Response.BinaryWrite(ms.GetBuffer());
  194. workbook = null;
  195. ms.Close();
  196. ms.Dispose();
  197. curContext.Response.End();
  198. }
  199. //}
  200. return "";
  201. }
  202. catch
  203. {
  204. return "导出失败!";
  205. }
  206. }
  207. /// <summary>
  208. /// 导入excel转换为datatable
  209. /// </summary>
  210. /// <param name="upfile"></param>
  211. /// <param name="headrow"></param>
  212. /// <returns></returns>
  213. public DataTable ExcelToTable(HttpPostedFile upfile, int headrow)
  214. {
  215. DataTable dt = new DataTable();
  216. IWorkbook workbook = null;
  217. Stream stream = upfile.InputStream;
  218. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  219. if (suffix == "xlsx") // 2007版本
  220. {
  221. workbook = new XSSFWorkbook(stream);
  222. }
  223. else if (suffix == "xls") // 2003版本
  224. {
  225. workbook = new HSSFWorkbook(stream);
  226. }
  227. //获取excel的第一个sheet
  228. ISheet sheet = workbook.GetSheetAt(0);
  229. //获取sheet的第一行
  230. IRow headerRow = sheet.GetRow(headrow);
  231. //一行最后一个方格的编号 即总的列数
  232. int cellCount = headerRow.LastCellNum;
  233. //最后一列的标号 即总的行数
  234. int rowCount = sheet.LastRowNum;
  235. //列名
  236. for (int i = 0; i < cellCount; i++)
  237. {
  238. dt.Columns.Add(headerRow.GetCell(i).ToString());
  239. }
  240. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  241. {
  242. DataRow dr = dt.NewRow();
  243. IRow row = sheet.GetRow(i);
  244. for (int j = row.FirstCellNum; j < cellCount; j++)
  245. {
  246. if (row.GetCell(j) != null)
  247. {
  248. dr[j] = row.GetCell(j).ToString();
  249. }
  250. }
  251. dt.Rows.Add(dr);
  252. }
  253. sheet = null;
  254. workbook = null;
  255. return dt;
  256. }
  257. /// <summary>
  258. /// 导入excel转换为datatable
  259. /// </summary>
  260. /// <param name="upfile"></param>
  261. /// <param name="headrow"></param>
  262. /// <returns></returns>
  263. public DataTable ExcelToTable(HttpPostedFileBase upfile, int headrow)
  264. {
  265. DataTable dt = new DataTable();
  266. IWorkbook workbook = null;
  267. Stream stream = upfile.InputStream;
  268. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  269. if (suffix == "xlsx") // 2007版本
  270. {
  271. workbook = new XSSFWorkbook(stream);
  272. }
  273. else if (suffix == "xls") // 2003版本
  274. {
  275. workbook = new HSSFWorkbook(stream);
  276. }
  277. //获取excel的第一个sheet
  278. ISheet sheet = workbook.GetSheetAt(0);
  279. //获取sheet的第一行
  280. IRow headerRow = sheet.GetRow(headrow);
  281. //一行最后一个方格的编号 即总的列数
  282. int cellCount = headerRow.LastCellNum;
  283. //最后一列的标号 即总的行数
  284. int rowCount = sheet.LastRowNum;
  285. //列名
  286. for (int i = 0; i < cellCount; i++)
  287. {
  288. dt.Columns.Add(headerRow.GetCell(i).ToString());
  289. }
  290. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  291. {
  292. DataRow dr = dt.NewRow();
  293. IRow row = sheet.GetRow(i);
  294. for (int j = row.FirstCellNum; j < cellCount; j++)
  295. {
  296. if (row.GetCell(j) != null)
  297. {
  298. dr[j] = row.GetCell(j).ToString().Trim(' ').Trim('\t');
  299. }
  300. }
  301. dt.Rows.Add(dr);
  302. }
  303. sheet = null;
  304. workbook = null;
  305. return dt;
  306. }
  307. /// <summary>
  308. /// 弹出下载框导出excel
  309. /// </summary>
  310. /// <param name="Name"></param>
  311. /// <param name="dt"></param>
  312. /// <returns></returns>
  313. public string TSExportToExcel(DataTable dt, int tscount)
  314. {
  315. try
  316. {
  317. //if (dt.Rows.Count > 0)
  318. //{
  319. HSSFWorkbook workbook = new HSSFWorkbook();
  320. ISheet sheet = workbook.CreateSheet("Sheet1");
  321. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  322. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  323. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  324. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  325. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  326. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  327. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  328. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  329. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  330. //字体
  331. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  332. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  333. headerfont.FontHeightInPoints = 12;
  334. HeadercellStyle.SetFont(headerfont);
  335. //用column name 作为列名
  336. int icolIndex = 0;
  337. IRow headerRow = sheet.CreateRow(0);
  338. foreach (DataColumn dc in dt.Columns)
  339. {
  340. ICell cell = headerRow.CreateCell(icolIndex);
  341. cell.SetCellValue(dc.ColumnName);
  342. cell.CellStyle = HeadercellStyle;
  343. icolIndex++;
  344. }
  345. ICellStyle cellStyle = workbook.CreateCellStyle();
  346. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  347. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  348. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  349. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  350. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  351. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  352. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  353. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  354. cellStyle.SetFont(cellfont);
  355. //建立内容行
  356. int iRowIndex = 0;
  357. foreach (DataRow dr in dt.Rows)
  358. {
  359. int iCellIndex = 0;
  360. IRow irow = sheet.CreateRow(iRowIndex + 1);
  361. for (int i = 0; i < dt.Columns.Count; i++)
  362. {
  363. string strsj = string.Empty;
  364. if (dr[i] != null)
  365. {
  366. strsj = dr[i].ToString();
  367. }
  368. ICell cell = irow.CreateCell(iCellIndex);
  369. cell.SetCellValue(strsj);
  370. cell.CellStyle = cellStyle;
  371. iCellIndex++;
  372. }
  373. iRowIndex++;
  374. }
  375. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 1));
  376. sheet.AddMergedRegion(new CellRangeAddress(1, tscount, 0, 0));
  377. for (int i = iRowIndex; i > tscount; i--)
  378. {
  379. sheet.AddMergedRegion(new CellRangeAddress(i, i, 0, 1));
  380. }
  381. //自适应列宽度
  382. for (int i = 0; i < icolIndex; i++)
  383. {
  384. sheet.AutoSizeColumn(i);
  385. }
  386. using (MemoryStream ms = new MemoryStream())
  387. {
  388. workbook.Write(ms);
  389. HttpContext curContext = HttpContext.Current;
  390. // 设置编码和附件格式
  391. curContext.Response.ContentType = "application/vnd.ms-excel";
  392. curContext.Response.ContentEncoding = Encoding.UTF8;
  393. curContext.Response.Charset = "";
  394. curContext.Response.AppendHeader("Content-Disposition",
  395. "attachment;filename=" + HttpUtility.UrlEncode("投诉统计_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  396. curContext.Response.BinaryWrite(ms.GetBuffer());
  397. workbook = null;
  398. ms.Close();
  399. ms.Dispose();
  400. curContext.Response.End();
  401. }
  402. //}
  403. return "";
  404. }
  405. catch
  406. {
  407. return "导出失败!";
  408. }
  409. }
  410. /// <summary>
  411. /// 生成excel到路径
  412. /// </summary>
  413. /// <param name="Name"></param>
  414. /// <param name="dt"></param>
  415. /// <returns></returns>
  416. public string CreateExcelFile(string Name, DataTable dt, string Path,string[] cols = null)
  417. {
  418. try
  419. {
  420. if (dt.Rows.Count > 0)
  421. {
  422. HSSFWorkbook workbook = new HSSFWorkbook();
  423. ISheet sheet = workbook.CreateSheet(Name);
  424. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  425. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  426. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  427. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  428. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  429. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  430. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  431. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  432. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  433. //字体
  434. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  435. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  436. headerfont.FontHeightInPoints = 12;
  437. HeadercellStyle.SetFont(headerfont);
  438. //用column name 作为列名
  439. int icolIndex = 0;
  440. IRow headerRow = sheet.CreateRow(0);
  441. if (cols == null || (cols != null && cols.Length == 0))
  442. {
  443. foreach (DataColumn dc in dt.Columns)
  444. {
  445. ICell cell = headerRow.CreateCell(icolIndex);
  446. cell.SetCellValue(dc.ColumnName);
  447. cell.CellStyle = HeadercellStyle;
  448. icolIndex++;
  449. }
  450. }
  451. else
  452. {
  453. foreach (string dc in cols)
  454. {
  455. ICell cell = headerRow.CreateCell(icolIndex);
  456. cell.SetCellValue(dc);
  457. cell.CellStyle = HeadercellStyle;
  458. icolIndex++;
  459. }
  460. }
  461. ICellStyle cellStyle = workbook.CreateCellStyle();
  462. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  463. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  464. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  465. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  466. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  467. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  468. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  469. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  470. cellStyle.SetFont(cellfont);
  471. //建立内容行
  472. int iRowIndex = 0;
  473. foreach (DataRow dr in dt.Rows)
  474. {
  475. int iCellIndex = 0;
  476. IRow irow = sheet.CreateRow(iRowIndex + 1);
  477. for (int i = 0; i < dt.Columns.Count; i++)
  478. {
  479. string strsj = string.Empty;
  480. if (dr[i] != null)
  481. {
  482. strsj = dr[i].ToString();
  483. }
  484. ICell cell = irow.CreateCell(iCellIndex);
  485. cell.SetCellValue(strsj);
  486. cell.CellStyle = cellStyle;
  487. iCellIndex++;
  488. }
  489. iRowIndex++;
  490. }
  491. //自适应列宽度
  492. for (int i = 0; i < icolIndex; i++)
  493. {
  494. sheet.AutoSizeColumn(i);
  495. }
  496. Name = HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls");
  497. Path = HttpContext.Current.Server.MapPath("..") + Path;
  498. if (!Directory.Exists(Path)) Directory.CreateDirectory(Path);
  499. FileStream fileHSSF = new FileStream(Path+ Name, FileMode.Create);
  500. workbook.Write(fileHSSF);
  501. fileHSSF.Close();
  502. fileHSSF.Dispose();
  503. workbook = null;
  504. }
  505. return "";
  506. }
  507. catch
  508. {
  509. return "生成失败!";
  510. }
  511. }
  512. /// <summary>
  513. /// 弹出下载框导出excel
  514. /// </summary>
  515. /// <param name="Name"></param>
  516. /// <param name="dt"></param>
  517. /// <returns></returns>
  518. public string ExportToExcel64(string name, string base64url)
  519. {
  520. try
  521. {
  522. int delLength = base64url.IndexOf(',') + 1;
  523. string str = base64url.Substring(delLength, base64url.Length - delLength);
  524. byte[] bData = Convert.FromBase64String(str);
  525. HttpContext curContext = HttpContext.Current;
  526. // 设置编码和附件格式
  527. curContext.Response.ContentType = "application/vnd.ms-excel";
  528. curContext.Response.ContentEncoding = Encoding.UTF8;
  529. curContext.Response.Charset = "";
  530. curContext.Response.AppendHeader("Content-Disposition",
  531. "attachment;filename=" + HttpUtility.UrlEncode(name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  532. curContext.Response.BinaryWrite(bData);
  533. curContext.Response.End();
  534. return "";
  535. }
  536. catch
  537. {
  538. return "导出失败!";
  539. }
  540. }
  541. /// <summary>
  542. /// 简报导出
  543. /// </summary>
  544. /// <param name="ds"></param>
  545. /// <returns></returns>
  546. public string SimpleExportToExcel(DataSet ds)
  547. {
  548. try
  549. {
  550. HSSFWorkbook workbook = new HSSFWorkbook();
  551. ISheet sheet = workbook.CreateSheet("Sheet1");
  552. ICellStyle cellStyle = workbook.CreateCellStyle();
  553. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  554. //cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  555. //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  556. //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  557. //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  558. //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  559. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  560. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  561. cellStyle.SetFont(cellfont);
  562. IRow irow1 = sheet.CreateRow(1);
  563. ICell cell1 = irow1.CreateCell(0);
  564. cell1.SetCellValue("时间:"+ ds.Tables[5].Rows[0]["sdate"].ToString()+ " 至 "+ ds.Tables[5].Rows[0]["edate"].ToString());
  565. cell1.CellStyle = cellStyle;
  566. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 7));
  567. #region 话务受理情况
  568. DataTable dt1 = ds.Tables[0];
  569. IRow irow2 = sheet.CreateRow(2);
  570. ICell cell2 = irow2.CreateCell(0);
  571. cell2.SetCellValue("话务受理情况");
  572. cell2.CellStyle = cellStyle;
  573. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 7));
  574. IRow irow3 = sheet.CreateRow(3);
  575. ICell cell31 = irow3.CreateCell(0);
  576. cell31.SetCellValue("来电");
  577. cell31.CellStyle = cellStyle;
  578. ICell cell32 = irow3.CreateCell(1);
  579. cell32.SetCellValue(Int32.Parse(dt1.Rows[0]["ldcount"].ToString()));
  580. cell32.CellStyle = cellStyle;
  581. ICell cell33 = irow3.CreateCell(2);
  582. cell33.SetCellValue("接听");
  583. cell33.CellStyle = cellStyle;
  584. ICell cell34 = irow3.CreateCell(3);
  585. cell34.SetCellValue(Int32.Parse(dt1.Rows[0]["jtcount"].ToString()));
  586. cell34.CellStyle = cellStyle;
  587. ICell cell35 = irow3.CreateCell(4);
  588. cell35.SetCellValue("有效接听");
  589. cell35.CellStyle = cellStyle;
  590. ICell cell36 = irow3.CreateCell(5);
  591. cell36.SetCellValue(Int32.Parse(dt1.Rows[0]["yxcount"].ToString()));
  592. cell36.CellStyle = cellStyle;
  593. #endregion
  594. #region 工单受理情况
  595. DataTable dt2 = ds.Tables[1];
  596. IRow irow4 = sheet.CreateRow(4);
  597. ICell cell4 = irow4.CreateCell(0);
  598. cell4.SetCellValue("工单受理情况");
  599. cell4.CellStyle = cellStyle;
  600. sheet.AddMergedRegion(new CellRangeAddress(4, 4, 0, 7));
  601. IRow irow5 = sheet.CreateRow(5);
  602. ICell cell51 = irow5.CreateCell(0);
  603. cell51.SetCellValue("受理");
  604. cell51.CellStyle = cellStyle;
  605. ICell cell52 = irow5.CreateCell(1);
  606. cell52.SetCellValue(Int32.Parse(dt2.Rows[0]["slcount"].ToString()));
  607. cell52.CellStyle = cellStyle;
  608. ICell cell53 = irow5.CreateCell(2);
  609. cell53.SetCellValue("待提交");
  610. cell53.CellStyle = cellStyle;
  611. ICell cell54 = irow5.CreateCell(3);
  612. cell54.SetCellValue(Int32.Parse(dt2.Rows[0]["dtjcount"].ToString()));
  613. cell54.CellStyle = cellStyle;
  614. ICell cell55 = irow5.CreateCell(4);
  615. cell55.SetCellValue("待交办");
  616. cell55.CellStyle = cellStyle;
  617. ICell cell56 = irow5.CreateCell(5);
  618. cell56.SetCellValue(Int32.Parse(dt2.Rows[0]["djbcount"].ToString()));
  619. cell56.CellStyle = cellStyle;
  620. ICell cell57 = irow5.CreateCell(6);
  621. cell57.SetCellValue("待查收");
  622. cell57.CellStyle = cellStyle;
  623. ICell cell58 = irow5.CreateCell(7);
  624. cell58.SetCellValue(Int32.Parse(dt2.Rows[0]["dcscount"].ToString()));
  625. cell58.CellStyle = cellStyle;
  626. IRow irow6 = sheet.CreateRow(6);
  627. ICell cell61 = irow6.CreateCell(0);
  628. cell61.SetCellValue("待审核退回");
  629. cell61.CellStyle = cellStyle;
  630. ICell cell62 = irow6.CreateCell(1);
  631. cell62.SetCellValue(Int32.Parse(dt2.Rows[0]["dshthcount"].ToString()));
  632. cell62.CellStyle = cellStyle;
  633. ICell cell63 = irow6.CreateCell(2);
  634. cell63.SetCellValue("待办理");
  635. cell63.CellStyle = cellStyle;
  636. ICell cell64 = irow6.CreateCell(3);
  637. cell64.SetCellValue(Int32.Parse(dt2.Rows[0]["dblcount"].ToString()));
  638. cell64.CellStyle = cellStyle;
  639. ICell cell65 = irow6.CreateCell(4);
  640. cell65.SetCellValue("待延时审核");
  641. cell65.CellStyle = cellStyle;
  642. ICell cell66 = irow6.CreateCell(5);
  643. cell66.SetCellValue(Int32.Parse(dt2.Rows[0]["dshyscount"].ToString()));
  644. cell66.CellStyle = cellStyle;
  645. ICell cell67 = irow6.CreateCell(6);
  646. cell67.SetCellValue("待回访");
  647. cell67.CellStyle = cellStyle;
  648. ICell cell68 = irow6.CreateCell(7);
  649. cell68.SetCellValue(Int32.Parse(dt2.Rows[0]["dhfcount"].ToString()));
  650. cell68.CellStyle = cellStyle;
  651. IRow irow7 = sheet.CreateRow(7);
  652. ICell cell71 = irow7.CreateCell(0);
  653. cell71.SetCellValue("待结案");
  654. cell71.CellStyle = cellStyle;
  655. ICell cell72 = irow7.CreateCell(1);
  656. cell72.SetCellValue(Int32.Parse(dt2.Rows[0]["dwjcount"].ToString()));
  657. cell72.CellStyle = cellStyle;
  658. ICell cell73 = irow7.CreateCell(2);
  659. cell73.SetCellValue("待重办");
  660. cell73.CellStyle = cellStyle;
  661. ICell cell74 = irow7.CreateCell(3);
  662. cell74.SetCellValue(Int32.Parse(dt2.Rows[0]["dcbcount"].ToString()));
  663. cell74.CellStyle = cellStyle;
  664. ICell cell75 = irow7.CreateCell(4);
  665. cell75.SetCellValue("已结案");
  666. cell75.CellStyle = cellStyle;
  667. ICell cell76 = irow7.CreateCell(5);
  668. cell76.SetCellValue(Int32.Parse(dt2.Rows[0]["ywjcount"].ToString()));
  669. cell76.CellStyle = cellStyle;
  670. ICell cell77 = irow7.CreateCell(6);
  671. cell77.SetCellValue("在线办理");
  672. cell77.CellStyle = cellStyle;
  673. ICell cell78 = irow7.CreateCell(7);
  674. cell78.SetCellValue(Int32.Parse(dt2.Rows[0]["zxbjcount"].ToString()));
  675. cell78.CellStyle = cellStyle;
  676. IRow irow8 = sheet.CreateRow(8);
  677. ICell cell81 = irow8.CreateCell(0);
  678. cell81.SetCellValue("中心转派");
  679. cell81.CellStyle = cellStyle;
  680. ICell cell82 = irow8.CreateCell(1);
  681. cell82.SetCellValue(Int32.Parse(dt2.Rows[0]["zxzpcount"].ToString()));
  682. cell82.CellStyle = cellStyle;
  683. #endregion
  684. #region 政府热线受理情况
  685. DataTable dt3 = ds.Tables[2];
  686. IRow irow9 = sheet.CreateRow(9);
  687. ICell cell9 = irow9.CreateCell(0);
  688. cell9.SetCellValue("政府热线受理情况");
  689. cell9.CellStyle = cellStyle;
  690. sheet.AddMergedRegion(new CellRangeAddress(9, 9, 0, 7));
  691. IRow irow10 = sheet.CreateRow(10);
  692. IRow irow11 = sheet.CreateRow(11);
  693. int n = 0;
  694. foreach (DataRow dr3 in dt3.Rows)
  695. {
  696. if (n < 4)
  697. {
  698. ICell cell101 = irow10.CreateCell(2 * (n + 1) - 2);
  699. cell101.SetCellValue(dr3["source"].ToString());
  700. cell101.CellStyle = cellStyle;
  701. ICell cell102 = irow10.CreateCell(2 * (n + 1) - 1);
  702. cell102.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  703. cell102.CellStyle = cellStyle;
  704. }
  705. else
  706. {
  707. ICell cell111 = irow11.CreateCell(2 * (n -3) - 2);
  708. cell111.SetCellValue(dr3["source"].ToString());
  709. cell111.CellStyle = cellStyle;
  710. ICell cell112 = irow11.CreateCell(2 * (n - 3) - 1);
  711. cell112.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  712. cell112.CellStyle = cellStyle;
  713. }
  714. n = n + 1;
  715. }
  716. #endregion
  717. #region 工单受理类型情况
  718. DataTable dt4 = ds.Tables[3];
  719. IRow irow12 = sheet.CreateRow(12);
  720. ICell cell12 = irow12.CreateCell(0);
  721. cell12.SetCellValue("工单受理类型情况");
  722. cell12.CellStyle = cellStyle;
  723. sheet.AddMergedRegion(new CellRangeAddress(12, 12, 0, 7));
  724. IRow irow13 = sheet.CreateRow(13);
  725. IRow irow14 = sheet.CreateRow(14);
  726. int m = 0;
  727. foreach (DataRow dr4 in dt4.Rows)
  728. {
  729. if (m < 4)
  730. {
  731. ICell cell131 = irow13.CreateCell(2 * (m + 1) - 2);
  732. cell131.SetCellValue(dr4["type"].ToString());
  733. cell131.CellStyle = cellStyle;
  734. ICell cell132 = irow13.CreateCell(2 * (m + 1) - 1);
  735. cell132.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  736. cell132.CellStyle = cellStyle;
  737. }
  738. else
  739. {
  740. ICell cell141 = irow14.CreateCell(2 * (m - 3) - 2);
  741. cell141.SetCellValue(dr4["type"].ToString());
  742. cell141.CellStyle = cellStyle;
  743. ICell cell152 = irow14.CreateCell(2 * (m - 3) - 1);
  744. cell152.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  745. cell152.CellStyle = cellStyle;
  746. }
  747. m = m + 1;
  748. }
  749. #endregion
  750. #region 工单事发区域情况
  751. DataTable dt5 = ds.Tables[4];
  752. IRow irow15 = sheet.CreateRow(15);
  753. ICell cell15 = irow15.CreateCell(0);
  754. cell15.SetCellValue("工单事发区域情况");
  755. cell15.CellStyle = cellStyle;
  756. sheet.AddMergedRegion(new CellRangeAddress(15, 15, 0, 7));
  757. int rowcount = (dt5.Rows.Count / 4) + 1;
  758. for (int i = 0; i < rowcount; i++)
  759. {
  760. IRow irow = sheet.CreateRow(16 + i);
  761. for (int j = 0; j < 4; j++)
  762. {
  763. int num = i * 4 + j;
  764. if (num != dt5.Rows.Count)
  765. {
  766. var dr = dt5.Rows[num];
  767. ICell cellname = irow.CreateCell(2 * (j + 1) - 2);
  768. cellname.SetCellValue(dr["areaname"].ToString());
  769. cellname.CellStyle = cellStyle;
  770. ICell cellcount = irow.CreateCell(2 * (j + 1) - 1);
  771. cellcount.SetCellValue(Int32.Parse(dr["count"].ToString()));
  772. cellcount.CellStyle = cellStyle;
  773. }
  774. else
  775. {
  776. break;
  777. }
  778. }
  779. }
  780. #endregion
  781. //自适应列宽度
  782. for (int i = 0; i < 8; i++)
  783. {
  784. sheet.AutoSizeColumn(i);
  785. }
  786. using (MemoryStream ms = new MemoryStream())
  787. {
  788. workbook.Write(ms);
  789. HttpContext curContext = HttpContext.Current;
  790. // 设置编码和附件格式
  791. curContext.Response.ContentType = "application/vnd.ms-excel";
  792. curContext.Response.ContentEncoding = Encoding.UTF8;
  793. curContext.Response.Charset = "";
  794. curContext.Response.AppendHeader("Content-Disposition",
  795. "attachment;filename=" + HttpUtility.UrlEncode("业务简报_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  796. curContext.Response.BinaryWrite(ms.GetBuffer());
  797. workbook = null;
  798. ms.Close();
  799. ms.Dispose();
  800. curContext.Response.End();
  801. }
  802. return "";
  803. }
  804. catch
  805. {
  806. return "导出失败!";
  807. }
  808. }
  809. }
  810. }