博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.Net 使用NPOI 实现Excel的简单导入导出 - Ran0 - 博客园
阅读量:4920 次
发布时间:2019-06-11

本文共 1740 字,大约阅读时间需要 5 分钟。

//创建不同版本的文件, excel2003版 或2007+版
 
public IWorkbook BuildWorkbook(DataTable dt, string file)
    {
        IWorkbook book;
        string fileExt = Path.GetExtension(file).ToLower();
        if (fileExt == ".xlsx")
        { book = new XSSFWorkbook(); }
        else if (fileExt == ".xls")
        { book = new HSSFWorkbook(); }
        else { book = null; }
        //var book = new HSSFWorkbook();
        ISheet sheet1 = book.CreateSheet("Sheet1");
        ISheet sheet2 = book.CreateSheet("Sheet2");
        //填充数据
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (i < 65536)
            {
                IRow drow = sheet1.CreateRow(i);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    ICell cell = drow.CreateCell(j, CellType.String);
                    cell.SetCellValue(dt.Rows[i][j].ToString());
                }
            }
            if (i >= 65536) //再创建一个sheet
            {
                IRow drow = sheet2.CreateRow(i - 65536);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    ICell cell = drow.CreateCell(j, CellType.String);
                    cell.SetCellValue(dt.Rows[i][j].ToString());
                }
            }
        }
        //自动列宽
        for (int i = 0; i <= dt.Columns.Count; i++)
        {
            sheet1.AutoSizeColumn(i, true);
            sheet2.AutoSizeColumn(i, true);
        }
        return book;
    }
 
//导出至excel文件
public void ExportExcel(DataTable dt, string fileName = "")
    {
        生成Excel
        IWorkbook book = BuildWorkbook(dt, fileName);
       
       //web 下载
        if (fileName == "")
            fileName = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now) + ".xls";
 
        fileName = fileName.Trim();
        HttpResponse httpResponse = HttpContext.Current.Response;
        httpResponse.Clear();
        httpResponse.Buffer = true;
        httpResponse.Charset = Encoding.UTF8.BodyName;
        httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
        httpResponse.ContentEncoding = Encoding.UTF8;
        httpResponse.ContentType = "application/vnd.ms-excel; charset=UTF-8";
        book.Write(httpResponse.OutputStream);
        httpResponse.End(); 
    }
 
 
 
 
 
 

转载于:https://www.cnblogs.com/sophy87/p/9268490.html

你可能感兴趣的文章
【一题多解】Python 字符串逆序
查看>>
open ball、closed ball 与 open set、closed set(interior point,limit point)、dense set
查看>>
字典(dictionary)与映射(map)
查看>>
Python 编程规范 —— TODO 注释(结合 PyCharm)
查看>>
十万个为什么 —— 名词解释(体育)
查看>>
table的设置(w3c)
查看>>
冲刺一
查看>>
【练习】在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b...
查看>>
python解决上楼梯问题
查看>>
变参宏 __VA_ARGS__
查看>>
sql 语句
查看>>
VUE一 基础语法
查看>>
[MySQl]MySQL忘记密码
查看>>
Android的minSdkVersion,targetSdkVersion,maxSdkVersion
查看>>
Xceed WinForm数据表格控件Xceed Grid For .NET控件详细介绍及下载地址
查看>>
ecos启动流程分析
查看>>
Oracle CASE WHEN 用法介绍
查看>>
linux 下连接mysql服务器
查看>>
DOMContentLoad 首屏渲染
查看>>
rpm检验是否被改动过
查看>>