I want to convert .xlsx file to html using NPOI. Is this possible? I know , xls to html conversion is available using NPOI. But not sure if NPOI provide option to convert .xlsx file to html also. Thanks
Asked
Active
Viewed 1,979 times
1 Answers
3
You can use ExcelToHtmlConverter
. It has method ProcessWorkbook
which accepts IWorkbook
as a parameter. So it can be used to convert either HSSFWorkbook
(xls) or XSSFWorkbook
(xlsx).
public void ConvertXlsxToHtml()
{
XSSFWorkbook xssfwb;
var fileName = @"c:\temp\test.xlsx";
using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
xssfwb = new XSSFWorkbook(file);
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();
//set output parameter
excelToHtmlConverter.OutputColumnHeaders = false;
excelToHtmlConverter.OutputHiddenColumns = true;
excelToHtmlConverter.OutputHiddenRows = true;
excelToHtmlConverter.OutputLeadingSpacesAsNonBreaking = false;
excelToHtmlConverter.OutputRowNumbers = false;
excelToHtmlConverter.UseDivsToSpan = true;
//process the excel file
excelToHtmlConverter.ProcessWorkbook(xssfwb);
//output the html file
excelToHtmlConverter.Document.Save(Path.ChangeExtension(fileName, "html"));
}
}

user2250152
- 14,658
- 4
- 33
- 57
-
thanks for your prompt response. for some reason when i am converting my xlsx file using above code i am getting "Illegal IndexedColor index: 0" exception . is there any fix for this? – shashank Mar 08 '22 at 09:16
-
It's hard to say. I've tried this with simple xlsx file. How does your xlsx file looks like? I will test it. – user2250152 Mar 08 '22 at 09:23
-
@shashank I can't download it. The file has been deleted – user2250152 Mar 09 '22 at 08:10
-
can you please help me on" Illegal IndexedColor index: 0" exception – shashank Mar 09 '22 at 08:16
-
please help me on this. Really stuck . I want to convert excel files to html without opening excel in any UI – shashank Mar 11 '22 at 08:08
-
@shashank as I wrote in another question the error with Illegal IndexedColor index:0 is a bug in NPOI library and I have no idea when it will be fixed – user2250152 Mar 11 '22 at 09:07
-
Hi how to get the RawHTML from that object excelToHtmlConverter? – Drew Aguirre Apr 19 '23 at 05:41
-
or maybe save that Raw HTML to memory stream? – Drew Aguirre Apr 19 '23 at 05:41