开封利通水务后端

NPOIHelper.cs 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. 
  2. using System;
  3. using System.Data;
  4. using System.IO;
  5. using System.Text;
  6. using System.Web;
  7. using NPOI.HSSF.UserModel;
  8. using NPOI.SS.UserModel;
  9. using NPOI.SS.Util;
  10. using NPOI.XSSF.UserModel;
  11. using System.Collections.Generic;
  12. namespace CallCenter.Utility
  13. {
  14. public class NPOIHelper
  15. {
  16. private string _title;
  17. private string _sheetName;
  18. private string _filePath;
  19. /// <summary>
  20. /// 导出到Excel
  21. /// </summary>
  22. /// <param name="table"></param>
  23. /// <returns></returns>
  24. public bool ToExcel(DataTable table, string[] columns = null)
  25. {
  26. FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  27. IWorkbook workBook = new HSSFWorkbook();
  28. if (string.IsNullOrWhiteSpace(this._sheetName))
  29. {
  30. this._sheetName = "sheet1";
  31. }
  32. ISheet sheet = workBook.CreateSheet(this._sheetName);
  33. //处理表格标题
  34. IRow row = sheet.CreateRow(0);
  35. row.CreateCell(0).SetCellValue(this._title);
  36. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1));
  37. row.Height = 500;
  38. ICellStyle cellStyle = workBook.CreateCellStyle();
  39. IFont font = workBook.CreateFont();
  40. font.FontName = "微软雅黑";
  41. font.FontHeightInPoints = 17;
  42. cellStyle.SetFont(font);
  43. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  44. cellStyle.Alignment = HorizontalAlignment.Center;
  45. row.Cells[0].CellStyle = cellStyle;
  46. //处理表格列头
  47. row = sheet.CreateRow(1);
  48. if (columns == null)
  49. {
  50. for (int i = 0; i < table.Columns.Count; i++)
  51. {
  52. row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
  53. row.Height = 350;
  54. sheet.AutoSizeColumn(i);
  55. }
  56. }
  57. else
  58. {
  59. for (int i = 0; i < columns.Length; i++)
  60. {
  61. row.CreateCell(i).SetCellValue(columns[i]);
  62. row.Height = 350;
  63. sheet.AutoSizeColumn(i);
  64. }
  65. }
  66. //处理数据内容
  67. for (int i = 0; i < table.Rows.Count; i++)
  68. {
  69. row = sheet.CreateRow(2 + i);
  70. row.Height = 250;
  71. for (int j = 0; j < table.Columns.Count; j++)
  72. {
  73. row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
  74. sheet.SetColumnWidth(j, 256 * 15);
  75. }
  76. }
  77. //写入数据流
  78. workBook.Write(fs);
  79. fs.Flush();
  80. fs.Close();
  81. return true;
  82. }
  83. /// <summary>
  84. /// 导出到Excel
  85. /// </summary>
  86. /// <param name="table"></param>
  87. /// <param name="title"></param>
  88. /// <param name="sheetName">空字符串或null的话默认sheet1</param>
  89. /// <param name="columns">自定义表格列头,默认null</param>
  90. /// <returns></returns>
  91. public bool ToExcel(DataTable table, string title, string sheetName, string filePath, string[] columns = null)
  92. {
  93. this._title = title;
  94. this._sheetName = sheetName;
  95. this._filePath = filePath;
  96. return ToExcel(table, columns);
  97. }
  98. /// <summary>
  99. /// 弹出下载框导出excel
  100. /// </summary>
  101. /// <param name="Name"></param>
  102. /// <param name="dt"></param>
  103. /// <returns></returns>
  104. public string ExportToExcel(string Name, DataTable dt, string[] cols = null)
  105. {
  106. try
  107. {
  108. //if (dt.Rows.Count > 0)
  109. //{
  110. HSSFWorkbook workbook = new HSSFWorkbook();
  111. ISheet sheet = workbook.CreateSheet("Sheet1");
  112. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  113. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  114. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  115. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  116. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  117. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  118. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  119. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  120. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  121. //字体
  122. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  123. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  124. headerfont.FontHeightInPoints = 12;
  125. HeadercellStyle.SetFont(headerfont);
  126. //用column name 作为列名
  127. int icolIndex = 0;
  128. IRow headerRow = sheet.CreateRow(0);
  129. if (cols == null || (cols != null && cols.Length == 0))
  130. {
  131. foreach (DataColumn dc in dt.Columns)
  132. {
  133. ICell cell = headerRow.CreateCell(icolIndex);
  134. cell.SetCellValue(dc.ColumnName);
  135. cell.CellStyle = HeadercellStyle;
  136. icolIndex++;
  137. }
  138. }
  139. else
  140. {
  141. foreach (string dc in cols)
  142. {
  143. ICell cell = headerRow.CreateCell(icolIndex);
  144. cell.SetCellValue(dc);
  145. cell.CellStyle = HeadercellStyle;
  146. icolIndex++;
  147. }
  148. }
  149. ICellStyle cellStyle = workbook.CreateCellStyle();
  150. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  151. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  152. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  153. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  154. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  155. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  156. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  157. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  158. cellStyle.SetFont(cellfont);
  159. //建立内容行
  160. int iRowIndex = 0;
  161. foreach (DataRow dr in dt.Rows)
  162. {
  163. int iCellIndex = 0;
  164. IRow irow = sheet.CreateRow(iRowIndex + 1);
  165. for (int i = 0; i < dt.Columns.Count; i++)
  166. {
  167. string strsj = string.Empty;
  168. if (dr[i] != null)
  169. {
  170. strsj = dr[i].ToString();
  171. }
  172. ICell cell = irow.CreateCell(iCellIndex);
  173. cell.SetCellValue(strsj);
  174. cell.CellStyle = cellStyle;
  175. iCellIndex++;
  176. }
  177. iRowIndex++;
  178. }
  179. //自适应列宽度
  180. for (int i = 0; i < icolIndex; i++)
  181. {
  182. sheet.AutoSizeColumn(i);
  183. }
  184. using (MemoryStream ms = new MemoryStream())
  185. {
  186. workbook.Write(ms);
  187. HttpContext curContext = HttpContext.Current;
  188. // 设置编码和附件格式
  189. curContext.Response.ContentType = "application/vnd.ms-excel";
  190. curContext.Response.ContentEncoding = Encoding.UTF8;
  191. curContext.Response.Charset = "";
  192. curContext.Response.AppendHeader("Content-Disposition",
  193. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  194. curContext.Response.BinaryWrite(ms.GetBuffer());
  195. workbook = null;
  196. ms.Close();
  197. ms.Dispose();
  198. curContext.Response.End();
  199. }
  200. //}
  201. return "";
  202. }
  203. catch
  204. {
  205. return "导出失败!";
  206. }
  207. }
  208. /// <summary>
  209. /// 合并单元格
  210. /// </summary>
  211. /// <param name="sheet">要合并单元格所在的sheet</param>
  212. /// <param name="rowstart">开始行的索引</param>
  213. /// <param name="rowend">结束行的索引</param>
  214. /// <param name="colstart">开始列的索引</param>
  215. /// <param name="colend">结束列的索引</param>
  216. public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
  217. {
  218. CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
  219. sheet.AddMergedRegion(cellRangeAddress);
  220. }
  221. /// <summary>
  222. /// 工单类型弹出下载框导出excel
  223. /// </summary>
  224. /// <param name="Name"></param>
  225. /// <param name="dt"></param>
  226. /// <param name="typeclass">仪器或试剂</param>
  227. /// <returns></returns>
  228. public string ExportToExcelForGDLX(string Name, DataTable dt, string typeclass, List<string> colnames, List<int> erows, List<string> secolnames)
  229. {
  230. try
  231. {
  232. HSSFWorkbook workbook = new HSSFWorkbook();
  233. ISheet sheet = workbook.CreateSheet("Sheet1");
  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. for (int i = 0; i < colnames.Count; i++)
  252. {
  253. ICell cell = headerRow.CreateCell(i);
  254. cell.SetCellValue(colnames[i]);
  255. cell.CellStyle = HeadercellStyle;
  256. //SetCellRangeAddress(sheet, 0, 0, erows[i * 2], erows[i * 2 + 1]);
  257. }
  258. for (int k = 0; k < erows.Count / 2; k++)
  259. {
  260. SetCellRangeAddress(sheet, 0, 0, erows[k * 2], erows[k * 2 + 1]);
  261. }
  262. //添加第二行标题
  263. IRow SecRow = sheet.CreateRow(1);
  264. for (int i = 0; i < secolnames.Count; i++)
  265. {
  266. ICell cell = SecRow.CreateCell(i);
  267. cell.SetCellValue(secolnames[i].ToString());
  268. }
  269. ICellStyle cellStyle = workbook.CreateCellStyle();
  270. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  271. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  272. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  273. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  274. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  275. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  276. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  277. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  278. cellStyle.SetFont(cellfont);
  279. //建立内容行
  280. int iRowIndex = 0;
  281. foreach (DataRow dr in dt.Rows)
  282. {
  283. int iCellIndex = 0;
  284. IRow irow = sheet.CreateRow(iRowIndex + 2);
  285. for (int i = 0; i < dt.Columns.Count; i++)
  286. {
  287. string strsj = string.Empty;
  288. if (dr[i] != null)
  289. {
  290. strsj = dr[i].ToString();
  291. }
  292. ICell cell = irow.CreateCell(iCellIndex);
  293. cell.SetCellValue(strsj);
  294. cell.CellStyle = cellStyle;
  295. iCellIndex++;
  296. }
  297. iRowIndex++;
  298. }
  299. //自适应列宽度
  300. for (int i = 0; i < icolIndex; i++)
  301. {
  302. sheet.AutoSizeColumn(i);
  303. }
  304. using (MemoryStream ms = new MemoryStream())
  305. {
  306. workbook.Write(ms);
  307. HttpContext curContext = HttpContext.Current;
  308. // 设置编码和附件格式
  309. curContext.Response.ContentType = "application/vnd.ms-excel";
  310. curContext.Response.ContentEncoding = Encoding.UTF8;
  311. curContext.Response.Charset = "";
  312. curContext.Response.AppendHeader("Content-Disposition",
  313. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  314. curContext.Response.BinaryWrite(ms.GetBuffer());
  315. workbook = null;
  316. ms.Close();
  317. ms.Dispose();
  318. curContext.Response.End();
  319. }
  320. return "";
  321. }
  322. catch (Exception e)
  323. {
  324. return "导出失败!" + e.Message;
  325. }
  326. }
  327. /// <summary>
  328. /// 导入excel转换为datatable
  329. /// </summary>
  330. /// <param name="upfile"></param>
  331. /// <param name="headrow"></param>
  332. /// <returns></returns>
  333. public DataTable ExcelToTable(HttpPostedFile upfile, int headrow)
  334. {
  335. DataTable dt = new DataTable();
  336. IWorkbook workbook = null;
  337. Stream stream = upfile.InputStream;
  338. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  339. if (suffix == "xlsx") // 2007版本
  340. {
  341. workbook = new XSSFWorkbook(stream);
  342. }
  343. else if (suffix == "xls") // 2003版本
  344. {
  345. workbook = new HSSFWorkbook(stream);
  346. }
  347. //获取excel的第一个sheet
  348. ISheet sheet = workbook.GetSheetAt(0);
  349. //获取sheet的第一行
  350. IRow headerRow = sheet.GetRow(headrow);
  351. //一行最后一个方格的编号 即总的列数
  352. int cellCount = headerRow.LastCellNum;
  353. //最后一列的标号 即总的行数
  354. int rowCount = sheet.LastRowNum;
  355. //列名
  356. for (int i = 0; i < cellCount; i++)
  357. {
  358. dt.Columns.Add(headerRow.GetCell(i).ToString());
  359. }
  360. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  361. {
  362. DataRow dr = dt.NewRow();
  363. IRow row = sheet.GetRow(i);
  364. for (int j = row.FirstCellNum; j < cellCount; j++)
  365. {
  366. if (row.GetCell(j) != null)
  367. {
  368. dr[j] = row.GetCell(j).ToString();
  369. }
  370. }
  371. dt.Rows.Add(dr);
  372. }
  373. sheet = null;
  374. workbook = null;
  375. return dt;
  376. }
  377. /// <summary>
  378. /// 导入excel转换为datatable
  379. /// </summary>
  380. /// <param name="upfile"></param>
  381. /// <param name="headrow"></param>
  382. /// <returns></returns>
  383. public DataTable ExcelToTable(string fileName, int headrow)
  384. {
  385. DataTable dt = new DataTable();
  386. IWorkbook workbook = null;
  387. Stream stream = new FileStream(fileName, FileMode.OpenOrCreate);
  388. string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
  389. if (suffix == "xlsx") // 2007版本
  390. {
  391. workbook = new XSSFWorkbook(stream);
  392. }
  393. else if (suffix == "xls") // 2003版本
  394. {
  395. workbook = new HSSFWorkbook(stream);
  396. }
  397. //获取excel的第一个sheet
  398. ISheet sheet = workbook.GetSheetAt(0);
  399. //获取sheet的第一行
  400. IRow headerRow = sheet.GetRow(headrow);
  401. //一行最后一个方格的编号 即总的列数
  402. int cellCount = headerRow.LastCellNum;
  403. //最后一列的标号 即总的行数
  404. int rowCount = sheet.LastRowNum;
  405. //列名
  406. for (int i = 0; i < cellCount; i++)
  407. {
  408. dt.Columns.Add(headerRow.GetCell(i).ToString());
  409. }
  410. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  411. {
  412. DataRow dr = dt.NewRow();
  413. IRow row = sheet.GetRow(i);
  414. for (int j = row.FirstCellNum; j < cellCount; j++)
  415. {
  416. if (row.GetCell(j) != null)
  417. {
  418. dr[j] = row.GetCell(j).ToString();
  419. }
  420. }
  421. dt.Rows.Add(dr);
  422. }
  423. sheet = null;
  424. workbook = null;
  425. return dt;
  426. }
  427. #region 弹出下载框导出excel(数据第一行为标题)
  428. /// <summary>
  429. /// 弹出下载框导出excel(数据第一行为标题)
  430. /// </summary>
  431. /// <param name="Name"></param>
  432. /// <param name="dt"></param>
  433. /// <returns></returns>
  434. public string ExportToExcel2(string Name, DataTable dt, string[] cols = null)
  435. {
  436. try
  437. {
  438. //if (dt.Rows.Count > 0)
  439. //{
  440. HSSFWorkbook workbook = new HSSFWorkbook();
  441. ISheet sheet = workbook.CreateSheet("Sheet1");
  442. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  443. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  444. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  445. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  446. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  447. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  448. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  449. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  450. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  451. //字体
  452. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  453. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  454. headerfont.FontHeightInPoints = 12;
  455. HeadercellStyle.SetFont(headerfont);
  456. //用column name 作为列名
  457. int icolIndex = 0;
  458. IRow headerRow = sheet.CreateRow(0);
  459. if (cols == null || (cols != null && cols.Length == 0))
  460. {
  461. //foreach (DataColumn dc in dt.Columns)
  462. //{
  463. // ICell cell = headerRow.CreateCell(icolIndex);
  464. // cell.SetCellValue(dc.ColumnName);
  465. // cell.CellStyle = HeadercellStyle;
  466. // icolIndex++;
  467. //}
  468. for (int i = 0; i < dt.Columns.Count; i++)
  469. {
  470. ICell cell = headerRow.CreateCell(icolIndex);
  471. cell.SetCellValue(dt.Rows[0][i].ToString());
  472. cell.CellStyle = HeadercellStyle;
  473. icolIndex++;
  474. }
  475. }
  476. else
  477. {
  478. foreach (string dc in cols)
  479. {
  480. ICell cell = headerRow.CreateCell(icolIndex);
  481. cell.SetCellValue(dc);
  482. cell.CellStyle = HeadercellStyle;
  483. icolIndex++;
  484. }
  485. }
  486. ICellStyle cellStyle = workbook.CreateCellStyle();
  487. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  488. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  489. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  490. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  491. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  492. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  493. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  494. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  495. cellStyle.SetFont(cellfont);
  496. //建立内容行
  497. int iRowIndex = 0;
  498. foreach (DataRow dr in dt.Rows)
  499. {
  500. if (iRowIndex > 0)
  501. {
  502. int iCellIndex = 0;
  503. IRow irow = sheet.CreateRow(iRowIndex);
  504. for (int i = 0; i < dt.Columns.Count; i++)
  505. {
  506. string strsj = string.Empty;
  507. if (dr[i] != null)
  508. {
  509. strsj = dr[i].ToString();
  510. }
  511. ICell cell = irow.CreateCell(iCellIndex);
  512. cell.SetCellValue(strsj);
  513. cell.CellStyle = cellStyle;
  514. iCellIndex++;
  515. }
  516. }
  517. iRowIndex++;
  518. }
  519. //自适应列宽度
  520. for (int i = 0; i < icolIndex; i++)
  521. {
  522. sheet.AutoSizeColumn(i);
  523. }
  524. using (MemoryStream ms = new MemoryStream())
  525. {
  526. workbook.Write(ms);
  527. HttpContext curContext = HttpContext.Current;
  528. // 设置编码和附件格式
  529. curContext.Response.ContentType = "application/vnd.ms-excel";
  530. curContext.Response.ContentEncoding = Encoding.UTF8;
  531. curContext.Response.Charset = "";
  532. curContext.Response.AppendHeader("Content-Disposition",
  533. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  534. curContext.Response.BinaryWrite(ms.GetBuffer());
  535. workbook = null;
  536. ms.Close();
  537. ms.Dispose();
  538. curContext.Response.End();
  539. }
  540. //}
  541. return "";
  542. }
  543. catch
  544. {
  545. return "导出失败!";
  546. }
  547. }
  548. #endregion
  549. /// <summary>
  550. /// 导出工单详情
  551. /// </summary>
  552. /// <param name="Name"></param>
  553. /// <param name="dt"></param>
  554. /// <returns></returns>
  555. public string ExportWDToExcel(string Name, string id,string gdlx,string cjsj,string gdzt,string khxm,string khdh,string gdyxx,string gdly,string jsbm,string cjr,string fknr,string clnr)
  556. {
  557. try
  558. {
  559. //if (dt.Rows.Count > 0)
  560. //{
  561. HSSFWorkbook workbook = new HSSFWorkbook();
  562. ISheet sheet = workbook.CreateSheet("Sheet1");
  563. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  564. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  565. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  566. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  567. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  568. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  569. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  570. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  571. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  572. //字体
  573. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  574. headerfont.Boldweight = (short)FontBoldWeight.Normal ;
  575. headerfont.FontHeightInPoints = 12;
  576. HeadercellStyle.SetFont(headerfont);
  577. ICellStyle cellStyle = workbook.CreateCellStyle();
  578. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  579. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  580. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  581. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  582. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  583. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  584. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  585. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  586. cellStyle.SetFont(cellfont);
  587. IRow irow = sheet.CreateRow(0);
  588. ICell cell = irow.CreateCell(0);
  589. cell.SetCellValue("工单编号");
  590. cell.CellStyle = HeadercellStyle;
  591. ICell cell1 = irow.CreateCell(1);
  592. cell1.SetCellValue(id);
  593. cell1.CellStyle = cellStyle;
  594. ICell cell2 = irow.CreateCell(2);
  595. cell2.SetCellValue("工单类型");
  596. cell2.CellStyle = HeadercellStyle;
  597. ICell cell3 = irow.CreateCell(3);
  598. cell3.SetCellValue(gdlx );
  599. cell3.CellStyle = cellStyle;
  600. ICell cell4 = irow.CreateCell(4);
  601. cell4.SetCellValue("创建时间");
  602. cell4.CellStyle = HeadercellStyle;
  603. ICell cell5 = irow.CreateCell(5);
  604. cell5.SetCellValue(cjsj );
  605. cell5.CellStyle = cellStyle;
  606. IRow irow1 = sheet.CreateRow(1);
  607. ICell cell20 = irow1.CreateCell(0);
  608. cell20.SetCellValue("工单状态");
  609. cell20.CellStyle = HeadercellStyle;
  610. ICell cell21 = irow1.CreateCell(1);
  611. cell21.SetCellValue(gdzt );
  612. cell21.CellStyle = cellStyle;
  613. ICell cell22 = irow1.CreateCell(2);
  614. cell22.SetCellValue("客户姓名");
  615. cell22.CellStyle = HeadercellStyle;
  616. ICell cell23 = irow1.CreateCell(3);
  617. cell23.SetCellValue(khxm );
  618. cell23.CellStyle = cellStyle;
  619. ICell cell24 = irow1.CreateCell(4);
  620. cell24.SetCellValue("客户电话");
  621. cell24.CellStyle = HeadercellStyle;
  622. ICell cell25 = irow1.CreateCell(5);
  623. cell25.SetCellValue(khdh );
  624. cell25.CellStyle = cellStyle;
  625. IRow irow2 = sheet.CreateRow(2);
  626. ICell cell30 = irow2.CreateCell(0);
  627. cell30.SetCellValue("工单有效性");
  628. cell30.CellStyle = HeadercellStyle;
  629. ICell cell31 = irow2.CreateCell(1);
  630. cell31.SetCellValue(gdyxx);
  631. cell31.CellStyle = cellStyle;
  632. ICell cell32 = irow2.CreateCell(2);
  633. cell32.SetCellValue("工单来源");
  634. cell32.CellStyle = HeadercellStyle;
  635. ICell cell33 = irow2.CreateCell(3);
  636. cell33.SetCellValue(gdly );
  637. cell33.CellStyle = cellStyle;
  638. ICell cell34 = irow2.CreateCell(4);
  639. cell34.SetCellValue("接收部门");
  640. cell34.CellStyle = HeadercellStyle;
  641. ICell cell35 = irow2.CreateCell(5);
  642. cell35.SetCellValue(jsbm );
  643. cell35.CellStyle = cellStyle;
  644. IRow irow3 = sheet.CreateRow(3);
  645. ICell cell40 = irow3.CreateCell(0);
  646. cell40.SetCellValue("创建人");
  647. cell40.CellStyle = HeadercellStyle;
  648. ICell cell41 = irow3.CreateCell(1);
  649. cell41.SetCellValue(cjr);
  650. cell41.CellStyle = cellStyle;
  651. ICell cell42 = irow3.CreateCell(2);
  652. cell42.SetCellValue("");
  653. cell42.CellStyle = HeadercellStyle;
  654. ICell cell43 = irow3.CreateCell(3);
  655. cell43.CellStyle = cellStyle;
  656. ICell cell44 = irow3.CreateCell(4);
  657. cell44.SetCellValue("");
  658. cell44.CellStyle = HeadercellStyle;
  659. ICell cell45 = irow3.CreateCell(5);
  660. cell45.CellStyle = cellStyle;
  661. IRow irow4 = sheet.CreateRow(4);
  662. ICell cell51 = irow4.CreateCell(0);
  663. cell51.SetCellValue("反馈内容");
  664. cell51.CellStyle = HeadercellStyle;
  665. ICell cell52 = irow4.CreateCell(1);
  666. cell52.SetCellValue(fknr );
  667. cell52.CellStyle = cellStyle;
  668. ICell cell53 = irow4.CreateCell(2);
  669. cell53.SetCellValue("");
  670. cell53.CellStyle = HeadercellStyle;
  671. ICell cell54 = irow4.CreateCell(3);
  672. cell54.CellStyle = cellStyle;
  673. ICell cell55 = irow4.CreateCell(4);
  674. cell55.SetCellValue("");
  675. cell55.CellStyle = HeadercellStyle;
  676. ICell cell56 = irow4.CreateCell(5);
  677. cell56.CellStyle = cellStyle;
  678. IRow irow5 = sheet.CreateRow(5);
  679. ICell cell61 = irow5.CreateCell(0);
  680. cell61.SetCellValue("处理内容");
  681. cell61.CellStyle = HeadercellStyle;
  682. ICell cell62 = irow5.CreateCell(1);
  683. cell62.SetCellValue(clnr );
  684. cell62.CellStyle = cellStyle;
  685. ICell cell63 = irow5.CreateCell(2);
  686. cell63.SetCellValue("");
  687. cell63.CellStyle = HeadercellStyle;
  688. ICell cell64 = irow5.CreateCell(3);
  689. cell64.CellStyle = cellStyle;
  690. ICell cell65 = irow5.CreateCell(4);
  691. cell65.SetCellValue("");
  692. cell65.CellStyle = HeadercellStyle;
  693. ICell cell66 = irow5.CreateCell(5);
  694. cell66.CellStyle = cellStyle;
  695. //自适应列宽度
  696. for (int i = 0; i < 6; i++)
  697. {
  698. sheet.AutoSizeColumn(i);
  699. }
  700. sheet.AddMergedRegion(new CellRangeAddress(3, 3, 1, 5));//第一列前2行合并
  701. sheet.AddMergedRegion(new CellRangeAddress(4, 4, 1, 5));//第一列前2行合并
  702. sheet.AddMergedRegion(new CellRangeAddress(5, 5, 1, 5));//第一列前2行合并
  703. using (MemoryStream ms = new MemoryStream())
  704. {
  705. workbook.Write(ms);
  706. HttpContext curContext = HttpContext.Current;
  707. // 设置编码和附件格式
  708. curContext.Response.ContentType = "application/vnd.ms-excel";
  709. curContext.Response.ContentEncoding = Encoding.UTF8;
  710. curContext.Response.Charset = "";
  711. curContext.Response.AppendHeader("Content-Disposition",
  712. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  713. curContext.Response.BinaryWrite(ms.GetBuffer());
  714. workbook = null;
  715. ms.Close();
  716. ms.Dispose();
  717. curContext.Response.End();
  718. }
  719. //}
  720. return "";
  721. }
  722. catch
  723. {
  724. return "导出失败!";
  725. }
  726. }
  727. }
  728. }