c# - 使用 NPOI 编辑和保存 xlsx 会导致文件损坏

标签 c# excel xlsx npoi

FileStream s = new FileStream(@myPath, FileMode.Open, FileAccess.ReadWrite);
IWorkbook wb = new XSSFWorkbook(s);
s.Close();

ISheet isheet = wb.GetSheetAt(0);
IRow irow = isheet.CreateRow(0);
irow.CreateCell(0).SetCellValue("foo");

s = new FileStream(@myPath + "blah.xlsx", FileMode.Create);
wb.Write(s);
s.Close();

新文件已创建但已损坏。我看到有人说这在 2.0.6 版中已修复,但仍然不适合我。

最佳答案

看起来您在代码的第 1 行使用 @myPath 作为共享/文件名来创建流,但您将文件名连接到 @myPath 上以保存文件。这可能是错误的来源吗?

关于c# - 使用 NPOI 编辑和保存 xlsx 会导致文件损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23836885/

相关文章:

C# 命名约定(标题与名称)

excel - Excel 通过 Outlook 发送电子邮件时如何处理错误?

excel - 使用replace和find函数同时查找多个 "dot"值

python - 不能用 pandas read_excel

python - 我可以向写入单元格的公式添加字符吗?

java - 使用 Java 中的 Apache POI 将 16 位字符写入 .xlsx 文件

python - 如何创建没有任何样式的 XLSX 表格?

c# - 嵌套 Async Await 不等待

c# - 如果数组用作 struct (C#) 中的元素,它存储在哪里?

C# 从文件名中删除无效字符