Няма описание

NPOIHelper.cs 55KB

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