NPOI产生难以理解的内容

时间:2017-02-06 10:56:09

标签: excel apache-poi npoi

我正在使用NPOI打开现有的Excel文件,进行修改并将其写回磁盘。

但是,当我用NPOI打开文件并将其写回时,文件就会损坏。 Excel抱怨文件"包含不可读的内容",并询问我是否要"恢复内容"的文件。当我选择OK时,它会说:

Excel cannot open the file 'test.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

这是我的代码:

        var excelFilename = "c:\\temp\\simplefile.xlsx";
        IWorkbook wb;
        using (var fs = File.OpenRead(excelFilename))
        {
            wb = new XSSFWorkbook(fs);
        }
        var sheet = wb.GetSheetAt(0);
        // For this sample, we don't make any modifications to the file.
        // Just opening and writing it back is enough to produce this error.
        using (var str = new FileStream(excelFilename, FileMode.Open, FileAccess.ReadWrite))
        {
            wb.Write(str);
        }

" simplefile.xlsx"是Excel 2010创建的空Excel工作簿。 这里发生了什么?

0 个答案:

没有答案