Search code examples
apache-poinpoi

Convert .xlsx file to html using NPOI


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


Solution

  • 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"));
        }
    }