----------------------------------------------------------
- .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\test.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);
}