经验分享 · 2023年8月17日

Solved “Cannot access a closed stream” error when update NPOI HSSFWorkbook to XSSFWorkbook

I am using NPOI library to export datatable to excel file.

When using HSSFWorkbook, everything works well. But I try to migrate to XSSFWorkbook today, got “Cannot access a closed stream” error. After debug, It is confirmed that XSSFWorkbook.write function closed the stream after write.

So the solution is clearly, use book.Write(ms, true); overided version to leave the stream open.