颐和api

NPOIHelper.cs 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Web;
  8. using Microsoft.AspNetCore.Http;
  9. using NPOI.HSSF.UserModel;
  10. using NPOI.SS.UserModel;
  11. using NPOI.SS.Util;
  12. using NPOI.XSSF.UserModel;
  13. using System.Runtime.Serialization;
  14. using System.Runtime.Serialization.Formatters.Binary;
  15. using Newtonsoft.Json.Linq;
  16. using Newtonsoft.Json;
  17. namespace MadRunFabric.Common
  18. {
  19. public class NPOIHelper
  20. {
  21. private string _title;
  22. private string _sheetName;
  23. private string _filePath;
  24. /// <summary>
  25. /// 导出到Excel
  26. /// </summary>
  27. /// <param name="table"></param>
  28. /// <returns></returns>
  29. public bool ToExcel(DataTable table, string[] columns = null)
  30. {
  31. FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  32. IWorkbook workBook = new HSSFWorkbook();
  33. if (string.IsNullOrWhiteSpace(this._sheetName))
  34. {
  35. this._sheetName = "sheet1";
  36. }
  37. ISheet sheet = workBook.CreateSheet(this._sheetName);
  38. //处理表格标题
  39. IRow row = sheet.CreateRow(0);
  40. row.CreateCell(0).SetCellValue(this._title);
  41. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1));
  42. row.Height = 500;
  43. ICellStyle cellStyle = workBook.CreateCellStyle();
  44. IFont font = workBook.CreateFont();
  45. font.FontName = "微软雅黑";
  46. font.FontHeightInPoints = 17;
  47. cellStyle.SetFont(font);
  48. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  49. cellStyle.Alignment = HorizontalAlignment.Center;
  50. row.Cells[0].CellStyle = cellStyle;
  51. //处理表格列头
  52. row = sheet.CreateRow(1);
  53. if (columns == null)
  54. {
  55. for (int i = 0; i < table.Columns.Count; i++)
  56. {
  57. row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
  58. row.Height = 350;
  59. sheet.AutoSizeColumn(i);
  60. }
  61. }
  62. else
  63. {
  64. for (int i = 0; i < columns.Length; i++)
  65. {
  66. row.CreateCell(i).SetCellValue(columns[i]);
  67. row.Height = 350;
  68. sheet.AutoSizeColumn(i);
  69. }
  70. }
  71. //处理数据内容
  72. for (int i = 0; i < table.Rows.Count; i++)
  73. {
  74. row = sheet.CreateRow(2 + i);
  75. row.Height = 250;
  76. for (int j = 0; j < table.Columns.Count; j++)
  77. {
  78. row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
  79. sheet.SetColumnWidth(j, 256 * 15);
  80. }
  81. }
  82. //写入数据流
  83. workBook.Write(fs);
  84. fs.Flush();
  85. fs.Close();
  86. return true;
  87. }
  88. /// <summary>
  89. /// 导出到Excel
  90. /// </summary>
  91. /// <param name="table"></param>
  92. /// <param name="title"></param>
  93. /// <param name="sheetName">空字符串或null的话默认sheet1</param>
  94. /// <param name="columns">自定义表格列头,默认null</param>
  95. /// <returns></returns>
  96. public bool ToExcel(DataTable table, string title, string sheetName, string filePath, string[] columns = null)
  97. {
  98. this._title = title;
  99. this._sheetName = sheetName;
  100. this._filePath = filePath;
  101. return ToExcel(table, columns);
  102. }
  103. /// <summary>
  104. /// 弹出下载框导出excel
  105. /// </summary>
  106. /// <param name="Name"></param>
  107. /// <param name="dt"></param>
  108. /// <returns></returns>
  109. /*public string ExportToExcel(string Name, DataTable dt, string[] cols = null)
  110. {
  111. try
  112. {
  113. //if (dt.Rows.Count > 0)
  114. //{
  115. //HSSFWorkbook workbook = new HSSFWorkbook();
  116. IWorkbook workbook = new XSSFWorkbook();
  117. ISheet sheet = workbook.CreateSheet("Sheet1");
  118. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  119. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  120. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  121. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  122. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  123. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  124. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  125. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  126. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  127. //字体
  128. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  129. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  130. headerfont.FontHeightInPoints = 12;
  131. HeadercellStyle.SetFont(headerfont);
  132. //用column name 作为列名
  133. int icolIndex = 0;
  134. IRow headerRow = sheet.CreateRow(0);
  135. if (cols == null || (cols != null && cols.Length == 0))
  136. {
  137. foreach (DataColumn dc in dt.Columns)
  138. {
  139. ICell cell = headerRow.CreateCell(icolIndex);
  140. cell.SetCellValue(dc.ColumnName);
  141. cell.CellStyle = HeadercellStyle;
  142. icolIndex++;
  143. }
  144. }
  145. else
  146. {
  147. foreach (string dc in cols)
  148. {
  149. ICell cell = headerRow.CreateCell(icolIndex);
  150. cell.SetCellValue(dc);
  151. cell.CellStyle = HeadercellStyle;
  152. icolIndex++;
  153. }
  154. }
  155. ICellStyle cellStyle = workbook.CreateCellStyle();
  156. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  157. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  158. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  159. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  160. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  161. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  162. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  163. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  164. cellStyle.SetFont(cellfont);
  165. //建立内容行
  166. int iRowIndex = 0;
  167. foreach (DataRow dr in dt.Rows)
  168. {
  169. int iCellIndex = 0;
  170. IRow irow = sheet.CreateRow(iRowIndex + 1);
  171. for (int i = 0; i < dt.Columns.Count; i++)
  172. {
  173. string strsj = string.Empty;
  174. if (dr[i] != null)
  175. {
  176. strsj = dr[i].ToString();
  177. }
  178. ICell cell = irow.CreateCell(iCellIndex);
  179. cell.SetCellValue(strsj);
  180. cell.CellStyle = cellStyle;
  181. iCellIndex++;
  182. }
  183. iRowIndex++;
  184. }
  185. //自适应列宽度
  186. //for (int i = 0; i < icolIndex; i++)
  187. //{
  188. // sheet.AutoSizeColumn(i);
  189. //}
  190. using (MemoryStream ms = new MemoryStream())
  191. {
  192. workbook.Write(ms);
  193. HttpContext curContext = HttpContext.Current;
  194. // 设置编码和附件格式
  195. curContext.Response.ContentType = "application/vnd.ms-excel";
  196. curContext.Response.ContentEncoding = Encoding.UTF8;
  197. curContext.Response.Charset = "";
  198. curContext.Response.AppendHeader("Content-Disposition",
  199. "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
  200. curContext.Response.BinaryWrite(ms.GetBuffer());
  201. workbook = null;
  202. ms.Close();
  203. ms.Dispose();
  204. curContext.Response.End();
  205. }
  206. //}
  207. return "";
  208. }
  209. catch
  210. {
  211. return "导出失败!";
  212. }
  213. }
  214. /// <summary>
  215. /// 导入excel转换为datatable
  216. /// </summary>
  217. /// <param name="upfile"></param>
  218. /// <param name="headrow"></param>
  219. /// <returns></returns>
  220. public DataTable ExcelToTable(HttpPostedFile upfile, int headrow)
  221. {
  222. DataTable dt = new DataTable();
  223. IWorkbook workbook = null;
  224. Stream stream = upfile.InputStream;
  225. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  226. if (suffix == "xlsx") // 2007版本
  227. {
  228. workbook = new XSSFWorkbook(stream);
  229. }
  230. else if (suffix == "xls") // 2003版本
  231. {
  232. workbook = new HSSFWorkbook(stream);
  233. }
  234. //获取excel的第一个sheet
  235. ISheet sheet = workbook.GetSheetAt(0);
  236. //获取sheet的第一行
  237. IRow headerRow = sheet.GetRow(headrow);
  238. //一行最后一个方格的编号 即总的列数
  239. int cellCount = headerRow.LastCellNum;
  240. //最后一列的标号 即总的行数
  241. int rowCount = sheet.LastRowNum;
  242. //列名
  243. for (int i = 0; i < cellCount; i++)
  244. {
  245. dt.Columns.Add(headerRow.GetCell(i).ToString());
  246. }
  247. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  248. {
  249. DataRow dr = dt.NewRow();
  250. IRow row = sheet.GetRow(i);
  251. for (int j = row.FirstCellNum; j < cellCount; j++)
  252. {
  253. if (row.GetCell(j) != null)
  254. {
  255. dr[j] = row.GetCell(j).ToString();
  256. }
  257. }
  258. dt.Rows.Add(dr);
  259. }
  260. sheet = null;
  261. workbook = null;
  262. return dt;
  263. }*/
  264. /// <summary>
  265. /// 导入excel转换为datatable
  266. /// </summary>
  267. /// <param name="upfile"></param>
  268. /// <param name="headrow"></param>
  269. /// <returns></returns>
  270. public DataTable ExcelToTable1(Microsoft.AspNetCore.Http.IFormFile upfile, int headrow)
  271. {
  272. DataTable dt = new DataTable();
  273. try
  274. {
  275. IWorkbook workbook = null;
  276. Stream stream = upfile.OpenReadStream();
  277. string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
  278. if (suffix == "xlsx") // 2007版本
  279. {
  280. workbook = new XSSFWorkbook(stream);
  281. }
  282. else if (suffix == "xls") // 2003版本
  283. {
  284. workbook = new HSSFWorkbook(stream);
  285. }
  286. //获取excel的第一个sheet
  287. ISheet sheet = workbook.GetSheetAt(0);
  288. //获取sheet的第一行
  289. IRow headerRow = sheet.GetRow(headrow);
  290. //一行最后一个方格的编号 即总的列数
  291. int cellCount = headerRow.LastCellNum;
  292. //最后一列的标号 即总的行数
  293. int rowCount = sheet.LastRowNum;
  294. //列名
  295. for (int i = 0; i < cellCount; i++)
  296. {
  297. dt.Columns.Add(headerRow.GetCell(i).ToString());
  298. }
  299. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  300. {
  301. DataRow dr = dt.NewRow();
  302. IRow row = sheet.GetRow(i);
  303. for (int j = row.FirstCellNum; j < cellCount; j++)
  304. {
  305. if (row.GetCell(j) != null)
  306. {
  307. dr[j] = row.GetCell(j).ToString();
  308. }
  309. }
  310. dt.Rows.Add(dr);
  311. }
  312. sheet = null;
  313. workbook = null;
  314. }
  315. catch (Exception ex)
  316. {
  317. //
  318. }
  319. return dt;
  320. }
  321. /// <summary>
  322. /// 导入excel转换为datatable
  323. /// </summary>
  324. /// <param name="upfile"></param>
  325. /// <param name="headrow"></param>
  326. /// <returns></returns>
  327. public DataTable ExcelToTable(string fileName, int headrow)
  328. {
  329. DataTable dt = new DataTable();
  330. IWorkbook workbook = null;
  331. Stream stream = new FileStream(fileName, FileMode.OpenOrCreate);
  332. string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
  333. if (suffix == "xlsx") // 2007版本
  334. {
  335. workbook = new XSSFWorkbook(stream);
  336. }
  337. else if (suffix == "xls") // 2003版本
  338. {
  339. workbook = new HSSFWorkbook(stream);
  340. }
  341. //获取excel的第一个sheet]
  342. ISheet sheet = workbook.GetSheetAt(0);
  343. //获取sheet的第一行
  344. IRow headerRow = sheet.GetRow(headrow);
  345. //一行最后一个方格的编号 即总的列数
  346. int cellCount = headerRow.LastCellNum;
  347. //最后一列的标号 即总的行数
  348. int rowCount = sheet.LastRowNum;
  349. //列名
  350. for (int i = 0; i < cellCount; i++)
  351. {
  352. dt.Columns.Add(headerRow.GetCell(i).ToString());
  353. }
  354. for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
  355. {
  356. DataRow dr = dt.NewRow();
  357. IRow row = sheet.GetRow(i);
  358. for (int j = row.FirstCellNum; j < cellCount; j++)
  359. {
  360. if (row.GetCell(j) != null)
  361. {
  362. dr[j] = row.GetCell(j).ToString();
  363. }
  364. }
  365. dt.Rows.Add(dr);
  366. }
  367. sheet = null;
  368. workbook = null;
  369. return dt;
  370. }
  371. /// <summary>
  372. /// 下载模板
  373. /// </summary>
  374. /// <param name="cols"></param>
  375. /// <returns></returns>
  376. public byte[] ExportToExcelTemplate(string[] cols)
  377. {
  378. IWorkbook workbook = new XSSFWorkbook();
  379. ISheet sheet = workbook.CreateSheet("Sheet1");
  380. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  381. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  382. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  383. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  384. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  385. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  386. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  387. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  388. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  389. //字体
  390. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  391. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  392. headerfont.FontHeightInPoints = 12;
  393. HeadercellStyle.SetFont(headerfont);
  394. ICellStyle cellStyle = workbook.CreateCellStyle();
  395. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  396. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  397. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  398. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  399. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  400. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  401. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  402. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  403. cellStyle.SetFont(cellfont);
  404. //用column name 作为列名
  405. int icolIndex = 0;
  406. IRow headerRow = sheet.CreateRow(0);
  407. foreach (string dc in cols)
  408. {
  409. ICell cell = headerRow.CreateCell(icolIndex);
  410. cell.SetCellValue(dc);
  411. cell.CellStyle = HeadercellStyle;
  412. sheet.SetDefaultColumnStyle(icolIndex, cellStyle);
  413. icolIndex++;
  414. }
  415. using (MemoryStream ms = new MemoryStream())
  416. {
  417. workbook.Write(ms);
  418. byte[] bs = ms.ToArray();
  419. workbook = null;
  420. ms.Close();
  421. ms.Dispose();
  422. return bs;
  423. }
  424. }
  425. /// <summary>
  426. /// 下载excel
  427. /// </summary>
  428. /// <param name="cols"></param>
  429. /// <returns></returns>
  430. public byte[] ExportToExcel(List<object> objs, string[] cols = null, string[] colname = null)
  431. {
  432. IWorkbook workbook = new XSSFWorkbook();
  433. ISheet sheet = workbook.CreateSheet("Sheet1");
  434. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  435. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  436. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  437. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  438. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  439. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  440. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  441. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  442. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  443. //字体
  444. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  445. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  446. headerfont.FontHeightInPoints = 12;
  447. HeadercellStyle.SetFont(headerfont);
  448. if (objs.Count > 0)
  449. {
  450. ICellStyle cellStyle = workbook.CreateCellStyle();
  451. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  452. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  453. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  454. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  455. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  456. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  457. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  458. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  459. cellStyle.SetFont(cellfont);
  460. int icolIndex = 0;
  461. IRow headerRow = sheet.CreateRow(0);
  462. if (cols == null)
  463. {
  464. //var pis = ((Newtonsoft.Json.Linq.JObject)objs[0]).GetType().GetProperties();
  465. var pis = objs[0].GetType().GetProperties();
  466. cols = new string[pis.Length];
  467. for (int i = 0; i < pis.Length; i++)
  468. {
  469. //cols[i] = pis[i].Name;
  470. cols[i] = pis[i].GetValue(objs[0],null).ToString();
  471. }
  472. // cols = new string[objs.Columns.Count];
  473. //for (int i = 0; i < objs.Columns.Count; i++)
  474. //{
  475. // cols[i] = objs.Columns[i].ColumnName;
  476. //}
  477. }
  478. foreach (string dc in cols)
  479. {
  480. ICell cell = headerRow.CreateCell(icolIndex);
  481. cell.SetCellValue(dc);
  482. cell.CellStyle = HeadercellStyle;
  483. sheet.SetDefaultColumnStyle(icolIndex, cellStyle);
  484. icolIndex++;
  485. }
  486. //建立内容行
  487. int iRowIndex = 0;
  488. //if (colname != null)
  489. //{
  490. // foreach (var item in colname)
  491. // {
  492. // for (int j = 0; j < objs.Columns.Count; j++)
  493. // {
  494. // if (objs.Columns[j].ColumnName == item)
  495. // {
  496. // string strsj = string.Empty;
  497. // if (objs.Rows[i][j] != null)
  498. // {
  499. // strsj = objs.Rows[i][j].ToString();
  500. // }
  501. // ICell cell = irow.CreateCell(iCellIndex);
  502. // cell.SetCellValue(strsj);
  503. // //cell.CellStyle = cellStyle;
  504. // iCellIndex++;
  505. // }
  506. // }
  507. // }
  508. //}
  509. //else
  510. //{
  511. foreach (var obj in objs)
  512. {
  513. PropertyInfo[] jobj = obj.GetType().GetProperties();
  514. var dd = obj.ToJson();
  515. //var jobj = (Newtonsoft.Json.Linq.JObject)obj;
  516. int iCellIndex = 0;
  517. IRow irow = sheet.CreateRow(iRowIndex + 1);
  518. for (int i = 0; i < cols.Length; i++)
  519. {
  520. string strsj = string.Empty;
  521. if (jobj[i].GetValue(obj, null) != null)
  522. {
  523. strsj = jobj[i].GetValue(obj, null).ToString();
  524. }
  525. ICell cell = irow.CreateCell(iCellIndex);
  526. cell.SetCellValue(strsj);
  527. //cell.CellStyle = cellStyle;
  528. iCellIndex++;
  529. }
  530. iRowIndex++;
  531. }
  532. //}
  533. }
  534. using (MemoryStream ms = new MemoryStream())
  535. {
  536. workbook.Write(ms);
  537. byte[] bs = ms.ToArray();
  538. workbook = null;
  539. ms.Close();
  540. ms.Dispose();
  541. return bs;
  542. }
  543. }
  544. /// <summary>
  545. /// 下载excel
  546. /// </summary>
  547. /// <param name="cols"></param>
  548. /// <returns></returns>
  549. public byte[] ExportToExcel1(DataTable objs, string[] cols = null, string[] colname = null)
  550. {
  551. IWorkbook workbook = new XSSFWorkbook();
  552. ISheet sheet = workbook.CreateSheet("Sheet1");
  553. ICellStyle HeadercellStyle = workbook.CreateCellStyle();
  554. HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  555. HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  556. HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  557. HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  558. HeadercellStyle.Alignment = HorizontalAlignment.Center;
  559. HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  560. HeadercellStyle.FillPattern = FillPattern.SolidForeground;
  561. HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
  562. //字体
  563. NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
  564. headerfont.Boldweight = (short)FontBoldWeight.Bold;
  565. headerfont.FontHeightInPoints = 12;
  566. HeadercellStyle.SetFont(headerfont);
  567. if (objs.Rows.Count > 0)
  568. {
  569. ICellStyle cellStyle = workbook.CreateCellStyle();
  570. //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
  571. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
  572. //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  573. //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  574. //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  575. //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  576. NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
  577. cellfont.Boldweight = (short)FontBoldWeight.Normal;
  578. cellStyle.SetFont(cellfont);
  579. int icolIndex = 0;
  580. IRow headerRow = sheet.CreateRow(0);
  581. if (cols == null)
  582. {
  583. cols = new string[objs.Columns.Count];
  584. for (int i = 0; i < objs.Columns.Count; i++)
  585. {
  586. cols[i] = objs.Columns[i].ColumnName;
  587. }
  588. }
  589. foreach (string dc in cols)
  590. {
  591. ICell cell = headerRow.CreateCell(icolIndex);
  592. cell.SetCellValue(dc);
  593. cell.CellStyle = HeadercellStyle;
  594. sheet.SetDefaultColumnStyle(icolIndex, cellStyle);
  595. icolIndex++;
  596. }
  597. //处理数据内容
  598. //建立内容行
  599. int iRowIndex = 0;
  600. for (int i = 0; i < objs.Rows.Count; i++)
  601. {
  602. //var jobj = (Newtonsoft.Json.Linq.JObject)obj;
  603. int iCellIndex = 0;
  604. IRow irow = sheet.CreateRow(iRowIndex + 1);
  605. if (colname != null)
  606. {
  607. foreach (var item in colname)
  608. {
  609. for (int j = 0; j < objs.Columns.Count; j++)
  610. {
  611. if (objs.Columns[j].ColumnName == item)
  612. {
  613. string strsj = string.Empty;
  614. if (objs.Rows[i][j] != null)
  615. {
  616. strsj = objs.Rows[i][j].ToString();
  617. }
  618. ICell cell = irow.CreateCell(iCellIndex);
  619. cell.SetCellValue(strsj);
  620. cell.CellStyle = cellStyle;
  621. iCellIndex++;
  622. }
  623. }
  624. }
  625. }
  626. else
  627. {
  628. for (int j = 0; j < objs.Columns.Count; j++)
  629. {
  630. //row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
  631. //sheet.SetColumnWidth(j, 256 * 15);
  632. string strsj = string.Empty;
  633. if (objs.Rows[i][j] != null)
  634. {
  635. strsj = objs.Rows[i][j].ToString();
  636. }
  637. ICell cell = irow.CreateCell(iCellIndex);
  638. cell.SetCellValue(strsj);
  639. cell.CellStyle = cellStyle;
  640. iCellIndex++;
  641. }
  642. }
  643. iRowIndex++;
  644. }
  645. }
  646. //自适应列宽度
  647. for (int i = 0; i < objs.Rows.Count; i++)
  648. {
  649. sheet.AutoSizeColumn(i);
  650. }
  651. using (MemoryStream ms = new MemoryStream())
  652. {
  653. workbook.Write(ms);
  654. byte[] bs = ms.ToArray();
  655. workbook = null;
  656. ms.Close();
  657. ms.Dispose();
  658. return bs;
  659. }
  660. }
  661. }
  662. }