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
It is possible in general, but not quite easy, rather it is significantly complicated task.
XLS
file format is handled byHSSFWorkbook
class (and accordingHSSFSheet
and so on).XLSX
file format is handled byXSSFWorkbook
class (andXSSFSheet
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 appropriateXSSFSheet
, 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.It's an old question, but I've encountered with the similar problem. This code is using NPOI 2.2.1 from https://npoi.codeplex.com/releases. This code is based on code from Convert xlsx file to xls using NPOI in c# question, but it converts xls->xlsx, not xlsx->xls. Plus it converts styles and fixes bug with ranges. This code works for simple workbooks without charts and other complex stuff.