.NetCore 导出Execl

/* 

Nuget  - NPOI.2.5.1

*/

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System; 
using System.Data;
using System.IO;

//-----------------------------------------------------------------
///dt 数据列表 ///sheetName 标签名 ///excelpath 保存路径 例:C:/WORK/XXXX.xls public static void ExportExcel(DataTable dt, string sheetName, string excelpath) { try { IWorkbook workbook; if (excelpath.Contains(".xlsx")) workbook = new XSSFWorkbook(); else if (excelpath.Contains(".xls")) workbook = new HSSFWorkbook(); else workbook = null; ISheet sheet = null; int headRowIndex = 0; if (!string.IsNullOrEmpty(dt.TableName)) { sheetName = dt.TableName; } sheet = workbook.CreateSheet(sheetName); int rowIndex = 0; #region 列头及样式 { IRow headerRow = sheet.CreateRow(headRowIndex); ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.Alignment = HorizontalAlignment.Center; IFont font = workbook.CreateFont(); font.FontHeightInPoints = 10; font.Boldweight = 700; headStyle.SetFont(font); foreach (DataColumn column in dt.Columns) { headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); headerRow.GetCell(column.Ordinal).CellStyle = headStyle; } } #endregion #region 填充内容 foreach (DataRow row in dt.Rows) { rowIndex++; IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in dt.Columns) { string drValue = row[column].ToString(); dataRow.CreateCell(column.Ordinal).SetCellValue(drValue); } } #endregion using (FileStream file = new FileStream(excelpath, FileMode.Create)) { workbook.Write(file); } return; } catch (Exception ex) { //log4.logtofile('ImportExcel',ex.Message); } }

  

原文地址:https://www.cnblogs.com/Zingu/p/14046370.html