足力健后端,使用.netcore版本,合并1个项目使用

NPOIHelper.cs 29KB

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