The new Excel file created by NOPI is always corrupted. When the file is opened, it says' We found a problem with some content in 'FileName.xlsx'.Do you want us to try to recover as much as we can? If you trust the source of this workbook, Click Yes" (发现FileName.xlsx 中部分内容有问题。是否让我们尽量尝试恢复?如果您信任次工作簿的源,请单击是)
We narrow down the condition, just use NPOI to open it and then save as a new file. The file will be corrupted.
We try the newest version 2.2.0, but in vain.
Comments: ** Comment from web user: Hellissimo **
Hi
try maybe this 2 small examples:
Reading example:
```
private IWorkbook InitializeIWorkbook(string path)
{
try
{
IWorkbook workbook = null;
using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
FileInfo fileInfo = new FileInfo(path);
if (fileInfo.Extension.ToLower() == ".xlsx" || fileInfo.Extension.ToLower() == ".xlsm")
{
// Excel 2007+
workbook = new XSSFWorkbook(file);
}
if (fileInfo.Extension.ToLower() == ".xls" || fileInfo.Extension.ToLower() == ".xlm")
{
// Excel 2003
workbook = new HSSFWorkbook(file);
}
}
return workbook;
}
catch (IOException e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
```
Writing example:
```
public void FileStreamToExcel()
{
try
{
// speichern
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
workbook.Write(fs);
}
}
catch (IOException e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
```
In the most cases it is a problem of FileStream...