zoukankan      html  css  js  c++  java
  • .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); } }

      

  • 相关阅读:
    git 镜像地址
    IntelliJ IDEA 2019 控制台中文乱码问题
    LINUX配置本地YUM源
    动态添加js的代码
    Java 多线程
    Java I/O系统
    Java 中的容器 Collection 和 Map
    Java 数组
    javaweb的四大作用域
    三层 转自http://git.oschina.net/tzhsweet/superui
  • 原文地址:https://www.cnblogs.com/Zingu/p/14046370.html
Copyright © 2011-2022 走看看