[转载]使用NPOI将数据集导出到Excel

news/2024/7/21 4:55:49 标签: NPOI, excel

NPOI2.2.0.0版本下载地址: https://github.com/dotnetcore/NPOI

引用:NPOI.dll

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

namespace Demo.Code.Excel
{
    public class NPOIExcel
    {
        private string _title;
        private string _sheetName;
        private string _filePath;

        /// <summary>
        /// 导出到Excel
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public bool ToExcel(DataTable table)
        {
            FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            IWorkbook workBook = new HSSFWorkbook();
            this._sheetName = this._sheetName.IsEmpty() ? "sheet1" : this._sheetName;
            ISheet sheet = workBook.CreateSheet(this._sheetName);

            //处理表格标题
            IRow row = sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue(this._title);
            sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1));
            row.Height = 500;

            ICellStyle cellStyle = workBook.CreateCellStyle();
            IFont font = workBook.CreateFont();
            font.FontName = "微软雅黑";
            font.FontHeightInPoints = 17;
            cellStyle.SetFont(font);
            cellStyle.VerticalAlignment = VerticalAlignment.Center;
            cellStyle.Alignment = HorizontalAlignment.Center;
            row.Cells[0].CellStyle = cellStyle;

            //处理表格列头
            row = sheet.CreateRow(1);
            for (int i = 0; i < table.Columns.Count; i++)
            {
                row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
                row.Height = 350;
                sheet.AutoSizeColumn(i);
            }

            //处理数据内容
            for (int i = 0; i < table.Rows.Count; i++)
            {
                row = sheet.CreateRow(2 + i);
                row.Height = 250;
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
                    sheet.SetColumnWidth(j, 256 * 15);
                }
            }

            //写入数据流
            workBook.Write(fs);
            fs.Flush();
            fs.Close();

            return true;
        }

        /// <summary>
        /// 导出到Excel
        /// </summary>
        /// <param name="table"></param>
        /// <param name="title"></param>
        /// <param name="sheetName"></param>
        /// <returns></returns>
        public bool ToExcel(DataTable table, string title, string sheetName, string filePath)
        {
            this._title = title;
            this._sheetName = sheetName;
            this._filePath = filePath;
            return ToExcel(table);
        }
    }
}


http://www.niftyadmin.cn/n/837044.html

相关文章

c++编程思想

当我们需要处理复杂程序的时候,不应该马上就进入到繁琐的细节中去,而是应该先从总体上用易于理解的语言描述程序的目的,把程序分解为几个大的功能模块,然后逐步细化每个模块,也就是说,把一个问题分解为几个子问题。这个细化过程可能需要多次,直至用C或者c等语言把每个小的功能模…

TOSCA自动化测试工具--openURL

在folder下面create test case 输入自己的url&#xff0c;actionMode 是input, String类型 转载于:https://www.cnblogs.com/baxianhua/p/9563336.html

数据仓库之ETL实战

ETL&#xff0c;Extraction-Transformation-Loading的缩写&#xff0c;中文名称为数据抽取、转换和加载。一般随着业务的发展扩张&#xff0c;产线也越来越多&#xff0c;产生的数据也越来越多&#xff0c;这些数据的收集方式、原始数据格式、数据量、存储要求、使用场景等方面…

STORM启动与部署TOPOLOGY

文章来源&#xff1a;http://www.blogjava.net/paulwong/archive/2013/09/11/403942.html STORM启动与部署TOPOLOGY 启动ZOOPKEEPER zkServer.sh start启动NIMBUS storm nimbus &启动SUPERVISOR storm supervisor &启动UI storm ui &部署TOPOLOGY storm jar /opt/…

区分SDK、MFC、API、DLL、句柄

API是Application Programming Interface的缩写: 在Windows编程的前提下,就是特指的Windows API,是应用程序与windows系统打交道的最底层接口,平时人们常说的“用SDK写程序”就是指用Windows的API函数来写程序,API函数集由上千个API函数组成。任何一个程序,不管它是用MFC,…

smart contract 知识点

知识点 memory vs storage vs stack storage , where all the contract state variables reside. Every contract has its own storage and it is persistent between function calls and quite expensive to use.memory , this is used to hold temporary values. It is erase…

apolloxlua官网

2019独角兽企业重金招聘Python工程师标准>>> apolloxlua 官网入口 转载于:https://my.oschina.net/littlemonkeyc/blog/1939646

java使用环信信息推送,环信推送详解

一. 离线推送如果app集成时添加- (void)applicationDidEnterBackground:(UIApplication *)application {[[EMClient sharedClient] applicationDidEnterBackground:application];}- (void)applicationWillEnterForeground:(UIApplication *)application {[[EMClient sharedClie…