Нет описания

NPOIHelper.cs 61KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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. private string GetCellValue(DataRow dr,int index,int i )
  444. {
  445. string str = "";
  446. switch (i)
  447. {
  448. case 0:
  449. str = index.ToString();
  450. break;
  451. case 1:
  452. str = dr["deptname"].ToString();
  453. break;
  454. case 2:
  455. str = dr["Undertakers"].ToString();
  456. break;
  457. case 3:
  458. str = dr["Undertakersrate"].ToString();
  459. break;
  460. case 4:
  461. str = dr["Undertakersscore"].ToString();
  462. break;
  463. case 5:
  464. str = dr["overdue"].ToString();
  465. break;
  466. case 6:
  467. str = dr["overduerate"].ToString();
  468. break;
  469. case 7:
  470. str = dr["overduescore"].ToString();
  471. break;
  472. case 8:
  473. str = dr["unsuccessful"].ToString();
  474. break;
  475. case 9:
  476. str = dr["unsuccessfulrate"].ToString();
  477. break;
  478. case 10:
  479. str = dr["unsuccessfulscore"].ToString();
  480. break;
  481. case 11:
  482. str = dr["Chargeback"].ToString();
  483. break;
  484. case 12:
  485. str = dr["Chargebackrate"].ToString();
  486. break;
  487. case 13:
  488. str = dr["Chargebackscore"].ToString();
  489. break;
  490. case 14:
  491. str = dr["Comment"].ToString();
  492. break;
  493. case 15:
  494. str = dr["dissatisfied"].ToString();
  495. break;
  496. case 16:
  497. str = dr["satisfiedrate"].ToString();
  498. break;
  499. case 17:
  500. str = dr["satisfiedscore"].ToString();
  501. break;
  502. case 18:
  503. str = dr["total"].ToString();
  504. break;
  505. case 19:
  506. str = index.ToString();
  507. break;
  508. }
  509. return str;
  510. }
  511. /// <summary>
  512. /// 督办数据报表导出
  513. /// </summary>
  514. /// <param name="ds"></param>
  515. /// <returns></returns>
  516. public string DBExportToExcel(DataTable dt,string Name,string month,string starttime ,string endtime )
  517. {
  518. try
  519. {
  520. HSSFWorkbook workbook = new HSSFWorkbook();
  521. ISheet sheet = workbook.CreateSheet(Name);
  522. ICellStyle cellStyle = workbook.CreateCellStyle();
  523. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  524. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  525. cellStyle.SetFont(cellfont);
  526. ICellStyle cellStylebt = workbook.CreateCellStyle();
  527. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  528. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  529. cellStylebt.SetFont(cellfontbt);
  530. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  531. cellStylebt.Alignment = HorizontalAlignment.Center;
  532. IRow irow1 = sheet.CreateRow(0);
  533. ICell cell1 = irow1.CreateCell(0);
  534. cell1.SetCellValue("12345联动服务工作"+ month + "月份办理情况通报表");
  535. cell1.CellStyle = cellStylebt;
  536. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 19));
  537. IRow irow2 = sheet.CreateRow(2);
  538. ICell cell2 = irow2.CreateCell(0);
  539. cell2.SetCellValue("统计周期:"+ starttime+"至"+ endtime+" 统计时间:"+DateTime .Now.ToString ("yyyy年MM月dd日"));
  540. cell2.CellStyle = cellStylebt;
  541. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 19));
  542. IRow irow3 = sheet.CreateRow(3);
  543. ICell cell3 = irow3.CreateCell(0);
  544. cell3.SetCellValue("一、县(市、区)联动单位");
  545. cell3.CellStyle = cellStylebt;
  546. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 0, 19));
  547. string[] cols = {"序号","联动单位","承办件",
  548. "承办率","得分(5分)","超期件","按时反馈率","得分(20分)",
  549. "未果件","办结率","得分(10分)","退单件","有效回复率",
  550. "得分(15分)","群众评议总数","不满意件","满意率","得分(50分)","总分","排名"};
  551. IRow irow4 = sheet.CreateRow(4);
  552. int icolIndex = 0;
  553. foreach (string dc in cols)
  554. {
  555. ICell cell = irow4.CreateCell(icolIndex);
  556. cell.SetCellValue(dc);
  557. cell.CellStyle = cellStylebt;
  558. icolIndex++;
  559. }
  560. int iRowIndex = 5;
  561. DataRow[] rows = dt.Select("category=1");
  562. int index = 0;
  563. foreach (DataRow dr in rows)
  564. {
  565. index++;
  566. int iCellIndex = 0;
  567. IRow irow = sheet.CreateRow(iRowIndex );
  568. for (int i = 0; i < 20; i++)
  569. {
  570. ICell cell = irow.CreateCell(iCellIndex);
  571. cell.SetCellValue(GetCellValue(dr, index,i ));
  572. cell.CellStyle = cellStyle;
  573. iCellIndex++;
  574. }
  575. iRowIndex++;
  576. }
  577. IRow irow5 = sheet.CreateRow(iRowIndex);
  578. ICell cell5 = irow5.CreateCell(0);
  579. cell5.SetCellValue("二、市直机关联动单位一组");
  580. cell5.CellStyle = cellStylebt;
  581. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  582. iRowIndex++;
  583. IRow irow6 = sheet.CreateRow(iRowIndex);
  584. icolIndex = 0;
  585. foreach (string dc in cols)
  586. {
  587. ICell cell = irow6.CreateCell(icolIndex);
  588. cell.SetCellValue(dc);
  589. cell.CellStyle = cellStylebt;
  590. icolIndex++;
  591. }
  592. iRowIndex++;
  593. rows = dt.Select("category=2");
  594. index = 0;
  595. foreach (DataRow dr in rows)
  596. {
  597. index++;
  598. int iCellIndex = 0;
  599. IRow irow = sheet.CreateRow(iRowIndex);
  600. for (int i = 0; i < 20; i++)
  601. {
  602. ICell cell = irow.CreateCell(iCellIndex);
  603. cell.SetCellValue(GetCellValue(dr, index,i ));
  604. cell.CellStyle = cellStyle;
  605. iCellIndex++;
  606. }
  607. iRowIndex++;
  608. }
  609. IRow irow7 = sheet.CreateRow(iRowIndex);
  610. ICell cell7 = irow7.CreateCell(0);
  611. cell7.SetCellValue("三、市直机关联动单位二组");
  612. cell7.CellStyle = cellStylebt;
  613. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  614. iRowIndex++;
  615. IRow irow8 = sheet.CreateRow(iRowIndex);
  616. icolIndex = 0;
  617. foreach (string dc in cols)
  618. {
  619. ICell cell = irow8.CreateCell(icolIndex);
  620. cell.SetCellValue(dc);
  621. cell.CellStyle = cellStylebt;
  622. icolIndex++;
  623. }
  624. iRowIndex++;
  625. rows = dt.Select("category=3");
  626. index = 0;
  627. foreach (DataRow dr in rows)
  628. {
  629. index++;
  630. int iCellIndex = 0;
  631. IRow irow = sheet.CreateRow(iRowIndex);
  632. for (int i = 0; i < 20; i++)
  633. {
  634. ICell cell = irow.CreateCell(iCellIndex);
  635. cell.SetCellValue(GetCellValue(dr, index,i ));
  636. cell.CellStyle = cellStyle;
  637. iCellIndex++;
  638. }
  639. iRowIndex++;
  640. }
  641. IRow irow9 = sheet.CreateRow(iRowIndex);
  642. ICell cell9 = irow9.CreateCell(0);
  643. cell9.SetCellValue("四、公益型企业联动单位");
  644. cell9.CellStyle = cellStylebt;
  645. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  646. iRowIndex++;
  647. IRow irow10 = sheet.CreateRow(iRowIndex);
  648. icolIndex = 0;
  649. foreach (string dc in cols)
  650. {
  651. ICell cell = irow10.CreateCell(icolIndex);
  652. cell.SetCellValue(dc);
  653. cell.CellStyle = cellStylebt;
  654. icolIndex++;
  655. }
  656. iRowIndex++;
  657. rows = dt.Select("category=4");
  658. index = 0;
  659. foreach (DataRow dr in rows)
  660. {
  661. index++;
  662. int iCellIndex = 0;
  663. IRow irow = sheet.CreateRow(iRowIndex);
  664. for (int i = 0; i < 20; i++)
  665. {
  666. ICell cell = irow.CreateCell(iCellIndex);
  667. cell.SetCellValue(GetCellValue(dr, index,i ));
  668. cell.CellStyle = cellStyle;
  669. iCellIndex++;
  670. }
  671. iRowIndex++;
  672. }
  673. //自适应列宽度
  674. for (int i = 0; i < 20; i++)
  675. {
  676. sheet.AutoSizeColumn(i);
  677. // sheet.SetColumnWidth(i, 20 * 256);
  678. }
  679. using (MemoryStream ms = new MemoryStream())
  680. {
  681. workbook.Write(ms);
  682. HttpContext curContext = HttpContext.Current;
  683. // 设置编码和附件格式
  684. curContext.Response.ContentType = "application/vnd.ms-excel";
  685. curContext.Response.ContentEncoding = Encoding.UTF8;
  686. curContext.Response.Charset = "";
  687. curContext.Response.AppendHeader("Content-Disposition",
  688. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  689. curContext.Response.BinaryWrite(ms.GetBuffer());
  690. workbook = null;
  691. ms.Close();
  692. ms.Dispose();
  693. curContext.Response.End();
  694. }
  695. return "";
  696. }
  697. catch
  698. {
  699. return "导出失败!";
  700. }
  701. }
  702. /// <summary>
  703. /// 弹出下载框导出excel
  704. /// </summary>
  705. /// <param name="Name"></param>
  706. /// <param name="dt"></param>
  707. /// <returns></returns>
  708. public string TSExportToExcel(DataTable dt, int tscount)
  709. {
  710. try
  711. {
  712. //if (dt.Rows.Count > 0)
  713. //{
  714. HSSFWorkbook workbook = new HSSFWorkbook();
  715. ISheet sheet = workbook.CreateSheet("Sheet1");
  716. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  717. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  718. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  719. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  720. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  721. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  722. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  723. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  724. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  725. //字体
  726. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  727. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  728. headerfont.FontHeightInPoints = 12;
  729. HeadercellStyle.SetFont(headerfont);
  730. //用column name 作为列名
  731. int icolIndex = 0;
  732. IRow headerRow = sheet.CreateRow(0);
  733. foreach (DataColumn dc in dt.Columns)
  734. {
  735. ICell cell = headerRow.CreateCell(icolIndex);
  736. cell.SetCellValue(dc.ColumnName);
  737. cell.CellStyle = HeadercellStyle;
  738. icolIndex++;
  739. }
  740. ICellStyle cellStyle = workbook.CreateCellStyle();
  741. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  742. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  743. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  744. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  745. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  746. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  747. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  748. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  749. cellStyle.SetFont(cellfont);
  750. //建立内容行
  751. int iRowIndex = 0;
  752. foreach (DataRow dr in dt.Rows)
  753. {
  754. int iCellIndex = 0;
  755. IRow irow = sheet.CreateRow(iRowIndex + 1);
  756. for (int i = 0; i < dt.Columns.Count; i++)
  757. {
  758. string strsj = string.Empty;
  759. if (dr[i] != null)
  760. {
  761. strsj = dr[i].ToString();
  762. }
  763. ICell cell = irow.CreateCell(iCellIndex);
  764. cell.SetCellValue(strsj);
  765. cell.CellStyle = cellStyle;
  766. iCellIndex++;
  767. }
  768. iRowIndex++;
  769. }
  770. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 1));
  771. sheet.AddMergedRegion(new CellRangeAddress(1, tscount, 0, 0));
  772. for (int i = iRowIndex; i > tscount; i--)
  773. {
  774. sheet.AddMergedRegion(new CellRangeAddress(i, i, 0, 1));
  775. }
  776. //自适应列宽度
  777. for (int i = 0; i < icolIndex; i++)
  778. {
  779. sheet.AutoSizeColumn(i);
  780. }
  781. using (MemoryStream ms = new MemoryStream())
  782. {
  783. workbook.Write(ms);
  784. HttpContext curContext = HttpContext.Current;
  785. // 设置编码和附件格式
  786. curContext.Response.ContentType = "application/vnd.ms-excel";
  787. curContext.Response.ContentEncoding = Encoding.UTF8;
  788. curContext.Response.Charset = "";
  789. curContext.Response.AppendHeader("Content-Disposition",
  790. "attachment;filename=" + HttpUtility.UrlEncode("投诉统计_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  791. curContext.Response.BinaryWrite(ms.GetBuffer());
  792. workbook = null;
  793. ms.Close();
  794. ms.Dispose();
  795. curContext.Response.End();
  796. }
  797. //}
  798. return "";
  799. }
  800. catch
  801. {
  802. return "导出失败!";
  803. }
  804. }
  805. /// <summary>
  806. /// 生成excel到路径
  807. /// </summary>
  808. /// <param name="Name"></param>
  809. /// <param name="dt"></param>
  810. /// <returns></returns>
  811. public string CreateExcelFile(string Name, DataTable dt, string Path,string[] cols = null)
  812. {
  813. try
  814. {
  815. if (dt.Rows.Count > 0)
  816. {
  817. HSSFWorkbook workbook = new HSSFWorkbook();
  818. ISheet sheet = workbook.CreateSheet(Name);
  819. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  820. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  821. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  822. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  823. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  824. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  825. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  826. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  827. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  828. //字体
  829. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  830. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  831. headerfont.FontHeightInPoints = 12;
  832. HeadercellStyle.SetFont(headerfont);
  833. //用column name 作为列名
  834. int icolIndex = 0;
  835. IRow headerRow = sheet.CreateRow(0);
  836. if (cols == null || (cols != null && cols.Length == 0))
  837. {
  838. foreach (DataColumn dc in dt.Columns)
  839. {
  840. ICell cell = headerRow.CreateCell(icolIndex);
  841. cell.SetCellValue(dc.ColumnName);
  842. cell.CellStyle = HeadercellStyle;
  843. icolIndex++;
  844. }
  845. }
  846. else
  847. {
  848. foreach (string dc in cols)
  849. {
  850. ICell cell = headerRow.CreateCell(icolIndex);
  851. cell.SetCellValue(dc);
  852. cell.CellStyle = HeadercellStyle;
  853. icolIndex++;
  854. }
  855. }
  856. ICellStyle cellStyle = workbook.CreateCellStyle();
  857. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  858. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  859. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  860. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  861. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  862. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  863. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  864. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  865. cellStyle.SetFont(cellfont);
  866. //建立内容行
  867. int iRowIndex = 0;
  868. foreach (DataRow dr in dt.Rows)
  869. {
  870. int iCellIndex = 0;
  871. IRow irow = sheet.CreateRow(iRowIndex + 1);
  872. for (int i = 0; i < dt.Columns.Count; i++)
  873. {
  874. string strsj = string.Empty;
  875. if (dr[i] != null)
  876. {
  877. strsj = dr[i].ToString();
  878. }
  879. ICell cell = irow.CreateCell(iCellIndex);
  880. cell.SetCellValue(strsj);
  881. cell.CellStyle = cellStyle;
  882. iCellIndex++;
  883. }
  884. iRowIndex++;
  885. }
  886. //自适应列宽度
  887. for (int i = 0; i < icolIndex; i++)
  888. {
  889. sheet.AutoSizeColumn(i);
  890. }
  891. Name = HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls");
  892. Path = HttpContext.Current.Server.MapPath("..") + Path;
  893. if (!Directory.Exists(Path)) Directory.CreateDirectory(Path);
  894. FileStream fileHSSF = new FileStream(Path+ Name, FileMode.Create);
  895. workbook.Write(fileHSSF);
  896. fileHSSF.Close();
  897. fileHSSF.Dispose();
  898. workbook = null;
  899. }
  900. return "";
  901. }
  902. catch
  903. {
  904. return "生成失败!";
  905. }
  906. }
  907. /// <summary>
  908. /// 弹出下载框导出excel
  909. /// </summary>
  910. /// <param name="Name"></param>
  911. /// <param name="dt"></param>
  912. /// <returns></returns>
  913. public string ExportToExcel64(string name, string base64url)
  914. {
  915. try
  916. {
  917. int delLength = base64url.IndexOf(',') + 1;
  918. string str = base64url.Substring(delLength, base64url.Length - delLength);
  919. byte[] bData = Convert.FromBase64String(str);
  920. HttpContext curContext = HttpContext.Current;
  921. // 设置编码和附件格式
  922. curContext.Response.ContentType = "application/vnd.ms-excel";
  923. curContext.Response.ContentEncoding = Encoding.UTF8;
  924. curContext.Response.Charset = "";
  925. curContext.Response.AppendHeader("Content-Disposition",
  926. "attachment;filename=" + HttpUtility.UrlEncode(name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  927. curContext.Response.BinaryWrite(bData);
  928. curContext.Response.End();
  929. return "";
  930. }
  931. catch
  932. {
  933. return "导出失败!";
  934. }
  935. }
  936. /// <summary>
  937. /// 简报导出
  938. /// </summary>
  939. /// <param name="ds"></param>
  940. /// <returns></returns>
  941. public string SimpleExportToExcel(DataSet ds)
  942. {
  943. try
  944. {
  945. HSSFWorkbook workbook = new HSSFWorkbook();
  946. ISheet sheet = workbook.CreateSheet("Sheet1");
  947. ICellStyle cellStyle = workbook.CreateCellStyle();
  948. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  949. //cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  950. //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  951. //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  952. //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  953. //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  954. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  955. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  956. cellStyle.SetFont(cellfont);
  957. ICellStyle cellStylebt = workbook.CreateCellStyle();
  958. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  959. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  960. cellStylebt.SetFont(cellfontbt);
  961. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  962. cellStylebt.Alignment = HorizontalAlignment.Center;
  963. IRow irow1 = sheet.CreateRow(1);
  964. ICell cell1 = irow1.CreateCell(0);
  965. cell1.SetCellValue("时间:"+ ds.Tables[5].Rows[0]["sdate"].ToString()+ " 至 "+ ds.Tables[5].Rows[0]["edate"].ToString());
  966. cell1.CellStyle = cellStylebt;
  967. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 7));
  968. //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(new CellRangeAddress(1, 1, 0, 7), BorderStyle.Thin, NPOI.HSSF.Util.HSSFColor.Black.Index);
  969. #region 话务受理情况
  970. DataTable dt1 = ds.Tables[0];
  971. IRow irow2 = sheet.CreateRow(2);
  972. ICell cell2 = irow2.CreateCell(0);
  973. cell2.SetCellValue("话务受理情况");
  974. cell2.CellStyle = cellStylebt;
  975. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 7));
  976. IRow irow3 = sheet.CreateRow(3);
  977. ICell cell31 = irow3.CreateCell(0);
  978. cell31.SetCellValue("来电");
  979. cell31.CellStyle = cellStyle;
  980. ICell cell32 = irow3.CreateCell(1);
  981. cell32.SetCellValue(Int32.Parse(dt1.Rows[0]["ldcount"].ToString()));
  982. cell32.CellStyle = cellStyle;
  983. ICell cell33 = irow3.CreateCell(2);
  984. cell33.SetCellValue("接听");
  985. cell33.CellStyle = cellStyle;
  986. ICell cell34 = irow3.CreateCell(3);
  987. cell34.SetCellValue(Int32.Parse(dt1.Rows[0]["jtcount"].ToString()));
  988. cell34.CellStyle = cellStyle;
  989. ICell cell35 = irow3.CreateCell(4);
  990. cell35.SetCellValue("有效接听");
  991. cell35.CellStyle = cellStyle;
  992. ICell cell36 = irow3.CreateCell(5);
  993. cell36.SetCellValue(Int32.Parse(dt1.Rows[0]["yxcount"].ToString()));
  994. cell36.CellStyle = cellStyle;
  995. #endregion
  996. #region 工单受理情况
  997. DataTable dt2 = ds.Tables[1];
  998. IRow irow4 = sheet.CreateRow(4);
  999. ICell cell4 = irow4.CreateCell(0);
  1000. cell4.SetCellValue("工单受理情况");
  1001. cell4.CellStyle = cellStylebt;
  1002. sheet.AddMergedRegion(new CellRangeAddress(4, 4, 0, 7));
  1003. IRow irow5 = sheet.CreateRow(5);
  1004. ICell cell51 = irow5.CreateCell(0);
  1005. cell51.SetCellValue("受理");
  1006. cell51.CellStyle = cellStyle;
  1007. ICell cell52 = irow5.CreateCell(1);
  1008. cell52.SetCellValue(Int32.Parse(dt2.Rows[0]["slcount"].ToString()));
  1009. cell52.CellStyle = cellStyle;
  1010. ICell cell53 = irow5.CreateCell(2);
  1011. cell53.SetCellValue("待提交");
  1012. cell53.CellStyle = cellStyle;
  1013. ICell cell54 = irow5.CreateCell(3);
  1014. cell54.SetCellValue(Int32.Parse(dt2.Rows[0]["dtjcount"].ToString()));
  1015. cell54.CellStyle = cellStyle;
  1016. ICell cell55 = irow5.CreateCell(4);
  1017. cell55.SetCellValue("待交办");
  1018. cell55.CellStyle = cellStyle;
  1019. ICell cell56 = irow5.CreateCell(5);
  1020. cell56.SetCellValue(Int32.Parse(dt2.Rows[0]["djbcount"].ToString()));
  1021. cell56.CellStyle = cellStyle;
  1022. ICell cell57 = irow5.CreateCell(6);
  1023. cell57.SetCellValue("待查收");
  1024. cell57.CellStyle = cellStyle;
  1025. ICell cell58 = irow5.CreateCell(7);
  1026. cell58.SetCellValue(Int32.Parse(dt2.Rows[0]["dcscount"].ToString()));
  1027. cell58.CellStyle = cellStyle;
  1028. IRow irow6 = sheet.CreateRow(6);
  1029. ICell cell61 = irow6.CreateCell(0);
  1030. cell61.SetCellValue("待审核退回");
  1031. cell61.CellStyle = cellStyle;
  1032. ICell cell62 = irow6.CreateCell(1);
  1033. cell62.SetCellValue(Int32.Parse(dt2.Rows[0]["dshthcount"].ToString()));
  1034. cell62.CellStyle = cellStyle;
  1035. ICell cell63 = irow6.CreateCell(2);
  1036. cell63.SetCellValue("待办理");
  1037. cell63.CellStyle = cellStyle;
  1038. ICell cell64 = irow6.CreateCell(3);
  1039. cell64.SetCellValue(Int32.Parse(dt2.Rows[0]["dblcount"].ToString()));
  1040. cell64.CellStyle = cellStyle;
  1041. ICell cell65 = irow6.CreateCell(4);
  1042. cell65.SetCellValue("待延时审核");
  1043. cell65.CellStyle = cellStyle;
  1044. ICell cell66 = irow6.CreateCell(5);
  1045. cell66.SetCellValue(Int32.Parse(dt2.Rows[0]["dshyscount"].ToString()));
  1046. cell66.CellStyle = cellStyle;
  1047. ICell cell67 = irow6.CreateCell(6);
  1048. cell67.SetCellValue("待回访");
  1049. cell67.CellStyle = cellStyle;
  1050. ICell cell68 = irow6.CreateCell(7);
  1051. cell68.SetCellValue(Int32.Parse(dt2.Rows[0]["dhfcount"].ToString()));
  1052. cell68.CellStyle = cellStyle;
  1053. IRow irow7 = sheet.CreateRow(7);
  1054. ICell cell71 = irow7.CreateCell(0);
  1055. cell71.SetCellValue("待结案");
  1056. cell71.CellStyle = cellStyle;
  1057. ICell cell72 = irow7.CreateCell(1);
  1058. cell72.SetCellValue(Int32.Parse(dt2.Rows[0]["dwjcount"].ToString()));
  1059. cell72.CellStyle = cellStyle;
  1060. ICell cell73 = irow7.CreateCell(2);
  1061. cell73.SetCellValue("待重办");
  1062. cell73.CellStyle = cellStyle;
  1063. ICell cell74 = irow7.CreateCell(3);
  1064. cell74.SetCellValue(Int32.Parse(dt2.Rows[0]["dcbcount"].ToString()));
  1065. cell74.CellStyle = cellStyle;
  1066. ICell cell75 = irow7.CreateCell(4);
  1067. cell75.SetCellValue("已结案");
  1068. cell75.CellStyle = cellStyle;
  1069. ICell cell76 = irow7.CreateCell(5);
  1070. cell76.SetCellValue(Int32.Parse(dt2.Rows[0]["ywjcount"].ToString()));
  1071. cell76.CellStyle = cellStyle;
  1072. ICell cell77 = irow7.CreateCell(6);
  1073. cell77.SetCellValue("在线办理");
  1074. cell77.CellStyle = cellStyle;
  1075. ICell cell78 = irow7.CreateCell(7);
  1076. cell78.SetCellValue(Int32.Parse(dt2.Rows[0]["zxbjcount"].ToString()));
  1077. cell78.CellStyle = cellStyle;
  1078. IRow irow8 = sheet.CreateRow(8);
  1079. ICell cell81 = irow8.CreateCell(0);
  1080. cell81.SetCellValue("中心转派");
  1081. cell81.CellStyle = cellStyle;
  1082. ICell cell82 = irow8.CreateCell(1);
  1083. cell82.SetCellValue(Int32.Parse(dt2.Rows[0]["zxzpcount"].ToString()));
  1084. cell82.CellStyle = cellStyle;
  1085. ICell cell83 = irow8.CreateCell(2);
  1086. cell83.SetCellValue("在线办理率");
  1087. cell83.CellStyle = cellStyle;
  1088. ICell cell84 = irow8.CreateCell(3);
  1089. cell84.SetCellValue(dt2.Rows[0]["zxbjrate"].ToString());
  1090. cell84.CellStyle = cellStyle;
  1091. #endregion
  1092. #region 政府热线受理情况
  1093. DataTable dt3 = ds.Tables[2];
  1094. IRow irow9 = sheet.CreateRow(9);
  1095. ICell cell9 = irow9.CreateCell(0);
  1096. cell9.SetCellValue("政府热线受理情况");
  1097. cell9.CellStyle = cellStylebt;
  1098. sheet.AddMergedRegion(new CellRangeAddress(9, 9, 0, 7));
  1099. IRow irow10 = sheet.CreateRow(10);
  1100. IRow irow11 = sheet.CreateRow(11);
  1101. int n = 0;
  1102. foreach (DataRow dr3 in dt3.Rows)
  1103. {
  1104. if (n < 4)
  1105. {
  1106. ICell cell101 = irow10.CreateCell(2 * (n + 1) - 2);
  1107. cell101.SetCellValue(dr3["source"].ToString());
  1108. cell101.CellStyle = cellStyle;
  1109. ICell cell102 = irow10.CreateCell(2 * (n + 1) - 1);
  1110. cell102.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  1111. cell102.CellStyle = cellStyle;
  1112. }
  1113. else
  1114. {
  1115. ICell cell111 = irow11.CreateCell(2 * (n -3) - 2);
  1116. cell111.SetCellValue(dr3["source"].ToString());
  1117. cell111.CellStyle = cellStyle;
  1118. ICell cell112 = irow11.CreateCell(2 * (n - 3) - 1);
  1119. cell112.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  1120. cell112.CellStyle = cellStyle;
  1121. }
  1122. n = n + 1;
  1123. }
  1124. #endregion
  1125. #region 工单受理类型情况
  1126. DataTable dt4 = ds.Tables[3];
  1127. IRow irow12 = sheet.CreateRow(12);
  1128. ICell cell12 = irow12.CreateCell(0);
  1129. cell12.SetCellValue("工单受理类型情况");
  1130. cell12.CellStyle = cellStylebt;
  1131. sheet.AddMergedRegion(new CellRangeAddress(12, 12, 0, 7));
  1132. IRow irow13 = sheet.CreateRow(13);
  1133. IRow irow14 = sheet.CreateRow(14);
  1134. int m = 0;
  1135. foreach (DataRow dr4 in dt4.Rows)
  1136. {
  1137. if (m < 4)
  1138. {
  1139. ICell cell131 = irow13.CreateCell(2 * (m + 1) - 2);
  1140. cell131.SetCellValue(dr4["type"].ToString());
  1141. cell131.CellStyle = cellStyle;
  1142. ICell cell132 = irow13.CreateCell(2 * (m + 1) - 1);
  1143. cell132.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  1144. cell132.CellStyle = cellStyle;
  1145. }
  1146. else
  1147. {
  1148. ICell cell141 = irow14.CreateCell(2 * (m - 3) - 2);
  1149. cell141.SetCellValue(dr4["type"].ToString());
  1150. cell141.CellStyle = cellStyle;
  1151. ICell cell152 = irow14.CreateCell(2 * (m - 3) - 1);
  1152. cell152.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  1153. cell152.CellStyle = cellStyle;
  1154. }
  1155. m = m + 1;
  1156. }
  1157. #endregion
  1158. #region 工单事发区域情况
  1159. DataTable dt5 = ds.Tables[4];
  1160. IRow irow15 = sheet.CreateRow(15);
  1161. ICell cell15 = irow15.CreateCell(0);
  1162. cell15.SetCellValue("工单事发区域情况");
  1163. cell15.CellStyle = cellStylebt;
  1164. sheet.AddMergedRegion(new CellRangeAddress(15, 15, 0, 7));
  1165. int rowcount = (dt5.Rows.Count / 4) + 1;
  1166. for (int i = 0; i < rowcount; i++)
  1167. {
  1168. IRow irow = sheet.CreateRow(16 + i);
  1169. for (int j = 0; j < 4; j++)
  1170. {
  1171. int num = i * 4 + j;
  1172. if (num != dt5.Rows.Count)
  1173. {
  1174. var dr = dt5.Rows[num];
  1175. ICell cellname = irow.CreateCell(2 * (j + 1) - 2);
  1176. cellname.SetCellValue(dr["areaname"].ToString());
  1177. cellname.CellStyle = cellStyle;
  1178. ICell cellcount = irow.CreateCell(2 * (j + 1) - 1);
  1179. cellcount.SetCellValue(Int32.Parse(dr["count"].ToString()));
  1180. cellcount.CellStyle = cellStyle;
  1181. }
  1182. else
  1183. {
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. #endregion
  1189. //自适应列宽度
  1190. for (int i = 0; i < 8; i++)
  1191. {
  1192. sheet.AutoSizeColumn(i);
  1193. }
  1194. using (MemoryStream ms = new MemoryStream())
  1195. {
  1196. workbook.Write(ms);
  1197. HttpContext curContext = HttpContext.Current;
  1198. // 设置编码和附件格式
  1199. curContext.Response.ContentType = "application/vnd.ms-excel";
  1200. curContext.Response.ContentEncoding = Encoding.UTF8;
  1201. curContext.Response.Charset = "";
  1202. curContext.Response.AppendHeader("Content-Disposition",
  1203. "attachment;filename=" + HttpUtility.UrlEncode("业务简报_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  1204. curContext.Response.BinaryWrite(ms.GetBuffer());
  1205. workbook = null;
  1206. ms.Close();
  1207. ms.Dispose();
  1208. curContext.Response.End();
  1209. }
  1210. return "";
  1211. }
  1212. catch
  1213. {
  1214. return "导出失败!";
  1215. }
  1216. }
  1217. /// <summary>
  1218. /// 弹出下载框导出excel(数据第一行为标题)
  1219. /// </summary>
  1220. /// <param name="Name"></param>
  1221. /// <param name="dt"></param>
  1222. /// <returns></returns>
  1223. public string ExportToExcel2(string Name, DataTable dt, string[] cols = null)
  1224. {
  1225. try
  1226. {
  1227. //if (dt.Rows.Count > 0)
  1228. //{
  1229. HSSFWorkbook workbook = new HSSFWorkbook();
  1230. ISheet sheet = workbook.CreateSheet("Sheet1");
  1231. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  1232. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1233. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1234. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1235. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1236. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  1237. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1238. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  1239. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1240. //字体
  1241. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  1242. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  1243. headerfont.FontHeightInPoints = 12;
  1244. HeadercellStyle.SetFont(headerfont);
  1245. //用column name 作为列名
  1246. int icolIndex = 0;
  1247. IRow headerRow = sheet.CreateRow(0);
  1248. if (cols == null || (cols != null && cols.Length == 0))
  1249. {
  1250. //foreach (DataColumn dc in dt.Columns)
  1251. //{
  1252. // ICell cell = headerRow.CreateCell(icolIndex);
  1253. // cell.SetCellValue(dc.ColumnName);
  1254. // cell.CellStyle = HeadercellStyle;
  1255. // icolIndex++;
  1256. //}
  1257. for (int i = 0; i < dt.Columns.Count; i++)
  1258. {
  1259. ICell cell = headerRow.CreateCell(icolIndex);
  1260. cell.SetCellValue(dt.Rows[0][i].ToString());
  1261. cell.CellStyle = HeadercellStyle;
  1262. icolIndex++;
  1263. }
  1264. }
  1265. else
  1266. {
  1267. foreach (string dc in cols)
  1268. {
  1269. ICell cell = headerRow.CreateCell(icolIndex);
  1270. cell.SetCellValue(dc);
  1271. cell.CellStyle = HeadercellStyle;
  1272. icolIndex++;
  1273. }
  1274. }
  1275. ICellStyle cellStyle = workbook.CreateCellStyle();
  1276. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  1277. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  1278. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1279. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1280. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1281. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1282. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1283. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1284. cellStyle.SetFont(cellfont);
  1285. //建立内容行
  1286. int iRowIndex = 0;
  1287. foreach (DataRow dr in dt.Rows)
  1288. {
  1289. if (iRowIndex > 0)
  1290. {
  1291. int iCellIndex = 0;
  1292. IRow irow = sheet.CreateRow(iRowIndex);
  1293. for (int i = 0; i < dt.Columns.Count; i++)
  1294. {
  1295. string strsj = string.Empty;
  1296. if (dr[i] != null)
  1297. {
  1298. strsj = dr[i].ToString();
  1299. }
  1300. ICell cell = irow.CreateCell(iCellIndex);
  1301. cell.SetCellValue(strsj);
  1302. cell.CellStyle = cellStyle;
  1303. iCellIndex++;
  1304. }
  1305. }
  1306. iRowIndex++;
  1307. }
  1308. //自适应列宽度
  1309. for (int i = 0; i < icolIndex; i++)
  1310. {
  1311. sheet.AutoSizeColumn(i);
  1312. }
  1313. using (MemoryStream ms = new MemoryStream())
  1314. {
  1315. workbook.Write(ms);
  1316. HttpContext curContext = HttpContext.Current;
  1317. // 设置编码和附件格式
  1318. curContext.Response.ContentType = "application/vnd.ms-excel";
  1319. curContext.Response.ContentEncoding = Encoding.UTF8;
  1320. curContext.Response.Charset = "";
  1321. curContext.Response.AppendHeader("Content-Disposition",
  1322. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  1323. curContext.Response.BinaryWrite(ms.GetBuffer());
  1324. workbook = null;
  1325. ms.Close();
  1326. ms.Dispose();
  1327. curContext.Response.End();
  1328. }
  1329. //}
  1330. return "";
  1331. }
  1332. catch
  1333. {
  1334. return "导出失败!";
  1335. }
  1336. }
  1337. }
  1338. }