NPOI使用记录

使用NPOI时,从官网下载的安装包,当时看里面有好几个DLL,觉得没啥用,就只导入了NPOI.dll

当使用的时候发现,还是需要全部导入进去,避免有一些东西引用不了,操作不了

其次,关于workbook的对象创建,如果是新版的,则用XSSF创建,若是xls的,则用HSSF来创建

另外还有一个情况,是NPOI的版本问题,版本不一样,IWorkbook的对象也不一样的,具体的忘记了。

 

另外,我使用的版本是NPOI 2.1.3

借此记录使用该动态库遇到的问题,避免下次再次遇到。

 

另外附上写的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;

public partial class ChildrenPageFolder_Test_Page_NPOI : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string fileName = @"F:\PGIS_CODE\PGIS_114\ChildrenPageFolder\Test_Page\abcdefg.xls";

        FileStream fs = File.OpenRead(fileName);
        // 此处可以判断文件的后缀名,判断调用不同的Workbook对象
        IWorkbook workbook = new HSSFWorkbook(fs);
        fs.Close();

        ISheet sheet = workbook.GetSheetAt(0);
        for (int i = 0; i <= sheet.LastRowNum; i++)
        {
            foreach (ICell cell in sheet.GetRow(i).Cells)
            {
                cell.SetCellType(CellType.String);
                cell.SetCellValue("0");
            }
        }

        // 将xls文件进行输出
        MemoryStream ms = new MemoryStream();
        workbook.Write(ms);
        Context.Response.ClearContent();
        Context.Response.ContentType = "application/octet-stream";
        Context.Response.AddHeader("Content-Disposition", "attachment;filename=abcdefg.xls");
        Context.Response.BinaryWrite(ms.ToArray());
        Context.Response.Flush();

        // 将xls文件保存到本地目录
        //FileStream fs2 = File.Create(@"D:\123.xls");
        //workbook.Write(fs2);
        //fs2.Close();

    }
}

 

转载于:https://www.cnblogs.com/loushuibazi/p/4927564.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值