Geen omschrijving

NPOIHelper.cs 80KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  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.OpenXmlFormats.Wordprocessing;
  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 ExportToExcel(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 < dt.Columns.Count; 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转换为datatable
  346. /// </summary>
  347. /// <param name="upfile"></param>
  348. /// <param name="headrow"></param>
  349. /// <returns></returns>
  350. public DataTable ExcelToTable(HttpPostedFile upfile, int headrow)
  351. {
  352. DataTable dt = new DataTable();
  353. IWorkbook workbook = null;
  354. Stream stream = upfile.InputStream;
  355. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  356. if (suffix == "xlsx") // 2007版本
  357. {
  358. workbook = new XSSFWorkbook(stream);
  359. }
  360. else if (suffix == "xls") // 2003版本
  361. {
  362. workbook = new HSSFWorkbook(stream);
  363. }
  364. //获取excel的第一个sheet
  365. ISheet sheet = workbook.GetSheetAt(0);
  366. //获取sheet的第一行
  367. IRow headerRow = sheet.GetRow(headrow);
  368. //一行最后一个方格的编号 即总的列数
  369. int cellCount = headerRow.LastCellNum;
  370. //最后一列的标号 即总的行数
  371. int rowCount = sheet.LastRowNum;
  372. //列名
  373. for (int i = 0; i < cellCount; i++)
  374. {
  375. dt.Columns.Add(headerRow.GetCell(i).ToString());
  376. }
  377. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  378. {
  379. DataRow dr = dt.NewRow();
  380. IRow row = sheet.GetRow(i);
  381. for (int j = row.FirstCellNum; j < cellCount; j++)
  382. {
  383. if (row.GetCell(j) != null)
  384. {
  385. dr[j] = row.GetCell(j).ToString();
  386. }
  387. }
  388. dt.Rows.Add(dr);
  389. }
  390. sheet = null;
  391. workbook = null;
  392. return dt;
  393. }
  394. /// <summary>
  395. /// 导入excel转换为datatable
  396. /// </summary>
  397. /// <param name="upfile"></param>
  398. /// <param name="headrow"></param>
  399. /// <returns></returns>
  400. public DataTable ExcelToTable(HttpPostedFileBase upfile, int headrow)
  401. {
  402. DataTable dt = new DataTable();
  403. IWorkbook workbook = null;
  404. Stream stream = upfile.InputStream;
  405. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  406. if (suffix == "xlsx") // 2007版本
  407. {
  408. workbook = new XSSFWorkbook(stream);
  409. }
  410. else if (suffix == "xls") // 2003版本
  411. {
  412. workbook = new HSSFWorkbook(stream);
  413. }
  414. //获取excel的第一个sheet
  415. ISheet sheet = workbook.GetSheetAt(0);
  416. //获取sheet的第一行
  417. IRow headerRow = sheet.GetRow(headrow);
  418. //一行最后一个方格的编号 即总的列数
  419. int cellCount = headerRow.LastCellNum;
  420. //最后一列的标号 即总的行数
  421. int rowCount = sheet.LastRowNum;
  422. //列名
  423. for (int i = 0; i < cellCount; i++)
  424. {
  425. dt.Columns.Add(headerRow.GetCell(i).ToString());
  426. }
  427. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  428. {
  429. DataRow dr = dt.NewRow();
  430. IRow row = sheet.GetRow(i);
  431. for (int j = row.FirstCellNum; j < cellCount; j++)
  432. {
  433. if (row.GetCell(j) != null)
  434. {
  435. dr[j] = row.GetCell(j).ToString().Trim(' ').Trim('\t');
  436. }
  437. }
  438. dt.Rows.Add(dr);
  439. }
  440. sheet = null;
  441. workbook = null;
  442. return dt;
  443. }
  444. private string GetCellValue(DataRow dr,int index,int i )
  445. {
  446. string str = "";
  447. switch (i)
  448. {
  449. case 0:
  450. str = index.ToString();
  451. break;
  452. case 1:
  453. str = dr["deptname"].ToString();
  454. break;
  455. case 2:
  456. str = dr["Undertakers"].ToString();
  457. break;
  458. case 3:
  459. str = dr["Undertakersrate"].ToString();
  460. break;
  461. case 4:
  462. str = dr["Undertakersscore"].ToString();
  463. break;
  464. case 5:
  465. str = dr["overdue"].ToString();
  466. break;
  467. case 6:
  468. str = dr["overduerate"].ToString();
  469. break;
  470. case 7:
  471. str = dr["overduescore"].ToString();
  472. break;
  473. case 8:
  474. str = dr["unsuccessful"].ToString();
  475. break;
  476. case 9:
  477. str = dr["unsuccessfulrate"].ToString();
  478. break;
  479. case 10:
  480. str = dr["unsuccessfulscore"].ToString();
  481. break;
  482. case 11:
  483. str = dr["Chargeback"].ToString();
  484. break;
  485. case 12:
  486. str = dr["Chargebackrate"].ToString();
  487. break;
  488. case 13:
  489. str = dr["Chargebackscore"].ToString();
  490. break;
  491. case 14:
  492. str = dr["Comment"].ToString();
  493. break;
  494. case 15:
  495. str = dr["dissatisfied"].ToString();
  496. break;
  497. case 16:
  498. str = dr["satisfiedrate"].ToString();
  499. break;
  500. case 17:
  501. str = dr["satisfiedscore"].ToString();
  502. break;
  503. case 18:
  504. str = dr["total"].ToString();
  505. break;
  506. case 19:
  507. str = index.ToString();
  508. break;
  509. }
  510. return str;
  511. }
  512. /// <summary>
  513. /// 督办数据报表导出
  514. /// </summary>
  515. /// <param name="ds"></param>
  516. /// <returns></returns>
  517. public string DBExportToExcel(DataTable dt,string Name,string month,string starttime ,string endtime )
  518. {
  519. try
  520. {
  521. HSSFWorkbook workbook = new HSSFWorkbook();
  522. ISheet sheet = workbook.CreateSheet(Name);
  523. ICellStyle cellStyle = workbook.CreateCellStyle();
  524. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  525. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  526. cellStyle.SetFont(cellfont);
  527. ICellStyle cellStylebt = workbook.CreateCellStyle();
  528. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  529. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  530. cellStylebt.SetFont(cellfontbt);
  531. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  532. cellStylebt.Alignment = HorizontalAlignment.Center;
  533. IRow irow1 = sheet.CreateRow(0);
  534. ICell cell1 = irow1.CreateCell(0);
  535. cell1.SetCellValue("12345联动服务工作"+ month + "月份办理情况通报表");
  536. cell1.CellStyle = cellStylebt;
  537. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 19));
  538. IRow irow2 = sheet.CreateRow(2);
  539. ICell cell2 = irow2.CreateCell(0);
  540. cell2.SetCellValue("统计周期:"+ starttime+"至"+ endtime+" 统计时间:"+DateTime .Now.ToString ("yyyy年MM月dd日"));
  541. cell2.CellStyle = cellStylebt;
  542. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 19));
  543. IRow irow3 = sheet.CreateRow(3);
  544. ICell cell3 = irow3.CreateCell(0);
  545. cell3.SetCellValue("一、县(市、区)联动单位");
  546. cell3.CellStyle = cellStylebt;
  547. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 0, 19));
  548. string[] cols = {"序号","联动单位","承办件",
  549. "承办率","得分(5分)","超期件","按时反馈率","得分(20分)",
  550. "未果件","办结率","得分(10分)","退单件","有效回复率",
  551. "得分(15分)","群众评议总数","一次不满意件","不满意件","满意率","得分(50分)","总分","排名"};
  552. IRow irow4 = sheet.CreateRow(4);
  553. int icolIndex = 0;
  554. foreach (string dc in cols)
  555. {
  556. ICell cell = irow4.CreateCell(icolIndex);
  557. cell.SetCellValue(dc);
  558. cell.CellStyle = cellStylebt;
  559. icolIndex++;
  560. }
  561. int iRowIndex = 5;
  562. DataRow[] rows = dt.Select("category=1");
  563. int index = 0;
  564. foreach (DataRow dr in rows)
  565. {
  566. index++;
  567. int iCellIndex = 0;
  568. IRow irow = sheet.CreateRow(iRowIndex );
  569. for (int i = 0; i < 20; i++)
  570. {
  571. ICell cell = irow.CreateCell(iCellIndex);
  572. cell.SetCellValue(GetCellValue(dr, index,i ));
  573. cell.CellStyle = cellStyle;
  574. iCellIndex++;
  575. }
  576. iRowIndex++;
  577. }
  578. IRow irow5 = sheet.CreateRow(iRowIndex);
  579. ICell cell5 = irow5.CreateCell(0);
  580. cell5.SetCellValue("二、市直机关联动单位一组");
  581. cell5.CellStyle = cellStylebt;
  582. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  583. iRowIndex++;
  584. IRow irow6 = sheet.CreateRow(iRowIndex);
  585. icolIndex = 0;
  586. foreach (string dc in cols)
  587. {
  588. ICell cell = irow6.CreateCell(icolIndex);
  589. cell.SetCellValue(dc);
  590. cell.CellStyle = cellStylebt;
  591. icolIndex++;
  592. }
  593. iRowIndex++;
  594. rows = dt.Select("category=2");
  595. index = 0;
  596. foreach (DataRow dr in rows)
  597. {
  598. index++;
  599. int iCellIndex = 0;
  600. IRow irow = sheet.CreateRow(iRowIndex);
  601. for (int i = 0; i < 20; i++)
  602. {
  603. ICell cell = irow.CreateCell(iCellIndex);
  604. cell.SetCellValue(GetCellValue(dr, index,i ));
  605. cell.CellStyle = cellStyle;
  606. iCellIndex++;
  607. }
  608. iRowIndex++;
  609. }
  610. IRow irow7 = sheet.CreateRow(iRowIndex);
  611. ICell cell7 = irow7.CreateCell(0);
  612. cell7.SetCellValue("三、市直机关联动单位二组");
  613. cell7.CellStyle = cellStylebt;
  614. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  615. iRowIndex++;
  616. IRow irow8 = sheet.CreateRow(iRowIndex);
  617. icolIndex = 0;
  618. foreach (string dc in cols)
  619. {
  620. ICell cell = irow8.CreateCell(icolIndex);
  621. cell.SetCellValue(dc);
  622. cell.CellStyle = cellStylebt;
  623. icolIndex++;
  624. }
  625. iRowIndex++;
  626. rows = dt.Select("category=3");
  627. index = 0;
  628. foreach (DataRow dr in rows)
  629. {
  630. index++;
  631. int iCellIndex = 0;
  632. IRow irow = sheet.CreateRow(iRowIndex);
  633. for (int i = 0; i < 20; i++)
  634. {
  635. ICell cell = irow.CreateCell(iCellIndex);
  636. cell.SetCellValue(GetCellValue(dr, index,i ));
  637. cell.CellStyle = cellStyle;
  638. iCellIndex++;
  639. }
  640. iRowIndex++;
  641. }
  642. IRow irow9 = sheet.CreateRow(iRowIndex);
  643. ICell cell9 = irow9.CreateCell(0);
  644. cell9.SetCellValue("四、公益型企业联动单位");
  645. cell9.CellStyle = cellStylebt;
  646. sheet.AddMergedRegion(new CellRangeAddress(iRowIndex, iRowIndex, 0, 19));
  647. iRowIndex++;
  648. IRow irow10 = sheet.CreateRow(iRowIndex);
  649. icolIndex = 0;
  650. foreach (string dc in cols)
  651. {
  652. ICell cell = irow10.CreateCell(icolIndex);
  653. cell.SetCellValue(dc);
  654. cell.CellStyle = cellStylebt;
  655. icolIndex++;
  656. }
  657. iRowIndex++;
  658. rows = dt.Select("category=4");
  659. index = 0;
  660. foreach (DataRow dr in rows)
  661. {
  662. index++;
  663. int iCellIndex = 0;
  664. IRow irow = sheet.CreateRow(iRowIndex);
  665. for (int i = 0; i < 20; i++)
  666. {
  667. ICell cell = irow.CreateCell(iCellIndex);
  668. cell.SetCellValue(GetCellValue(dr, index,i ));
  669. cell.CellStyle = cellStyle;
  670. iCellIndex++;
  671. }
  672. iRowIndex++;
  673. }
  674. //自适应列宽度
  675. for (int i = 0; i < 20; i++)
  676. {
  677. sheet.AutoSizeColumn(i);
  678. // sheet.SetColumnWidth(i, 20 * 256);
  679. }
  680. using (MemoryStream ms = new MemoryStream())
  681. {
  682. workbook.Write(ms);
  683. HttpContext curContext = HttpContext.Current;
  684. // 设置编码和附件格式
  685. curContext.Response.ContentType = "application/vnd.ms-excel";
  686. curContext.Response.ContentEncoding = Encoding.UTF8;
  687. curContext.Response.Charset = "";
  688. curContext.Response.AppendHeader("Content-Disposition",
  689. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  690. curContext.Response.BinaryWrite(ms.GetBuffer());
  691. workbook = null;
  692. ms.Close();
  693. ms.Dispose();
  694. curContext.Response.End();
  695. }
  696. return "";
  697. }
  698. catch
  699. {
  700. return "导出失败!";
  701. }
  702. }
  703. /// <summary>
  704. /// 督办数据报表导出
  705. /// </summary>
  706. /// <param name="ds"></param>
  707. /// <returns></returns>
  708. public string DBEJExportToExcel(DataTable dt, string Name, string month, string starttime, string endtime)
  709. {
  710. try
  711. {
  712. HSSFWorkbook workbook = new HSSFWorkbook();
  713. ISheet sheet = workbook.CreateSheet(Name);
  714. ICellStyle cellStyle = workbook.CreateCellStyle();
  715. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  716. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  717. cellStyle.SetFont(cellfont);
  718. ICellStyle cellStylebt = workbook.CreateCellStyle();
  719. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  720. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  721. cellStylebt.SetFont(cellfontbt);
  722. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  723. cellStylebt.Alignment = HorizontalAlignment.Center;
  724. IRow irow1 = sheet.CreateRow(0);
  725. ICell cell1 = irow1.CreateCell(0);
  726. cell1.SetCellValue("12345联动服务工作" + month + "月份办理情况通报表");
  727. cell1.CellStyle = cellStylebt;
  728. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 19));
  729. IRow irow2 = sheet.CreateRow(2);
  730. ICell cell2 = irow2.CreateCell(0);
  731. cell2.SetCellValue("统计周期:" + starttime + "至" + endtime + " 统计时间:" + DateTime.Now.ToString("yyyy年MM月dd日"));
  732. cell2.CellStyle = cellStylebt;
  733. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 19));
  734. IRow irow3 = sheet.CreateRow(3);
  735. ICell cell3 = irow3.CreateCell(0);
  736. cell3.SetCellValue("一、县(市、区)联动单位");
  737. cell3.CellStyle = cellStylebt;
  738. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 0, 19));
  739. string[] cols = {"序号","联动单位","承办件",
  740. "承办率","得分(5分)","超期件","按时反馈率","得分(20分)",
  741. "未果件","办结率","得分(10分)","退单件","有效回复率",
  742. "得分(15分)","群众评议总数","一次不满意件","不满意件","满意率","得分(50分)","总分","排名"};
  743. IRow irow4 = sheet.CreateRow(4);
  744. int icolIndex = 0;
  745. foreach (string dc in cols)
  746. {
  747. ICell cell = irow4.CreateCell(icolIndex);
  748. cell.SetCellValue(dc);
  749. cell.CellStyle = cellStylebt;
  750. icolIndex++;
  751. }
  752. int iRowIndex = 5;
  753. // DataRow[] rows = dt.Select("category=1");
  754. int index = 0;
  755. foreach (DataRow dr in dt.Rows )
  756. {
  757. index++;
  758. int iCellIndex = 0;
  759. IRow irow = sheet.CreateRow(iRowIndex);
  760. for (int i = 0; i < 20; i++)
  761. {
  762. ICell cell = irow.CreateCell(iCellIndex);
  763. cell.SetCellValue(GetCellValue(dr, index, i));
  764. cell.CellStyle = cellStyle;
  765. iCellIndex++;
  766. }
  767. iRowIndex++;
  768. }
  769. //自适应列宽度
  770. for (int i = 0; i < 20; i++)
  771. {
  772. sheet.AutoSizeColumn(i);
  773. // sheet.SetColumnWidth(i, 20 * 256);
  774. }
  775. using (MemoryStream ms = new MemoryStream())
  776. {
  777. workbook.Write(ms);
  778. HttpContext curContext = HttpContext.Current;
  779. // 设置编码和附件格式
  780. curContext.Response.ContentType = "application/vnd.ms-excel";
  781. curContext.Response.ContentEncoding = Encoding.UTF8;
  782. curContext.Response.Charset = "";
  783. curContext.Response.AppendHeader("Content-Disposition",
  784. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  785. curContext.Response.BinaryWrite(ms.GetBuffer());
  786. workbook = null;
  787. ms.Close();
  788. ms.Dispose();
  789. curContext.Response.End();
  790. }
  791. return "";
  792. }
  793. catch
  794. {
  795. return "导出失败!";
  796. }
  797. }
  798. /// <summary>
  799. /// 弹出下载框导出excel
  800. /// </summary>
  801. /// <param name="Name"></param>
  802. /// <param name="dt"></param>
  803. /// <returns></returns>
  804. public string TSExportToExcel(DataTable dt, int tscount)
  805. {
  806. try
  807. {
  808. //if (dt.Rows.Count > 0)
  809. //{
  810. HSSFWorkbook workbook = new HSSFWorkbook();
  811. ISheet sheet = workbook.CreateSheet("Sheet1");
  812. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  813. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  814. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  815. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  816. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  817. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  818. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  819. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  820. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  821. //字体
  822. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  823. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  824. headerfont.FontHeightInPoints = 12;
  825. HeadercellStyle.SetFont(headerfont);
  826. //用column name 作为列名
  827. int icolIndex = 0;
  828. IRow headerRow = sheet.CreateRow(0);
  829. foreach (DataColumn dc in dt.Columns)
  830. {
  831. ICell cell = headerRow.CreateCell(icolIndex);
  832. cell.SetCellValue(dc.ColumnName);
  833. cell.CellStyle = HeadercellStyle;
  834. icolIndex++;
  835. }
  836. ICellStyle cellStyle = workbook.CreateCellStyle();
  837. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  838. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  839. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  840. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  841. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  842. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  843. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  844. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  845. cellStyle.SetFont(cellfont);
  846. //建立内容行
  847. int iRowIndex = 0;
  848. foreach (DataRow dr in dt.Rows)
  849. {
  850. int iCellIndex = 0;
  851. IRow irow = sheet.CreateRow(iRowIndex + 1);
  852. for (int i = 0; i < dt.Columns.Count; i++)
  853. {
  854. string strsj = string.Empty;
  855. if (dr[i] != null)
  856. {
  857. strsj = dr[i].ToString();
  858. }
  859. ICell cell = irow.CreateCell(iCellIndex);
  860. cell.SetCellValue(strsj);
  861. cell.CellStyle = cellStyle;
  862. iCellIndex++;
  863. }
  864. iRowIndex++;
  865. }
  866. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 1));
  867. sheet.AddMergedRegion(new CellRangeAddress(1, tscount, 0, 0));
  868. for (int i = iRowIndex; i > tscount; i--)
  869. {
  870. sheet.AddMergedRegion(new CellRangeAddress(i, i, 0, 1));
  871. }
  872. //自适应列宽度
  873. for (int i = 0; i < icolIndex; i++)
  874. {
  875. sheet.AutoSizeColumn(i);
  876. }
  877. using (MemoryStream ms = new MemoryStream())
  878. {
  879. workbook.Write(ms);
  880. HttpContext curContext = HttpContext.Current;
  881. // 设置编码和附件格式
  882. curContext.Response.ContentType = "application/vnd.ms-excel";
  883. curContext.Response.ContentEncoding = Encoding.UTF8;
  884. curContext.Response.Charset = "";
  885. curContext.Response.AppendHeader("Content-Disposition",
  886. "attachment;filename=" + HttpUtility.UrlEncode("投诉统计_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  887. curContext.Response.BinaryWrite(ms.GetBuffer());
  888. workbook = null;
  889. ms.Close();
  890. ms.Dispose();
  891. curContext.Response.End();
  892. }
  893. //}
  894. return "";
  895. }
  896. catch
  897. {
  898. return "导出失败!";
  899. }
  900. }
  901. /// <summary>
  902. /// 生成excel到路径
  903. /// </summary>
  904. /// <param name="Name"></param>
  905. /// <param name="dt"></param>
  906. /// <returns></returns>
  907. public string CreateExcelFile(string Name, DataTable dt, string Path,string[] cols = null)
  908. {
  909. try
  910. {
  911. if (dt.Rows.Count > 0)
  912. {
  913. HSSFWorkbook workbook = new HSSFWorkbook();
  914. ISheet sheet = workbook.CreateSheet(Name);
  915. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  916. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  917. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  918. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  919. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  920. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  921. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  922. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  923. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  924. //字体
  925. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  926. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  927. headerfont.FontHeightInPoints = 12;
  928. HeadercellStyle.SetFont(headerfont);
  929. //用column name 作为列名
  930. int icolIndex = 0;
  931. IRow headerRow = sheet.CreateRow(0);
  932. if (cols == null || (cols != null && cols.Length == 0))
  933. {
  934. foreach (DataColumn dc in dt.Columns)
  935. {
  936. ICell cell = headerRow.CreateCell(icolIndex);
  937. cell.SetCellValue(dc.ColumnName);
  938. cell.CellStyle = HeadercellStyle;
  939. icolIndex++;
  940. }
  941. }
  942. else
  943. {
  944. foreach (string dc in cols)
  945. {
  946. ICell cell = headerRow.CreateCell(icolIndex);
  947. cell.SetCellValue(dc);
  948. cell.CellStyle = HeadercellStyle;
  949. icolIndex++;
  950. }
  951. }
  952. ICellStyle cellStyle = workbook.CreateCellStyle();
  953. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  954. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  955. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  956. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  957. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  958. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  959. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  960. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  961. cellStyle.SetFont(cellfont);
  962. //建立内容行
  963. int iRowIndex = 0;
  964. foreach (DataRow dr in dt.Rows)
  965. {
  966. int iCellIndex = 0;
  967. IRow irow = sheet.CreateRow(iRowIndex + 1);
  968. for (int i = 0; i < dt.Columns.Count; i++)
  969. {
  970. string strsj = string.Empty;
  971. if (dr[i] != null)
  972. {
  973. strsj = dr[i].ToString();
  974. }
  975. ICell cell = irow.CreateCell(iCellIndex);
  976. cell.SetCellValue(strsj);
  977. cell.CellStyle = cellStyle;
  978. iCellIndex++;
  979. }
  980. iRowIndex++;
  981. }
  982. //自适应列宽度
  983. for (int i = 0; i < icolIndex; i++)
  984. {
  985. sheet.AutoSizeColumn(i);
  986. }
  987. Name = HttpUtility.UrlEncode(Name + "_导出文件_" + DateTime.Now.Ticks + ".xls");
  988. Path = HttpContext.Current.Server.MapPath("..") + Path;
  989. if (!Directory.Exists(Path)) Directory.CreateDirectory(Path);
  990. FileStream fileHSSF = new FileStream(Path+ Name, FileMode.Create);
  991. workbook.Write(fileHSSF);
  992. fileHSSF.Close();
  993. fileHSSF.Dispose();
  994. workbook = null;
  995. }
  996. return "";
  997. }
  998. catch
  999. {
  1000. return "生成失败!";
  1001. }
  1002. }
  1003. /// <summary>
  1004. /// 弹出下载框导出excel
  1005. /// </summary>
  1006. /// <param name="Name"></param>
  1007. /// <param name="dt"></param>
  1008. /// <returns></returns>
  1009. public string ExportToExcel64(string name, string base64url)
  1010. {
  1011. try
  1012. {
  1013. int delLength = base64url.IndexOf(',') + 1;
  1014. string str = base64url.Substring(delLength, base64url.Length - delLength);
  1015. byte[] bData = Convert.FromBase64String(str);
  1016. HttpContext curContext = HttpContext.Current;
  1017. // 设置编码和附件格式
  1018. curContext.Response.ContentType = "application/vnd.ms-excel";
  1019. curContext.Response.ContentEncoding = Encoding.UTF8;
  1020. curContext.Response.Charset = "";
  1021. curContext.Response.AppendHeader("Content-Disposition",
  1022. "attachment;filename=" + HttpUtility.UrlEncode(name + "_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  1023. curContext.Response.BinaryWrite(bData);
  1024. curContext.Response.End();
  1025. return "";
  1026. }
  1027. catch
  1028. {
  1029. return "导出失败!";
  1030. }
  1031. }
  1032. /// <summary>
  1033. /// 简报导出
  1034. /// </summary>
  1035. /// <param name="ds"></param>
  1036. /// <returns></returns>
  1037. public string SimpleExportToExcel(DataSet ds)
  1038. {
  1039. try
  1040. {
  1041. HSSFWorkbook workbook = new HSSFWorkbook();
  1042. ISheet sheet = workbook.CreateSheet("Sheet1");
  1043. ICellStyle cellStyle = workbook.CreateCellStyle();
  1044. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  1045. //cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  1046. //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1047. //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1048. //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1049. //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1050. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1051. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1052. cellStyle.SetFont(cellfont);
  1053. ICellStyle cellStylebt = workbook.CreateCellStyle();
  1054. NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
  1055. cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
  1056. cellStylebt.SetFont(cellfontbt);
  1057. cellStylebt.VerticalAlignment = VerticalAlignment.Center;
  1058. cellStylebt.Alignment = HorizontalAlignment.Center;
  1059. IRow irow1 = sheet.CreateRow(1);
  1060. ICell cell1 = irow1.CreateCell(0);
  1061. cell1.SetCellValue("时间:"+ ds.Tables[5].Rows[0]["sdate"].ToString()+ " 至 "+ ds.Tables[5].Rows[0]["edate"].ToString());
  1062. cell1.CellStyle = cellStylebt;
  1063. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 7));
  1064. //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(new CellRangeAddress(1, 1, 0, 7), BorderStyle.Thin, NPOI.HSSF.Util.HSSFColor.Black.Index);
  1065. #region 话务受理情况
  1066. DataTable dt1 = ds.Tables[0];
  1067. IRow irow2 = sheet.CreateRow(2);
  1068. ICell cell2 = irow2.CreateCell(0);
  1069. cell2.SetCellValue("话务受理情况");
  1070. cell2.CellStyle = cellStylebt;
  1071. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 7));
  1072. IRow irow3 = sheet.CreateRow(3);
  1073. ICell cell31 = irow3.CreateCell(0);
  1074. cell31.SetCellValue("来电");
  1075. cell31.CellStyle = cellStyle;
  1076. ICell cell32 = irow3.CreateCell(1);
  1077. cell32.SetCellValue(Int32.Parse(dt1.Rows[0]["ldcount"].ToString()));
  1078. cell32.CellStyle = cellStyle;
  1079. ICell cell33 = irow3.CreateCell(2);
  1080. cell33.SetCellValue("接听");
  1081. cell33.CellStyle = cellStyle;
  1082. ICell cell34 = irow3.CreateCell(3);
  1083. cell34.SetCellValue(Int32.Parse(dt1.Rows[0]["jtcount"].ToString()));
  1084. cell34.CellStyle = cellStyle;
  1085. ICell cell35 = irow3.CreateCell(4);
  1086. cell35.SetCellValue("有效接听");
  1087. cell35.CellStyle = cellStyle;
  1088. ICell cell36 = irow3.CreateCell(5);
  1089. cell36.SetCellValue(Int32.Parse(dt1.Rows[0]["yxcount"].ToString()));
  1090. cell36.CellStyle = cellStyle;
  1091. #endregion
  1092. #region 工单受理情况
  1093. DataTable dt2 = ds.Tables[1];
  1094. IRow irow4 = sheet.CreateRow(4);
  1095. ICell cell4 = irow4.CreateCell(0);
  1096. cell4.SetCellValue("工单受理情况");
  1097. cell4.CellStyle = cellStylebt;
  1098. sheet.AddMergedRegion(new CellRangeAddress(4, 4, 0, 7));
  1099. IRow irow5 = sheet.CreateRow(5);
  1100. ICell cell51 = irow5.CreateCell(0);
  1101. cell51.SetCellValue("受理");
  1102. cell51.CellStyle = cellStyle;
  1103. ICell cell52 = irow5.CreateCell(1);
  1104. cell52.SetCellValue(Int32.Parse(dt2.Rows[0]["slcount"].ToString()));
  1105. cell52.CellStyle = cellStyle;
  1106. ICell cell53 = irow5.CreateCell(2);
  1107. cell53.SetCellValue("待提交");
  1108. cell53.CellStyle = cellStyle;
  1109. ICell cell54 = irow5.CreateCell(3);
  1110. cell54.SetCellValue(Int32.Parse(dt2.Rows[0]["dtjcount"].ToString()));
  1111. cell54.CellStyle = cellStyle;
  1112. ICell cell55 = irow5.CreateCell(4);
  1113. cell55.SetCellValue("待交办");
  1114. cell55.CellStyle = cellStyle;
  1115. ICell cell56 = irow5.CreateCell(5);
  1116. cell56.SetCellValue(Int32.Parse(dt2.Rows[0]["djbcount"].ToString()));
  1117. cell56.CellStyle = cellStyle;
  1118. ICell cell57 = irow5.CreateCell(6);
  1119. cell57.SetCellValue("待查收");
  1120. cell57.CellStyle = cellStyle;
  1121. ICell cell58 = irow5.CreateCell(7);
  1122. cell58.SetCellValue(Int32.Parse(dt2.Rows[0]["dcscount"].ToString()));
  1123. cell58.CellStyle = cellStyle;
  1124. IRow irow6 = sheet.CreateRow(6);
  1125. ICell cell61 = irow6.CreateCell(0);
  1126. cell61.SetCellValue("待审核退回");
  1127. cell61.CellStyle = cellStyle;
  1128. ICell cell62 = irow6.CreateCell(1);
  1129. cell62.SetCellValue(Int32.Parse(dt2.Rows[0]["dshthcount"].ToString()));
  1130. cell62.CellStyle = cellStyle;
  1131. ICell cell63 = irow6.CreateCell(2);
  1132. cell63.SetCellValue("待办理");
  1133. cell63.CellStyle = cellStyle;
  1134. ICell cell64 = irow6.CreateCell(3);
  1135. cell64.SetCellValue(Int32.Parse(dt2.Rows[0]["dblcount"].ToString()));
  1136. cell64.CellStyle = cellStyle;
  1137. ICell cell65 = irow6.CreateCell(4);
  1138. cell65.SetCellValue("待延时审核");
  1139. cell65.CellStyle = cellStyle;
  1140. ICell cell66 = irow6.CreateCell(5);
  1141. cell66.SetCellValue(Int32.Parse(dt2.Rows[0]["dshyscount"].ToString()));
  1142. cell66.CellStyle = cellStyle;
  1143. ICell cell67 = irow6.CreateCell(6);
  1144. cell67.SetCellValue("待回访");
  1145. cell67.CellStyle = cellStyle;
  1146. ICell cell68 = irow6.CreateCell(7);
  1147. cell68.SetCellValue(Int32.Parse(dt2.Rows[0]["dhfcount"].ToString()));
  1148. cell68.CellStyle = cellStyle;
  1149. IRow irow7 = sheet.CreateRow(7);
  1150. ICell cell71 = irow7.CreateCell(0);
  1151. cell71.SetCellValue("待结案");
  1152. cell71.CellStyle = cellStyle;
  1153. ICell cell72 = irow7.CreateCell(1);
  1154. cell72.SetCellValue(Int32.Parse(dt2.Rows[0]["dwjcount"].ToString()));
  1155. cell72.CellStyle = cellStyle;
  1156. ICell cell73 = irow7.CreateCell(2);
  1157. cell73.SetCellValue("待重办");
  1158. cell73.CellStyle = cellStyle;
  1159. ICell cell74 = irow7.CreateCell(3);
  1160. cell74.SetCellValue(Int32.Parse(dt2.Rows[0]["dcbcount"].ToString()));
  1161. cell74.CellStyle = cellStyle;
  1162. ICell cell75 = irow7.CreateCell(4);
  1163. cell75.SetCellValue("已结案");
  1164. cell75.CellStyle = cellStyle;
  1165. ICell cell76 = irow7.CreateCell(5);
  1166. cell76.SetCellValue(Int32.Parse(dt2.Rows[0]["ywjcount"].ToString()));
  1167. cell76.CellStyle = cellStyle;
  1168. ICell cell77 = irow7.CreateCell(6);
  1169. cell77.SetCellValue("在线办理");
  1170. cell77.CellStyle = cellStyle;
  1171. ICell cell78 = irow7.CreateCell(7);
  1172. cell78.SetCellValue(Int32.Parse(dt2.Rows[0]["zxbjcount"].ToString()));
  1173. cell78.CellStyle = cellStyle;
  1174. IRow irow8 = sheet.CreateRow(8);
  1175. ICell cell81 = irow8.CreateCell(0);
  1176. cell81.SetCellValue("中心转派");
  1177. cell81.CellStyle = cellStyle;
  1178. ICell cell82 = irow8.CreateCell(1);
  1179. cell82.SetCellValue(Int32.Parse(dt2.Rows[0]["zxzpcount"].ToString()));
  1180. cell82.CellStyle = cellStyle;
  1181. ICell cell83 = irow8.CreateCell(2);
  1182. cell83.SetCellValue("在线办理率");
  1183. cell83.CellStyle = cellStyle;
  1184. ICell cell84 = irow8.CreateCell(3);
  1185. cell84.SetCellValue(dt2.Rows[0]["zxbjrate"].ToString());
  1186. cell84.CellStyle = cellStyle;
  1187. #endregion
  1188. #region 政府热线受理情况
  1189. DataTable dt3 = ds.Tables[2];
  1190. IRow irow9 = sheet.CreateRow(9);
  1191. ICell cell9 = irow9.CreateCell(0);
  1192. cell9.SetCellValue("政府热线受理情况");
  1193. cell9.CellStyle = cellStylebt;
  1194. sheet.AddMergedRegion(new CellRangeAddress(9, 9, 0, 7));
  1195. IRow irow10 = sheet.CreateRow(10);
  1196. IRow irow11 = sheet.CreateRow(11);
  1197. int n = 0;
  1198. foreach (DataRow dr3 in dt3.Rows)
  1199. {
  1200. if (n < 4)
  1201. {
  1202. ICell cell101 = irow10.CreateCell(2 * (n + 1) - 2);
  1203. cell101.SetCellValue(dr3["source"].ToString());
  1204. cell101.CellStyle = cellStyle;
  1205. ICell cell102 = irow10.CreateCell(2 * (n + 1) - 1);
  1206. cell102.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  1207. cell102.CellStyle = cellStyle;
  1208. }
  1209. else
  1210. {
  1211. ICell cell111 = irow11.CreateCell(2 * (n -3) - 2);
  1212. cell111.SetCellValue(dr3["source"].ToString());
  1213. cell111.CellStyle = cellStyle;
  1214. ICell cell112 = irow11.CreateCell(2 * (n - 3) - 1);
  1215. cell112.SetCellValue(Int32.Parse(dr3["count"].ToString()));
  1216. cell112.CellStyle = cellStyle;
  1217. }
  1218. n = n + 1;
  1219. }
  1220. #endregion
  1221. #region 工单受理类型情况
  1222. DataTable dt4 = ds.Tables[3];
  1223. IRow irow12 = sheet.CreateRow(12);
  1224. ICell cell12 = irow12.CreateCell(0);
  1225. cell12.SetCellValue("工单受理类型情况");
  1226. cell12.CellStyle = cellStylebt;
  1227. sheet.AddMergedRegion(new CellRangeAddress(12, 12, 0, 7));
  1228. IRow irow13 = sheet.CreateRow(13);
  1229. IRow irow14 = sheet.CreateRow(14);
  1230. int m = 0;
  1231. foreach (DataRow dr4 in dt4.Rows)
  1232. {
  1233. if (m < 4)
  1234. {
  1235. ICell cell131 = irow13.CreateCell(2 * (m + 1) - 2);
  1236. cell131.SetCellValue(dr4["type"].ToString());
  1237. cell131.CellStyle = cellStyle;
  1238. ICell cell132 = irow13.CreateCell(2 * (m + 1) - 1);
  1239. cell132.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  1240. cell132.CellStyle = cellStyle;
  1241. }
  1242. else
  1243. {
  1244. ICell cell141 = irow14.CreateCell(2 * (m - 3) - 2);
  1245. cell141.SetCellValue(dr4["type"].ToString());
  1246. cell141.CellStyle = cellStyle;
  1247. ICell cell152 = irow14.CreateCell(2 * (m - 3) - 1);
  1248. cell152.SetCellValue(Int32.Parse(dr4["count"].ToString()));
  1249. cell152.CellStyle = cellStyle;
  1250. }
  1251. m = m + 1;
  1252. }
  1253. #endregion
  1254. #region 工单事发区域情况
  1255. DataTable dt5 = ds.Tables[4];
  1256. IRow irow15 = sheet.CreateRow(15);
  1257. ICell cell15 = irow15.CreateCell(0);
  1258. cell15.SetCellValue("工单事发区域情况");
  1259. cell15.CellStyle = cellStylebt;
  1260. sheet.AddMergedRegion(new CellRangeAddress(15, 15, 0, 7));
  1261. int rowcount = (dt5.Rows.Count / 4) + 1;
  1262. for (int i = 0; i < rowcount; i++)
  1263. {
  1264. IRow irow = sheet.CreateRow(16 + i);
  1265. for (int j = 0; j < 4; j++)
  1266. {
  1267. int num = i * 4 + j;
  1268. if (num != dt5.Rows.Count)
  1269. {
  1270. var dr = dt5.Rows[num];
  1271. ICell cellname = irow.CreateCell(2 * (j + 1) - 2);
  1272. cellname.SetCellValue(dr["areaname"].ToString());
  1273. cellname.CellStyle = cellStyle;
  1274. ICell cellcount = irow.CreateCell(2 * (j + 1) - 1);
  1275. cellcount.SetCellValue(Int32.Parse(dr["count"].ToString()));
  1276. cellcount.CellStyle = cellStyle;
  1277. }
  1278. else
  1279. {
  1280. break;
  1281. }
  1282. }
  1283. }
  1284. #endregion
  1285. //自适应列宽度
  1286. for (int i = 0; i < 8; i++)
  1287. {
  1288. sheet.AutoSizeColumn(i);
  1289. }
  1290. using (MemoryStream ms = new MemoryStream())
  1291. {
  1292. workbook.Write(ms);
  1293. HttpContext curContext = HttpContext.Current;
  1294. // 设置编码和附件格式
  1295. curContext.Response.ContentType = "application/vnd.ms-excel";
  1296. curContext.Response.ContentEncoding = Encoding.UTF8;
  1297. curContext.Response.Charset = "";
  1298. curContext.Response.AppendHeader("Content-Disposition",
  1299. "attachment;filename=" + HttpUtility.UrlEncode("业务简报_导出文件_" + DateTime.Now.Ticks + ".xls", Encoding.UTF8));
  1300. curContext.Response.BinaryWrite(ms.GetBuffer());
  1301. workbook = null;
  1302. ms.Close();
  1303. ms.Dispose();
  1304. curContext.Response.End();
  1305. }
  1306. return "";
  1307. }
  1308. catch
  1309. {
  1310. return "导出失败!";
  1311. }
  1312. }
  1313. /// <summary>
  1314. /// 弹出下载框导出excel(数据第一行为标题)
  1315. /// </summary>
  1316. /// <param name="Name"></param>
  1317. /// <param name="dt"></param>
  1318. /// <returns></returns>
  1319. public string ExportToExcel2(string Name, DataTable dt, string[] cols = null)
  1320. {
  1321. try
  1322. {
  1323. //if (dt.Rows.Count > 0)
  1324. //{
  1325. HSSFWorkbook workbook = new HSSFWorkbook();
  1326. ISheet sheet = workbook.CreateSheet("Sheet1");
  1327. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  1328. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1329. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1330. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1331. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1332. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  1333. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1334. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  1335. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  1336. //字体
  1337. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  1338. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  1339. headerfont.FontHeightInPoints = 12;
  1340. HeadercellStyle.SetFont(headerfont);
  1341. //用column name 作为列名
  1342. int icolIndex = 0;
  1343. IRow headerRow = sheet.CreateRow(0);
  1344. if (cols == null || (cols != null && cols.Length == 0))
  1345. {
  1346. //foreach (DataColumn dc in dt.Columns)
  1347. //{
  1348. // ICell cell = headerRow.CreateCell(icolIndex);
  1349. // cell.SetCellValue(dc.ColumnName);
  1350. // cell.CellStyle = HeadercellStyle;
  1351. // icolIndex++;
  1352. //}
  1353. for (int i = 0; i < dt.Columns.Count; i++)
  1354. {
  1355. ICell cell = headerRow.CreateCell(icolIndex);
  1356. cell.SetCellValue(dt.Rows[0][i].ToString());
  1357. cell.CellStyle = HeadercellStyle;
  1358. icolIndex++;
  1359. }
  1360. }
  1361. else
  1362. {
  1363. foreach (string dc in cols)
  1364. {
  1365. ICell cell = headerRow.CreateCell(icolIndex);
  1366. cell.SetCellValue(dc);
  1367. cell.CellStyle = HeadercellStyle;
  1368. icolIndex++;
  1369. }
  1370. }
  1371. ICellStyle cellStyle = workbook.CreateCellStyle();
  1372. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  1373. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  1374. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1375. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1376. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1377. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1378. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  1379. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  1380. cellStyle.SetFont(cellfont);
  1381. //建立内容行
  1382. int iRowIndex = 0;
  1383. foreach (DataRow dr in dt.Rows)
  1384. {
  1385. if (iRowIndex > 0)
  1386. {
  1387. int iCellIndex = 0;
  1388. IRow irow = sheet.CreateRow(iRowIndex);
  1389. for (int i = 0; i < dt.Columns.Count; i++)
  1390. {
  1391. string strsj = string.Empty;
  1392. if (dr[i] != null)
  1393. {
  1394. strsj = dr[i].ToString();
  1395. }
  1396. ICell cell = irow.CreateCell(iCellIndex);
  1397. cell.SetCellValue(strsj);
  1398. cell.CellStyle = cellStyle;
  1399. iCellIndex++;
  1400. }
  1401. }
  1402. iRowIndex++;
  1403. }
  1404. //自适应列宽度
  1405. for (int i = 0; i < icolIndex; i++)
  1406. {
  1407. sheet.AutoSizeColumn(i);
  1408. }
  1409. using (MemoryStream ms = new MemoryStream())
  1410. {
  1411. workbook.Write(ms);
  1412. HttpContext curContext = HttpContext.Current;
  1413. // 设置编码和附件格式
  1414. curContext.Response.ContentType = "application/vnd.ms-excel";
  1415. curContext.Response.ContentEncoding = Encoding.UTF8;
  1416. curContext.Response.Charset = "";
  1417. curContext.Response.AppendHeader("Content-Disposition",
  1418. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  1419. curContext.Response.BinaryWrite(ms.GetBuffer());
  1420. workbook = null;
  1421. ms.Close();
  1422. ms.Dispose();
  1423. curContext.Response.End();
  1424. }
  1425. //}
  1426. return "";
  1427. }
  1428. catch
  1429. {
  1430. return "导出失败!";
  1431. }
  1432. }
  1433. /// <summary>
  1434. /// 导出word
  1435. /// </summary>
  1436. /// <returns></returns>
  1437. public byte[] ExportToWordDemo()
  1438. {
  1439. try
  1440. {
  1441. //创建document文档对象对象实例
  1442. NPOI.XWPF.UserModel.XWPFDocument document = new NPOI.XWPF.UserModel.XWPFDocument();
  1443. document.CreateParagraph();
  1444. NPOI.XWPF.UserModel.XWPFParagraph paragraph = document.CreateParagraph();//创建段落对象
  1445. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;//文字显示位置,段落排列(左对齐,居中,右对齐)
  1446. NPOI.XWPF.UserModel.XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象
  1447. xwpfRun.IsBold = true;//文字加粗
  1448. xwpfRun.SetText("安阳市12345政务服务热线交办单");//填充内容
  1449. xwpfRun.FontSize = 18;//设置文字大小
  1450. xwpfRun.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  1451. document.CreateParagraph();
  1452. //创建文档中的表格对象实例
  1453. NPOI.XWPF.UserModel.XWPFTable table = document.CreateTable(9, 8);//显示的行列数rows:3行,cols:4列
  1454. table.Width = 5200;//总宽度
  1455. table.SetColumnWidth(0, 650); /* 设置列宽 */
  1456. table.SetColumnWidth(1, 650);
  1457. table.SetColumnWidth(2, 650);
  1458. table.SetColumnWidth(3, 650);
  1459. table.SetColumnWidth(4, 650);
  1460. table.SetColumnWidth(5, 650);
  1461. table.SetColumnWidth(6, 650);
  1462. table.SetColumnWidth(7, 650);
  1463. for (int i = 0; i < 9; i++)
  1464. {
  1465. for (int j = 0; j < 8; j++)
  1466. {
  1467. var cp = table.GetRow(i).GetCell(j).GetCTTc().AddNewTcPr();
  1468. cp.AddNewVAlign().val = ST_VerticalJc.center;
  1469. }
  1470. }
  1471. //第一行
  1472. table.GetRow(0).GetCTRow().AddNewTrPr().AddNewTrHeight().val= (ulong)300;
  1473. var cp0 = table.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr();
  1474. cp0.AddNewVMerge().val = ST_Merge.restart;
  1475. cp0.AddNewVAlign().val = ST_VerticalJc.center;
  1476. cp0.tcW = new CT_TblWidth();cp0.tcW.w = "650";cp0.tcW.type = ST_TblWidth.dxa;
  1477. table.GetRow(0).GetCell(0).SetParagraph(GetParagraph(table, true, "事项编号"));
  1478. var cp1 = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr();
  1479. cp1.AddNewVMerge().val = ST_Merge.restart;
  1480. cp1.AddNewVAlign().val = ST_VerticalJc.center;
  1481. table.GetRow(0).GetCell(1).SetParagraph(GetParagraph(table, false, "DH9941052721041200351"));
  1482. table.GetRow(0).GetCell(2).SetParagraph(GetParagraph(table, true, "工单来源"));
  1483. table.GetRow(0).GetCell(3).SetParagraph(GetParagraph(table, false, "市长电话"));
  1484. table.GetRow(0).GetCell(4).SetParagraph(GetParagraph(table, true, "办理时限"));
  1485. table.GetRow(0).GetCell(5).SetParagraph(GetParagraph(table, false, "2021-04-21"));
  1486. table.GetRow(0).GetCell(6).SetParagraph(GetParagraph(table, true, "联系电话"));
  1487. table.GetRow(0).GetCell(7).SetParagraph(GetParagraph(table, false, "017185328292"));
  1488. //第二行
  1489. table.GetRow(1).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)300;
  1490. var cp2 = table.GetRow(1).GetCell(0).GetCTTc().AddNewTcPr();
  1491. cp2.AddNewVMerge().val = ST_Merge.@continue;
  1492. var cp3 = table.GetRow(1).GetCell(1).GetCTTc().AddNewTcPr();
  1493. cp3.AddNewVMerge().val = ST_Merge.@continue;
  1494. table.GetRow(1).GetCell(2).SetParagraph(GetParagraph(table, true, "签收时间"));
  1495. table.GetRow(1).GetCell(3).SetParagraph(GetParagraph(table, false, "2021-04-12"));
  1496. table.GetRow(1).GetCell(4).SetParagraph(GetParagraph(table, true, "办理时间"));
  1497. table.GetRow(1).GetCell(5).SetParagraph(GetParagraph(table, false, "2021-04-12"));
  1498. table.GetRow(1).GetCell(6).SetParagraph(GetParagraph(table, true, "来电人"));
  1499. table.GetRow(1).GetCell(7).SetParagraph(GetParagraph(table, false, "徐青田"));
  1500. //第三行
  1501. table.GetRow(2).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)300;
  1502. table.GetRow(2).MergeCells(1, 7);//合并列
  1503. table.GetRow(2).GetCell(0).SetParagraph(GetParagraph(table, true, "事项区域"));
  1504. table.GetRow(2).GetCell(1).SetParagraph(GetParagraph(table, false, "河南省郑州市高新技术开发区大学科技园东区", false));
  1505. //第四行
  1506. table.GetRow(3).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)2000;
  1507. table.GetRow(3).MergeCells(1, 7);//合并列
  1508. table.GetRow(3).GetCell(0).SetParagraph(GetParagraph(table, true, "内容摘要"));
  1509. table.GetRow(3).GetCell(1).SetParagraph(GetParagraph(table, false, "来电人反映:内黄县城关镇宛庄村第四生产队第二、三小组土地至今无法确权(遗留问题),村委会和镇政府承诺给解决,至今未解决,请政府责成相关部门调查处理。", false));
  1510. //第五行
  1511. table.GetRow(4).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)300;
  1512. table.GetRow(4).MergeCells(1, 4);//合并列
  1513. table.GetRow(4).MergeCells(3, 4);//合并列
  1514. table.GetRow(4).GetCell(0).SetParagraph(GetParagraph(table, true, "承办单位"));
  1515. table.GetRow(4).GetCell(1).SetParagraph(GetParagraph(table, false, "内黄县政府"));
  1516. table.GetRow(4).GetCell(2).SetParagraph(GetParagraph(table, true, "协办单位"));
  1517. table.GetRow(4).GetCell(3).SetParagraph(GetParagraph(table, false, "内黄县政府"));
  1518. //第六行
  1519. table.GetRow(5).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)2000;
  1520. table.GetRow(5).MergeCells(1, 7);//合并列
  1521. table.GetRow(5).GetCell(0).SetParagraph(GetParagraph(table, true, "调度意见"));
  1522. table.GetRow(5).GetCell(1).SetParagraph(GetParagraph(table, false, "请于1小时内与来电人取得联系(匿名除外),并于7个工作日内将办理情况反馈来电人,并将办理结果报市长便民公开电话受理中心,同时做好来电人信息保密工作。(若超期将进行通报;若无故不回复,将转交市委市政府督查局。)", false));
  1523. //第七行
  1524. table.GetRow(6).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1500;
  1525. table.GetRow(6).MergeCells(1, 7);//合并列
  1526. table.GetRow(6).GetCell(0).SetParagraph(GetParagraph(table, true, "承办意见"));
  1527. table.GetRow(6).GetCell(1).SetParagraph(GetParagraph(table, false, "请于1小时内与来电人取得联系(匿名除外),并于7个工作日内将办理情况反馈来电人,并将办理结果报市长便民公开电话受理中心,同时做好来电人信息保密工作。(若超期将进行通报;若无故不回复,将转交市委市政府督查局。)", false));
  1528. //第八行
  1529. table.GetRow(7).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1500;
  1530. table.GetRow(7).MergeCells(1, 7);//合并列
  1531. table.GetRow(7).GetCell(0).SetParagraph(GetParagraph(table, true, "领导批示"));
  1532. table.GetRow(7).GetCell(1).SetParagraph(GetParagraph(table, false, "请于1小时内与来电人取得联系(匿名除外),并于7个工作日内将办理情况反馈来电人,并将办理结果报市长便民公开电话受理中心,同时做好来电人信息保密工作。(若超期将进行通报;若无故不回复,将转交市委市政府督查局。)", false));
  1533. //第九行
  1534. table.GetRow(8).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)2000;
  1535. table.GetRow(8).MergeCells(1, 7);//合并列
  1536. table.GetRow(8).GetCell(0).SetParagraph(GetParagraph(table, true, "处理结果"));
  1537. table.GetRow(8).GetCell(1).SetParagraph(GetParagraph(table, false, "2021-04-12 10:19:14 内黄县政府部门接到市长电话来源工单(编号:DH9941052721041200351)反映土地确权问题,已联系郑州第三方中标土地确权公司负责人待工作人员到我县后,就可以确权 [办理结果:已办理,承办人:刘志强,职务:城关镇人大主席,联系电话:15824608111,刘志强向当事人进行了反馈,当事人对结果表示未评价。]", false));
  1538. document.CreateParagraph();
  1539. NPOI.XWPF.UserModel.XWPFParagraph paragraph1 = document.CreateParagraph();//创建段落对象
  1540. paragraph1.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;//文字显示位置,段落排列(左对齐,居中,右对齐)
  1541. NPOI.XWPF.UserModel.XWPFRun xwpfRun1 = paragraph1.CreateRun();//创建段落文本对象
  1542. xwpfRun1.IsBold = true;//文字加粗
  1543. xwpfRun1.SetText("联系电话:(0372) 12345审核人:8000 调度员:8065");//填充内容
  1544. xwpfRun1.FontSize = 9;//设置文字大小
  1545. xwpfRun1.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  1546. NPOI.XWPF.UserModel.XWPFParagraph paragraph2 = document.CreateParagraph();//创建段落对象
  1547. paragraph2.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.RIGHT;//文字显示位置,段落排列(左对齐,居中,右对齐)
  1548. NPOI.XWPF.UserModel.XWPFRun xwpfRun2 = paragraph2.CreateRun();//创建段落文本对象
  1549. using (FileStream picData = new FileStream(HttpContext.Current.Server.MapPath("/Upload/Word/word.png"), FileMode.Open, FileAccess.Read))
  1550. {
  1551. //图片的文件流 图片类型 图片名称 设置的宽度以及高度
  1552. xwpfRun2.AddPicture(picData, (int)PictureType.PNG, "word.png", NPOI.Util.Units.ToEMU(100), NPOI.Util.Units.ToEMU(100));
  1553. }
  1554. using (MemoryStream ms = new MemoryStream())
  1555. {
  1556. document.Write(ms);
  1557. return ms.GetBuffer();
  1558. }
  1559. }
  1560. catch(Exception ex)
  1561. {
  1562. return null;
  1563. }
  1564. }
  1565. /// <summary>
  1566. ///
  1567. /// </summary>
  1568. /// <param name="document"></param>
  1569. /// <param name="isbold"></param>
  1570. /// <param name="content"></param>
  1571. /// <returns></returns>
  1572. private NPOI.XWPF.UserModel.XWPFParagraph GetParagraph(NPOI.XWPF.UserModel.XWPFTable table,bool isbold,string content, bool iscenter=true)
  1573. {
  1574. var para = new CT_P();
  1575. //设置单元格文本对齐
  1576. para.AddNewPPr().AddNewTextAlignment();
  1577. NPOI.XWPF.UserModel.XWPFParagraph paragraph = new NPOI.XWPF.UserModel.XWPFParagraph(para,table.Body);
  1578. paragraph.VerticalAlignment = NPOI.XWPF.UserModel.TextAlignment.CENTER;
  1579. if (iscenter)
  1580. {
  1581. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;
  1582. }
  1583. else
  1584. {
  1585. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.LEFT;
  1586. }
  1587. NPOI.XWPF.UserModel.XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象
  1588. xwpfRun.IsBold = isbold;//文字加粗
  1589. xwpfRun.SetText(content);//填充内容
  1590. xwpfRun.FontSize = 9;//设置文字大小
  1591. xwpfRun.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  1592. return paragraph;
  1593. }
  1594. /// <summary>
  1595. /// 插入图片
  1596. /// </summary>
  1597. /// <param name="table"></param>
  1598. /// <returns></returns>
  1599. private NPOI.XWPF.UserModel.XWPFParagraph GetParagraphImg(NPOI.XWPF.UserModel.XWPFTable table, bool isbold, string content, bool iscenter = true)
  1600. {
  1601. var para = new CT_P();
  1602. //设置单元格文本对齐
  1603. para.AddNewPPr().AddNewTextAlignment();
  1604. NPOI.XWPF.UserModel.XWPFParagraph paragraph = new NPOI.XWPF.UserModel.XWPFParagraph(para, table.Body);
  1605. paragraph.VerticalAlignment = NPOI.XWPF.UserModel.TextAlignment.CENTER;
  1606. if (iscenter)
  1607. {
  1608. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.CENTER;
  1609. }
  1610. else
  1611. {
  1612. paragraph.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.LEFT;
  1613. }
  1614. NPOI.XWPF.UserModel.XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象
  1615. xwpfRun.IsBold = isbold;//文字加粗
  1616. xwpfRun.SetText(content);//填充内容
  1617. xwpfRun.FontSize = 9;//设置文字大小
  1618. xwpfRun.SetFontFamily("宋体", NPOI.XWPF.UserModel.FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
  1619. using (FileStream picData = new FileStream(HttpContext.Current.Server.MapPath("/Upload/Word/word.png"), FileMode.Open, FileAccess.Read))
  1620. {
  1621. //图片的文件流 图片类型 图片名称 设置的宽度以及高度
  1622. xwpfRun.AddPicture(picData, (int)PictureType.PNG, "word.png", NPOI.Util.Units.ToEMU(100), NPOI.Util.Units.ToEMU(100));
  1623. }
  1624. return paragraph;
  1625. }
  1626. }
  1627. }