【发布时间】:2017-04-27 14:43:57
【问题描述】:
所以我能够打开并挖掘一个 xls (Excel 97-2003) 文件。但是,问题是当我尝试保存它时。在我保存它并成功后,我去手动打开 Excel 文件并得到一个错误,说它无法打开 Excel 文件并且它已损坏。无论我是否进行任何更改,都会发生这种情况。
我仍然能够在程序中打开 excel 文件并读取数据。
我正在使用 NPOI 2.2.1 和 2.3.0(我通过 Nuget 安装)。两个版本的结果相同。
string excelLocation = settings.GetExcelDirectory() + week.ExcelLocation;
HSSFWorkbook wbXLS;
// Try to open and read existing workbook
using (FileStream stream = new FileStream(excelLocation, FileMode.Open, FileAccess.Read))
{
wbXLS = new HSSFWorkbook(stream);
}
ISheet sheet = wbXLS.GetSheet("Schedule");
using (FileStream stream = new FileStream(excelLocation, FileMode.Create, FileAccess.Write))
{
wbXLS.Write(stream);
}
【问题讨论】:
-
您的代码在我看来是正确的,而且对我来说效果很好(当我打开文件时 Excel 不会抱怨)。
标签: c# excel xls excel-2003 npoi