how to save xls file as xlsx file using NPOI c#?

Yacino picture Yacino · Apr 8, 2015 · Viewed 7.5k times · Source

I'm using NPOI to open XLS file, then add some modifications to the XLS file. at the end i want to save it as XLSX file.

i'm using this code to save it as XLS file:

using (var fs = new FileStream(Name, FileMode.Create, FileAccess.Write))
{
     wb.Write(fs);
} 

Is it possible to save this XLS file as XLSX file using NPOI in C#?

Thanks in advance for your response

Answer

Andy Korneyev picture Andy Korneyev · Apr 10, 2015

It is possible in general, but not quite easy, rather it is significantly complicated task.

XLS file format is handled by HSSFWorkbook class (and according HSSFSheet and so on).
XLSX file format is handled by XSSFWorkbook class (and XSSFSheet and so on).

So in order to save your file as XLSX after you opened and modified it using NPOI, you need to create new XSSFWorkbook, then for each worksheet of your source file you need to create appropriate XSSFSheet, copy data to it from your original worksheet and so on until you will get full copy of your data, and then save workbook to file.