zoukankan      html  css  js  c++  java
  • .net NPOI C#处理Excel的类库使用

    ----------------------------------------------------------
    - .net NPOI C#处理Excel的类库使用
    https://www.nuget.org/packages/NPOI/
    VS->工具->程序包管理器控制台 运行 PM> Install-Package NPOI -Version 2.3.0
    相关DLL引用自动添加到项目中,命名空间中添加如下声明:
    using NPOI.XSSF.UserModel;
    using NPOI.SS.UserModel;

    -------------------------------------------------------
    - 用nuget下载旧版本的软件包
    SharpZipLib 必须用0.86版本的,否则NPOI打开EXCEL时会出错。
    如果更新了SharpZipLib到1.0.0版本,必须roll back到0.86.0版,或者卸载重新安装NPOI。

    在Visual Studio中打开包管理器控制台 – 它在工具/ NuGet包管理器/包管理器控制台中。然后运行Install-Package命令:
    Install-Package ICSharpCode.SharpZipLib -Version 0.86.0.518 或者0.86.0

    编辑:
    为了列出包的版本,您可以使用带有远程参数和过滤器的Get-Package命令:

    Get-Package -ListAvailable -Filter Common.Logging -AllVersions
    通过在Install-Package命令中的版本选项后按Tab键,可以获得最新可用版本的列表。

    I also met this problem with VS2017, NPOI 2.3.0 and SharpZipLib 1.0.0.
    I didn't solve the problem changing the verison of the SharpZipLib to 0.86 in NuGet. After test, I found that it should also delete the below content in the App.config if it has that:

    <assemblyIdentity name="ICSharpCode.SharpZipLib" publicKeyToken="1b03e6acf1164f73" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-1.0.0.999" newVersion="1.0.0.999"/>
    So that, not only roll back the verison of the SharpZipLib to 0.86, but also modify the App.config.

    ------------------------------------------------------------
    Excel 用NPOI编辑写入后打不开,尝试以下解决:
    FileMode.Open is working with .xls files but not .xlsx files.
    using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
    {
    workbook.Write(file);
    }

    I think the problem is that you are reading from, and writing to, the same FileStream. You should be doing the read and write using separate streams. Try it like this:

    string newPath = @"C:MyPath est.xlsx";

    // read the workbook
    IWorkbook wb;
    using (FileStream fs = new FileStream(newPath, FileMode.Open, FileAccess.Read))
    {
    wb = new XSSFWorkbook(fs);
    }

    // make changes
    ISheet s = wb.GetSheetAt(0);
    IRow r = s.GetRow(0) ?? s.CreateRow(0);
    ICell c = r.GetCell(1) ?? r.CreateCell(1);
    c.SetCellValue("test2");

    // overwrite the workbook using a new stream
    using (FileStream fs = new FileStream(newPath, FileMode.Create, FileAccess.Write))
    {
    wb.Write(fs);
    }

  • 相关阅读:
    Kubernetes弹性伸缩全场景解读(五)
    阿里靠什么支撑 EB 级计算力?
    云原生生态周报 Vol. 2
    国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google
    GitOps:Kubernetes多集群环境下的高效CICD实践
    阿里开发者招聘节 | 面试题01:如何实现一个高效的单向链表逆序输出?
    noip2012 开车旅行
    noip2012 借教室
    noip2012 同余方程
    noip2012 国王游戏
  • 原文地址:https://www.cnblogs.com/CCJVL/p/10913208.html
Copyright © 2011-2022 走看看