• 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_CODEPGIS_114ChildrenPageFolderTest_Pageabcdefg.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();
    
        }
    }
  • 相关阅读:
    js秒换成天时分
    vuex
    匹配iPhoneX
    ASP 解析json
    WPF 实现多语言支持
    c# 用DotNetZip来解压/压缩文件
    WPF UI Close button
    VB 获取所有窗体菜单信息
    ASP/ASP.NET/VB6文件上传
    c# 上传图片到一个外链相册服务器
  • 原文地址:https://www.cnblogs.com/loushuibazi/p/4927564.html
Copyright © 2020-2023  润新知