济源12345后端

NPOIHelper.cs 39KB

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