NPOI如何写入XLSX Excel文件 [英] NPOI how to write to an XLSX excel file

查看:618
本文介绍了NPOI如何写入XLSX Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了NPOI库,但是我认为可以在这里回答.

代码:

解决方案

将流与XSSFWorkBook一起使用时,它会使用流并将其关闭,因此您不能将其用于编写. /p>

您可以为此使用其他流来写入输出

示例代码在这里.

 XSSFWorkbook wb1 = null;
使用(var file = new FileStream("D:\\ banding.xlsx",FileMode.Open,FileAccess.ReadWrite)){
wb1 =新的XSSFWorkbook(file);
}
wb1.GetSheetAt(0).GetRow(0).GetCell(0).SetCellValue("Sample");

使用(var file2 = new FileStream("D:\\ banding2.xlsx",FileMode.Create,FileAccess.ReadWrite)){
wb1.Write(file2);
file2.Close();
}


Hi,

I make use of the NPOI libs but it can be answered here I think.

Code:

               using (FileStream file = new FileStream(bestand, FileMode.Open, FileAccess.ReadWrite))
                {
                    wb1 = new XSSFWorkbook(file);
// Lots of code

}
wb1.close(file);

When writing I get the errormessage  on closing: Must support writing: BaseOutputStream

Fileacces = readwrite.

tryed Filemode OpenorCreate.

But no results, the same error messgae

Somebody? its is for an existing XLSX file


Full of ideas but don't know how. Coding for fun.

解决方案

One you use the Stream with the XSSFWorkBook, it uses the stream and closes it, so you cannot use it for writing.

You can use a different stream for this to write the output

A sample code is here.

XSSFWorkbook wb1 = null;
using (var file = new FileStream("D:\\banding.xlsx", FileMode.Open, FileAccess.ReadWrite)) {
	wb1 = new XSSFWorkbook(file);
}
wb1.GetSheetAt(0).GetRow(0).GetCell(0).SetCellValue("Sample");

using (var file2 = new FileStream("D:\\banding2.xlsx", FileMode.Create, FileAccess.ReadWrite)) {
	wb1.Write(file2);
	file2.Close();
}	


这篇关于NPOI如何写入XLSX Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆