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); } }

      

  • 相关阅读:
    开发环境之Webpack
    开发环境之Nginx
    Winfrom排坑
    Winfrom打包教程(Inno Setup工具)
    博客园美化教程
    开发环境之Tomcat
    IDEA编码时卡顿问题
    学习Spring5源码时所遇到的坑
    docker+jmeter+influx+granfana搭建性能测试监控平台
    Windows10上安装 WSL---Ubuntu
  • 原文地址:https://www.cnblogs.com/Zingu/p/14046370.html
Copyright © 2011-2022 走看看