县级监管平台

NPOIHelper.cs 43KB

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