[英]After saving xlsx file using NPOI, when I open xlsx file, it says file corrupt
xlWorksheet.GetRow(1).GetCell(2).SetCellValue("Hello");
using (FileStream file = new FileStream("Test.xlsx", FileMode.Create, FileAccess.Write))
{
XLWorkBook.Write(file);
file.Close();
}
I used this code to write excel file. 我用此代码编写了excel文件。 After writing the excel, when I open excel file manually it is corrupted.
写入excel后,当我手动打开excel文件时,它已损坏。 I am using NPOI binary 2.1.3.1 Please tell how to avoid excel getting corrupted.
我正在使用NPOI二进制文件2.1.3.1,请说明如何避免excel损坏。
Try adding file.Flush();
尝试添加
file.Flush();
before file.Close();
在
file.Close();
之前file.Close();
: :
xlWorksheet.GetRow(1).GetCell(2).SetCellValue("Hello");
using (FileStream file = new FileStream("Test.xlsx", FileMode.Create, FileAccess.Write))
{
XLWorkBook.Write(file);
file.Flush();
file.Close();
}
I would not recommend using file.Close() in a repeat loop. 我不建议在重复循环中使用file.Close()。
Plus, I would recommend opening the file, doing the looping, and then flushing and closing the file. 另外,我建议打开文件,进行循环,然后刷新并关闭文件。
This method is much better time-wise because repeatedly opening and closing a file consumes a lot of time and requires a trip to the drive every time. 这种方法在时间上要好得多,因为重复打开和关闭文件会消耗大量时间,并且每次都需要访问驱动器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.