簡體   English   中英

使用NPOI保存xlsx文件后,當我打開xlsx文件時,它說文件已損壞

[英]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();
}

我用此代碼編寫了excel文件。 寫入excel后,當我手動打開excel文件時,它已損壞。 我正在使用NPOI二進制文件2.1.3.1,請說明如何避免excel損壞。

嘗試添加file.Flush(); 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();
}

我不建議在重復循環中使用file.Close()。

另外,我建議打開文件,進行循環,然后刷新並關閉文件。

這種方法在時間上要好得多,因為重復打開和關閉文件會消耗大量時間,並且每次都需要訪問驅動器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM