Sin descripción

NPOIHelper.cs 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Web;
  9. using NPOI.HSSF.UserModel;
  10. using NPOI.SS.UserModel;
  11. using NPOI.SS.Util;
  12. using NPOI.XSSF.UserModel;
  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. /// 投诉产品日期分布表
  101. /// </summary>
  102. /// <param name="ds"></param>
  103. /// <returns></returns>
  104. public string SatisfiedToExcel(string Name, IOrderedEnumerable<deptSatisfiedReport> deptSatisfied, 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. foreach (string dc in cols)
  130. {
  131. ICell cell = headerRow.CreateCell(icolIndex);
  132. cell.SetCellValue(dc);
  133. cell.CellStyle = HeadercellStyle;
  134. icolIndex++;
  135. }
  136. ICellStyle cellStyle = workbook.CreateCellStyle();
  137. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  138. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  139. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  140. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  141. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  142. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  143. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  144. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  145. cellStyle.SetFont(cellfont);
  146. //建立内容行
  147. int iRowIndex = 0;
  148. string strsj = "";
  149. foreach (var dr in deptSatisfied )
  150. {
  151. IRow irow = sheet.CreateRow(iRowIndex + 1);
  152. for (int i = 0; i <5 ; i++)
  153. {
  154. switch (i )
  155. {
  156. case 0:
  157. strsj = dr.deptname;
  158. ICell cell = irow.CreateCell(i);
  159. cell.SetCellValue(strsj);
  160. cell.CellStyle = cellStyle;
  161. break;
  162. case 1:
  163. strsj = dr.count.ToString ();
  164. ICell cell1 = irow.CreateCell(i);
  165. cell1.SetCellValue(strsj);
  166. cell1.CellStyle = cellStyle;
  167. break;
  168. case 2:
  169. strsj = dr.satisfiedcount.ToString();
  170. ICell cell2 = irow.CreateCell(i);
  171. cell2.SetCellValue(strsj);
  172. cell2.CellStyle = cellStyle;
  173. break;
  174. case 3:
  175. strsj = dr.notsatisfiedcount.ToString();
  176. ICell cell3 = irow.CreateCell(i);
  177. cell3.SetCellValue(strsj);
  178. cell3.CellStyle = cellStyle;
  179. break;
  180. case 4:
  181. strsj = dr.satisfiedrate;
  182. ICell cell4 = irow.CreateCell(i);
  183. cell4.SetCellValue(strsj);
  184. cell4.CellStyle = cellStyle;
  185. break;
  186. }
  187. }
  188. iRowIndex++;
  189. }
  190. //自适应列宽度
  191. for (int i = 0; i < icolIndex; i++)
  192. {
  193. sheet.AutoSizeColumn(i);
  194. }
  195. using (MemoryStream ms = new MemoryStream())
  196. {
  197. workbook.Write(ms);
  198. HttpContext curContext = HttpContext.Current;
  199. // 设置编码和附件格式
  200. curContext.Response.ContentType = "application/vnd.ms-excel";
  201. curContext.Response.ContentEncoding = Encoding.UTF8;
  202. curContext.Response.Charset = "";
  203. curContext.Response.AppendHeader("Content-Disposition",
  204. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  205. curContext.Response.BinaryWrite(ms.GetBuffer());
  206. workbook = null;
  207. ms.Close();
  208. ms.Dispose();
  209. curContext.Response.End();
  210. }
  211. //}
  212. return "";
  213. }
  214. catch
  215. {
  216. return "导出失败!";
  217. }
  218. }
  219. /// <summary>
  220. /// 弹出下载框导出excel
  221. /// </summary>
  222. /// <param name="Name"></param>
  223. /// <param name="dt"></param>
  224. /// <returns></returns>
  225. public string ExportToExcel(string Name, DataTable dt, string[] cols = null, int issort = 0)
  226. {
  227. try
  228. {
  229. //if (dt.Rows.Count > 0)
  230. //{
  231. HSSFWorkbook workbook = new HSSFWorkbook();
  232. ISheet sheet = workbook.CreateSheet(Name);
  233. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  234. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  235. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  236. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  237. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  238. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  239. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  240. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  241. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  242. //字体
  243. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  244. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  245. headerfont.FontHeightInPoints = 12;
  246. HeadercellStyle.SetFont(headerfont);
  247. //用column name 作为列名
  248. int icolIndex = 0;
  249. IRow headerRow = sheet.CreateRow(0);
  250. if (issort == 1)
  251. {
  252. ICell cell = headerRow.CreateCell(icolIndex);
  253. cell.SetCellValue("序号");
  254. cell.CellStyle = HeadercellStyle;
  255. icolIndex++;
  256. }
  257. if (cols == null || (cols != null && cols.Length == 0))
  258. {
  259. foreach (DataColumn dc in dt.Columns)
  260. {
  261. ICell cell = headerRow.CreateCell(icolIndex);
  262. cell.SetCellValue(dc.ColumnName);
  263. cell.CellStyle = HeadercellStyle;
  264. icolIndex++;
  265. }
  266. }
  267. else
  268. {
  269. foreach (string dc in cols)
  270. {
  271. ICell cell = headerRow.CreateCell(icolIndex);
  272. cell.SetCellValue(dc);
  273. cell.CellStyle = HeadercellStyle;
  274. icolIndex++;
  275. }
  276. }
  277. ICellStyle cellStyle = workbook.CreateCellStyle();
  278. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  279. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  280. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  281. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  282. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  283. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  284. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  285. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  286. cellStyle.SetFont(cellfont);
  287. //建立内容行
  288. int iRowIndex = 0;
  289. foreach (DataRow dr in dt.Rows)
  290. {
  291. int iCellIndex = 0;
  292. IRow irow = sheet.CreateRow(iRowIndex + 1);
  293. if (issort == 1)
  294. {
  295. ICell cell = irow.CreateCell(iCellIndex);
  296. cell.SetCellValue(iRowIndex + 1);
  297. cell.CellStyle = cellStyle;
  298. iCellIndex++;
  299. }
  300. for (int i = 0; i < dt.Columns.Count; i++)
  301. {
  302. string strsj = string.Empty;
  303. if (dr[i] != null)
  304. {
  305. strsj = dr[i].ToString();
  306. }
  307. ICell cell = irow.CreateCell(iCellIndex);
  308. cell.SetCellValue(strsj);
  309. cell.CellStyle = cellStyle;
  310. iCellIndex++;
  311. }
  312. iRowIndex++;
  313. }
  314. //自适应列宽度
  315. for (int i = 0; i < icolIndex; i++)
  316. {
  317. sheet.AutoSizeColumn(i);
  318. }
  319. using (MemoryStream ms = new MemoryStream())
  320. {
  321. workbook.Write(ms);
  322. HttpContext curContext = HttpContext.Current;
  323. // 设置编码和附件格式
  324. curContext.Response.ContentType = "application/vnd.ms-excel";
  325. curContext.Response.ContentEncoding = Encoding.UTF8;
  326. curContext.Response.Charset = "";
  327. curContext.Response.AppendHeader("Content-Disposition",
  328. "attachment;filename=" + HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  329. curContext.Response.BinaryWrite(ms.GetBuffer());
  330. workbook = null;
  331. ms.Close();
  332. ms.Dispose();
  333. curContext.Response.End();
  334. }
  335. //}
  336. return "";
  337. }
  338. catch
  339. {
  340. return "导出失败!";
  341. }
  342. }
  343. /// <summary>
  344. /// 导入excel转换为datatable
  345. /// </summary>
  346. /// <param name="upfile"></param>
  347. /// <param name="headrow"></param>
  348. /// <returns></returns>
  349. public DataTable ExcelToTable(HttpPostedFile upfile, int headrow)
  350. {
  351. DataTable dt = new DataTable();
  352. IWorkbook workbook = null;
  353. Stream stream = upfile.InputStream;
  354. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  355. if (suffix == "xlsx") // 2007版本
  356. {
  357. workbook = new XSSFWorkbook(stream);
  358. }
  359. else if (suffix == "xls") // 2003版本
  360. {
  361. workbook = new HSSFWorkbook(stream);
  362. }
  363. //获取excel的第一个sheet
  364. ISheet sheet = workbook.GetSheetAt(0);
  365. //获取sheet的第一行
  366. IRow headerRow = sheet.GetRow(headrow);
  367. //一行最后一个方格的编号 即总的列数
  368. int cellCount = headerRow.LastCellNum;
  369. //最后一列的标号 即总的行数
  370. int rowCount = sheet.LastRowNum;
  371. //列名
  372. for (int i = 0; i < cellCount; i++)
  373. {
  374. dt.Columns.Add(headerRow.GetCell(i).ToString());
  375. }
  376. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  377. {
  378. DataRow dr = dt.NewRow();
  379. IRow row = sheet.GetRow(i);
  380. for (int j = row.FirstCellNum; j < cellCount; j++)
  381. {
  382. if (row.GetCell(j) != null)
  383. {
  384. dr[j] = row.GetCell(j).ToString();
  385. }
  386. }
  387. dt.Rows.Add(dr);
  388. }
  389. sheet = null;
  390. workbook = null;
  391. return dt;
  392. }
  393. /// <summary>
  394. /// 导入excel转换为datatable
  395. /// </summary>
  396. /// <param name="upfile"></param>
  397. /// <param name="headrow"></param>
  398. /// <returns></returns>
  399. public DataTable ExcelToTable(HttpPostedFileBase upfile, int headrow)
  400. {
  401. DataTable dt = new DataTable();
  402. IWorkbook workbook = null;
  403. Stream stream = upfile.InputStream;
  404. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  405. if (suffix == "xlsx") // 2007版本
  406. {
  407. workbook = new XSSFWorkbook(stream);
  408. }
  409. else if (suffix == "xls") // 2003版本
  410. {
  411. workbook = new HSSFWorkbook(stream);
  412. }
  413. //获取excel的第一个sheet
  414. ISheet sheet = workbook.GetSheetAt(0);
  415. //获取sheet的第一行
  416. IRow headerRow = sheet.GetRow(headrow);
  417. //一行最后一个方格的编号 即总的列数
  418. int cellCount = headerRow.LastCellNum;
  419. //最后一列的标号 即总的行数
  420. int rowCount = sheet.LastRowNum;
  421. //列名
  422. for (int i = 0; i < cellCount; i++)
  423. {
  424. dt.Columns.Add(headerRow.GetCell(i).ToString());
  425. }
  426. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  427. {
  428. DataRow dr = dt.NewRow();
  429. IRow row = sheet.GetRow(i);
  430. for (int j = row.FirstCellNum; j < cellCount; j++)
  431. {
  432. if (row.GetCell(j) != null)
  433. {
  434. dr[j] = row.GetCell(j).ToString().Trim(' ').Trim('\t');
  435. }
  436. }
  437. dt.Rows.Add(dr);
  438. }
  439. sheet = null;
  440. workbook = null;
  441. return dt;
  442. }
  443. /// <summary>
  444. /// 弹出下载框导出excel
  445. /// </summary>
  446. /// <param name="Name"></param>
  447. /// <param name="dt"></param>
  448. /// <returns></returns>
  449. public string TSExportToExcel(DataTable dt, int tscount)
  450. {
  451. try
  452. {
  453. //if (dt.Rows.Count > 0)
  454. //{
  455. HSSFWorkbook workbook = new HSSFWorkbook();
  456. ISheet sheet = workbook.CreateSheet("Sheet1");
  457. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  458. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  459. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  460. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  461. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  462. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  463. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  464. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  465. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  466. //字体
  467. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  468. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  469. headerfont.FontHeightInPoints = 12;
  470. HeadercellStyle.SetFont(headerfont);
  471. //用column name 作为列名
  472. int icolIndex = 0;
  473. IRow headerRow = sheet.CreateRow(0);
  474. foreach (DataColumn dc in dt.Columns)
  475. {
  476. ICell cell = headerRow.CreateCell(icolIndex);
  477. cell.SetCellValue(dc.ColumnName);
  478. cell.CellStyle = HeadercellStyle;
  479. icolIndex++;
  480. }
  481. ICellStyle cellStyle = workbook.CreateCellStyle();
  482. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  483. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  484. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  485. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  486. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  487. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  488. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  489. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  490. cellStyle.SetFont(cellfont);
  491. //建立内容行
  492. int iRowIndex = 0;
  493. foreach (DataRow dr in dt.Rows)
  494. {
  495. int iCellIndex = 0;
  496. IRow irow = sheet.CreateRow(iRowIndex + 1);
  497. for (int i = 0; i < dt.Columns.Count; i++)
  498. {
  499. string strsj = string.Empty;
  500. if (dr[i] != null)
  501. {
  502. strsj = dr[i].ToString();
  503. }
  504. ICell cell = irow.CreateCell(iCellIndex);
  505. cell.SetCellValue(strsj);
  506. cell.CellStyle = cellStyle;
  507. iCellIndex++;
  508. }
  509. iRowIndex++;
  510. }
  511. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 1));
  512. sheet.AddMergedRegion(new CellRangeAddress(1, tscount, 0, 0));
  513. for (int i = iRowIndex; i > tscount; i--)
  514. {
  515. sheet.AddMergedRegion(new CellRangeAddress(i, i, 0, 1));
  516. }
  517. //自适应列宽度
  518. for (int i = 0; i < icolIndex; i++)
  519. {
  520. sheet.AutoSizeColumn(i);
  521. }
  522. using (MemoryStream ms = new MemoryStream())
  523. {
  524. workbook.Write(ms);
  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("投诉统计_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  532. curContext.Response.BinaryWrite(ms.GetBuffer());
  533. workbook = null;
  534. ms.Close();
  535. ms.Dispose();
  536. curContext.Response.End();
  537. }
  538. //}
  539. return "";
  540. }
  541. catch
  542. {
  543. return "导出失败!";
  544. }
  545. }
  546. /// <summary>
  547. /// 生成excel到路径
  548. /// </summary>
  549. /// <param name="Name"></param>
  550. /// <param name="dt"></param>
  551. /// <returns></returns>
  552. public string CreateExcelFile(string Name, DataTable dt, string Path,string[] cols = null)
  553. {
  554. try
  555. {
  556. if (dt.Rows.Count > 0)
  557. {
  558. HSSFWorkbook workbook = new HSSFWorkbook();
  559. ISheet sheet = workbook.CreateSheet(Name);
  560. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  561. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  562. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  563. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  564. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  565. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  566. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  567. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  568. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  569. //字体
  570. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  571. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  572. headerfont.FontHeightInPoints = 12;
  573. HeadercellStyle.SetFont(headerfont);
  574. //用column name 作为列名
  575. int icolIndex = 0;
  576. IRow headerRow = sheet.CreateRow(0);
  577. if (cols == null || (cols != null && cols.Length == 0))
  578. {
  579. foreach (DataColumn dc in dt.Columns)
  580. {
  581. ICell cell = headerRow.CreateCell(icolIndex);
  582. cell.SetCellValue(dc.ColumnName);
  583. cell.CellStyle = HeadercellStyle;
  584. icolIndex++;
  585. }
  586. }
  587. else
  588. {
  589. foreach (string dc in cols)
  590. {
  591. ICell cell = headerRow.CreateCell(icolIndex);
  592. cell.SetCellValue(dc);
  593. cell.CellStyle = HeadercellStyle;
  594. icolIndex++;
  595. }
  596. }
  597. ICellStyle cellStyle = workbook.CreateCellStyle();
  598. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  599. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  600. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  601. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  602. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  603. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  604. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  605. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  606. cellStyle.SetFont(cellfont);
  607. //建立内容行
  608. int iRowIndex = 0;
  609. foreach (DataRow dr in dt.Rows)
  610. {
  611. int iCellIndex = 0;
  612. IRow irow = sheet.CreateRow(iRowIndex + 1);
  613. for (int i = 0; i < dt.Columns.Count; i++)
  614. {
  615. string strsj = string.Empty;
  616. if (dr[i] != null)
  617. {
  618. strsj = dr[i].ToString();
  619. }
  620. ICell cell = irow.CreateCell(iCellIndex);
  621. cell.SetCellValue(strsj);
  622. cell.CellStyle = cellStyle;
  623. iCellIndex++;
  624. }
  625. iRowIndex++;
  626. }
  627. //自适应列宽度
  628. for (int i = 0; i < icolIndex; i++)
  629. {
  630. sheet.AutoSizeColumn(i);
  631. }
  632. Name = HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls");
  633. Path = HttpContext.Current.Server.MapPath("..") + Path;
  634. if (!Directory.Exists(Path)) Directory.CreateDirectory(Path);
  635. FileStream fileHSSF = new FileStream(Path+ Name, FileMode.Create);
  636. workbook.Write(fileHSSF);
  637. fileHSSF.Close();
  638. fileHSSF.Dispose();
  639. workbook = null;
  640. }
  641. return "";
  642. }
  643. catch
  644. {
  645. return "生成失败!";
  646. }
  647. }
  648. /// <summary>
  649. /// 弹出下载框导出excel
  650. /// </summary>
  651. /// <param name="Name"></param>
  652. /// <param name="dt"></param>
  653. /// <returns></returns>
  654. public string ExportToExcel64(string name, string base64url)
  655. {
  656. try
  657. {
  658. int delLength = base64url.IndexOf(',') + 1;
  659. string str = base64url.Substring(delLength, base64url.Length - delLength);
  660. byte[] bData = Convert.FromBase64String(str);
  661. HttpContext curContext = HttpContext.Current;
  662. // 设置编码和附件格式
  663. curContext.Response.ContentType = "application/vnd.ms-excel";
  664. curContext.Response.ContentEncoding = Encoding.UTF8;
  665. curContext.Response.Charset = "";
  666. curContext.Response.AppendHeader("Content-Disposition",
  667. "attachment;filename=" + HttpUtility.UrlEncode(name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  668. curContext.Response.BinaryWrite(bData);
  669. curContext.Response.End();
  670. return "";
  671. }
  672. catch
  673. {
  674. return "导出失败!";
  675. }
  676. }
  677. /// <summary>
  678. /// 简报导出
  679. /// </summary>
  680. /// <param name="ds"></param>
  681. /// <returns></returns>
  682. public string SimpleExportToExcel(DataSet ds)
  683. {
  684. try
  685. {
  686. HSSFWorkbook workbook = new HSSFWorkbook();
  687. ISheet sheet = workbook.CreateSheet("Sheet1");
  688. ICellStyle cellStyle = workbook.CreateCellStyle();
  689. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  690. //cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  691. //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  692. //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  693. //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  694. //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  695. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  696. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  697. cellStyle.SetFont(cellfont);
  698. ICellStyle cellStylebt = workbook.CreateCellStyle();
  699. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  700. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  701. cellStylebt.SetFont(cellfontbt);
  702. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  703. cellStylebt.Alignment = HorizontalAlignment.Center;
  704. IRow irow1 = sheet.CreateRow(1);
  705. ICell cell1 = irow1.CreateCell(0);
  706. cell1.SetCellValue("时间:"+ ds.Tables[5].Rows[0]["sdate"].ToString()+ " 至 "+ ds.Tables[5].Rows[0]["edate"].ToString());
  707. cell1.CellStyle = cellStylebt;
  708. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 7));
  709. //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(new CellRangeAddress(1, 1, 0, 7), BorderStyle.Thin, NPOI.HSSF.Util.HSSFColor.Black.Index);
  710. #region 话务受理情况
  711. DataTable dt1 = ds.Tables[0];
  712. IRow irow2 = sheet.CreateRow(2);
  713. ICell cell2 = irow2.CreateCell(0);
  714. cell2.SetCellValue("话务受理情况");
  715. cell2.CellStyle = cellStylebt;
  716. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 7));
  717. IRow irow3 = sheet.CreateRow(3);
  718. ICell cell31 = irow3.CreateCell(0);
  719. cell31.SetCellValue("来电");
  720. cell31.CellStyle = cellStyle;
  721. ICell cell32 = irow3.CreateCell(1);
  722. cell32.SetCellValue(Int32.Parse(dt1.Rows[0]["ldcount"].ToString()));
  723. cell32.CellStyle = cellStyle;
  724. ICell cell33 = irow3.CreateCell(2);
  725. cell33.SetCellValue("接听");
  726. cell33.CellStyle = cellStyle;
  727. ICell cell34 = irow3.CreateCell(3);
  728. cell34.SetCellValue(Int32.Parse(dt1.Rows[0]["jtcount"].ToString()));
  729. cell34.CellStyle = cellStyle;
  730. ICell cell35 = irow3.CreateCell(4);
  731. cell35.SetCellValue("有效接听");
  732. cell35.CellStyle = cellStyle;
  733. ICell cell36 = irow3.CreateCell(5);
  734. cell36.SetCellValue(Int32.Parse(dt1.Rows[0]["yxcount"].ToString()));
  735. cell36.CellStyle = cellStyle;
  736. #endregion
  737. #region 工单受理情况
  738. DataTable dt2 = ds.Tables[1];
  739. IRow irow4 = sheet.CreateRow(4);
  740. ICell cell4 = irow4.CreateCell(0);
  741. cell4.SetCellValue("工单受理情况");
  742. cell4.CellStyle = cellStylebt;
  743. sheet.AddMergedRegion(new CellRangeAddress(4, 4, 0, 7));
  744. IRow irow5 = sheet.CreateRow(5);
  745. ICell cell51 = irow5.CreateCell(0);
  746. cell51.SetCellValue("受理");
  747. cell51.CellStyle = cellStyle;
  748. ICell cell52 = irow5.CreateCell(1);
  749. cell52.SetCellValue(Int32.Parse(dt2.Rows[0]["slcount"].ToString()));
  750. cell52.CellStyle = cellStyle;
  751. ICell cell53 = irow5.CreateCell(2);
  752. cell53.SetCellValue("待提交");
  753. cell53.CellStyle = cellStyle;
  754. ICell cell54 = irow5.CreateCell(3);
  755. cell54.SetCellValue(Int32.Parse(dt2.Rows[0]["dtjcount"].ToString()));
  756. cell54.CellStyle = cellStyle;
  757. ICell cell55 = irow5.CreateCell(4);
  758. cell55.SetCellValue("待交办");
  759. cell55.CellStyle = cellStyle;
  760. ICell cell56 = irow5.CreateCell(5);
  761. cell56.SetCellValue(Int32.Parse(dt2.Rows[0]["djbcount"].ToString()));
  762. cell56.CellStyle = cellStyle;
  763. ICell cell57 = irow5.CreateCell(6);
  764. cell57.SetCellValue("待查收");
  765. cell57.CellStyle = cellStyle;
  766. ICell cell58 = irow5.CreateCell(7);
  767. cell58.SetCellValue(Int32.Parse(dt2.Rows[0]["dcscount"].ToString()));
  768. cell58.CellStyle = cellStyle;
  769. IRow irow6 = sheet.CreateRow(6);
  770. ICell cell61 = irow6.CreateCell(0);
  771. cell61.SetCellValue("待审核退回");
  772. cell61.CellStyle = cellStyle;
  773. ICell cell62 = irow6.CreateCell(1);
  774. cell62.SetCellValue(Int32.Parse(dt2.Rows[0]["dshthcount"].ToString()));
  775. cell62.CellStyle = cellStyle;
  776. ICell cell63 = irow6.CreateCell(2);
  777. cell63.SetCellValue("待办理");
  778. cell63.CellStyle = cellStyle;
  779. ICell cell64 = irow6.CreateCell(3);
  780. cell64.SetCellValue(Int32.Parse(dt2.Rows[0]["dblcount"].ToString()));
  781. cell64.CellStyle = cellStyle;
  782. ICell cell65 = irow6.CreateCell(4);
  783. cell65.SetCellValue("待延时审核");
  784. cell65.CellStyle = cellStyle;
  785. ICell cell66 = irow6.CreateCell(5);
  786. cell66.SetCellValue(Int32.Parse(dt2.Rows[0]["dshyscount"].ToString()));
  787. cell66.CellStyle = cellStyle;
  788. ICell cell67 = irow6.CreateCell(6);
  789. cell67.SetCellValue("待回访");
  790. cell67.CellStyle = cellStyle;
  791. ICell cell68 = irow6.CreateCell(7);
  792. cell68.SetCellValue(Int32.Parse(dt2.Rows[0]["dhfcount"].ToString()));
  793. cell68.CellStyle = cellStyle;
  794. IRow irow7 = sheet.CreateRow(7);
  795. ICell cell71 = irow7.CreateCell(0);
  796. cell71.SetCellValue("待结案");
  797. cell71.CellStyle = cellStyle;
  798. ICell cell72 = irow7.CreateCell(1);
  799. cell72.SetCellValue(Int32.Parse(dt2.Rows[0]["dwjcount"].ToString()));
  800. cell72.CellStyle = cellStyle;
  801. ICell cell73 = irow7.CreateCell(2);
  802. cell73.SetCellValue("待重办");
  803. cell73.CellStyle = cellStyle;
  804. ICell cell74 = irow7.CreateCell(3);
  805. cell74.SetCellValue(Int32.Parse(dt2.Rows[0]["dcbcount"].ToString()));
  806. cell74.CellStyle = cellStyle;
  807. ICell cell75 = irow7.CreateCell(4);
  808. cell75.SetCellValue("已结案");
  809. cell75.CellStyle = cellStyle;
  810. ICell cell76 = irow7.CreateCell(5);
  811. cell76.SetCellValue(Int32.Parse(dt2.Rows[0]["ywjcount"].ToString()));
  812. cell76.CellStyle = cellStyle;
  813. ICell cell77 = irow7.CreateCell(6);
  814. cell77.SetCellValue("在线办理");
  815. cell77.CellStyle = cellStyle;
  816. ICell cell78 = irow7.CreateCell(7);
  817. cell78.SetCellValue(Int32.Parse(dt2.Rows[0]["zxbjcount"].ToString()));
  818. cell78.CellStyle = cellStyle;
  819. IRow irow8 = sheet.CreateRow(8);
  820. ICell cell81 = irow8.CreateCell(0);
  821. cell81.SetCellValue("中心转派");
  822. cell81.CellStyle = cellStyle;
  823. ICell cell82 = irow8.CreateCell(1);
  824. cell82.SetCellValue(Int32.Parse(dt2.Rows[0]["zxzpcount"].ToString()));
  825. cell82.CellStyle = cellStyle;
  826. ICell cell83 = irow8.CreateCell(2);
  827. cell83.SetCellValue("在线办理率");
  828. cell83.CellStyle = cellStyle;
  829. ICell cell84 = irow8.CreateCell(3);
  830. cell84.SetCellValue(dt2.Rows[0]["zxbjrate"].ToString());
  831. cell84.CellStyle = cellStyle;
  832. #endregion
  833. #region 政府热线受理情况
  834. DataTable dt3 = ds.Tables[2];
  835. IRow irow9 = sheet.CreateRow(9);
  836. ICell cell9 = irow9.CreateCell(0);
  837. cell9.SetCellValue("政府热线受理情况");
  838. cell9.CellStyle = cellStylebt;
  839. sheet.AddMergedRegion(new CellRangeAddress(9, 9, 0, 7));
  840. IRow irow10 = sheet.CreateRow(10);
  841. IRow irow11 = sheet.CreateRow(11);
  842. int n = 0;
  843. foreach (DataRow dr3 in dt3.Rows)
  844. {
  845. if (n < 4)
  846. {
  847. ICell cell101 = irow10.CreateCell(2 * (n + 1) - 2);
  848. cell101.SetCellValue(dr3["source"].ToString());
  849. cell101.CellStyle = cellStyle;
  850. ICell cell102 = irow10.CreateCell(2 * (n + 1) - 1);
  851. cell102.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  852. cell102.CellStyle = cellStyle;
  853. }
  854. else
  855. {
  856. ICell cell111 = irow11.CreateCell(2 * (n -3) - 2);
  857. cell111.SetCellValue(dr3["source"].ToString());
  858. cell111.CellStyle = cellStyle;
  859. ICell cell112 = irow11.CreateCell(2 * (n - 3) - 1);
  860. cell112.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  861. cell112.CellStyle = cellStyle;
  862. }
  863. n = n + 1;
  864. }
  865. #endregion
  866. #region 工单受理类型情况
  867. DataTable dt4 = ds.Tables[3];
  868. IRow irow12 = sheet.CreateRow(12);
  869. ICell cell12 = irow12.CreateCell(0);
  870. cell12.SetCellValue("工单受理类型情况");
  871. cell12.CellStyle = cellStylebt;
  872. sheet.AddMergedRegion(new CellRangeAddress(12, 12, 0, 7));
  873. IRow irow13 = sheet.CreateRow(13);
  874. IRow irow14 = sheet.CreateRow(14);
  875. int m = 0;
  876. foreach (DataRow dr4 in dt4.Rows)
  877. {
  878. if (m < 4)
  879. {
  880. ICell cell131 = irow13.CreateCell(2 * (m + 1) - 2);
  881. cell131.SetCellValue(dr4["type"].ToString());
  882. cell131.CellStyle = cellStyle;
  883. ICell cell132 = irow13.CreateCell(2 * (m + 1) - 1);
  884. cell132.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  885. cell132.CellStyle = cellStyle;
  886. }
  887. else
  888. {
  889. ICell cell141 = irow14.CreateCell(2 * (m - 3) - 2);
  890. cell141.SetCellValue(dr4["type"].ToString());
  891. cell141.CellStyle = cellStyle;
  892. ICell cell152 = irow14.CreateCell(2 * (m - 3) - 1);
  893. cell152.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  894. cell152.CellStyle = cellStyle;
  895. }
  896. m = m + 1;
  897. }
  898. #endregion
  899. #region 工单事发区域情况
  900. DataTable dt5 = ds.Tables[4];
  901. IRow irow15 = sheet.CreateRow(15);
  902. ICell cell15 = irow15.CreateCell(0);
  903. cell15.SetCellValue("工单事发区域情况");
  904. cell15.CellStyle = cellStylebt;
  905. sheet.AddMergedRegion(new CellRangeAddress(15, 15, 0, 7));
  906. int rowcount = (dt5.Rows.Count / 4) + 1;
  907. for (int i = 0; i < rowcount; i++)
  908. {
  909. IRow irow = sheet.CreateRow(16 + i);
  910. for (int j = 0; j < 4; j++)
  911. {
  912. int num = i * 4 + j;
  913. if (num != dt5.Rows.Count)
  914. {
  915. var dr = dt5.Rows[num];
  916. ICell cellname = irow.CreateCell(2 * (j + 1) - 2);
  917. cellname.SetCellValue(dr["areaname"].ToString());
  918. cellname.CellStyle = cellStyle;
  919. ICell cellcount = irow.CreateCell(2 * (j + 1) - 1);
  920. cellcount.SetCellValue(Int32.Parse(dr["count"].ToString()));
  921. cellcount.CellStyle = cellStyle;
  922. }
  923. else
  924. {
  925. break;
  926. }
  927. }
  928. }
  929. #endregion
  930. //自适应列宽度
  931. for (int i = 0; i < 8; i++)
  932. {
  933. sheet.AutoSizeColumn(i);
  934. }
  935. using (MemoryStream ms = new MemoryStream())
  936. {
  937. workbook.Write(ms);
  938. HttpContext curContext = HttpContext.Current;
  939. // 设置编码和附件格式
  940. curContext.Response.ContentType = "application/vnd.ms-excel";
  941. curContext.Response.ContentEncoding = Encoding.UTF8;
  942. curContext.Response.Charset = "";
  943. curContext.Response.AppendHeader("Content-Disposition",
  944. "attachment;filename=" + HttpUtility.UrlEncode("业务简报_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  945. curContext.Response.BinaryWrite(ms.GetBuffer());
  946. workbook = null;
  947. ms.Close();
  948. ms.Dispose();
  949. curContext.Response.End();
  950. }
  951. return "";
  952. }
  953. catch
  954. {
  955. return "导出失败!";
  956. }
  957. }
  958. }
  959. }