zoukankan      html  css  js  c++  java
  • 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 Table排序类
    JavaScript使用技巧精萃
    修改鄒建 老師的SQL PivotTable,增加同分組非交叉欄位
    类似gmail添加附件
    [转贴]Js中 关于top、clientTop、scrollTop、offsetTop等
    Three Tier Code generation with Codesmith
    SQL中取得漢字拼音首字母或五筆首鍵編碼
    (转)ComputerStyle与currentStyle的区别
    html css样式色彩解析
    js 拖拽效果
  • 原文地址:https://www.cnblogs.com/loushuibazi/p/4927564.html
Copyright © 2011-2022 走看看