Document Solutions for Excel, Java Edition | Document Solutions
File Operations / Export to HTML
In This Topic
    Export to HTML
    In This Topic

    Many organizations maintain their product inventories, hiring positions, price lists etc. in Excel files. However, it is very convenient to publish such data on websites to share it with relevant customers. Hence, exporting to HTML files becomes an important feature in such cases.

    DsExcel allows users to export a workbook, worksheet or any specific range to an HTML file. By default, it exports an HTML file and a folder containing additional files. These additional files can be images in a worksheet, htm files of other worksheets in a workbook or css file used for styling the html files. However, a single HTML file can also be exported while exporting a worksheet or any range of a worksheet.

    With various methods in HtmlSaveOptions class, the exported content can be controlled in various ways like exporting headings, gridlines, document properties or apply other settings like scalable width, page title, displaying tooltip text etc.

    setCssExportType method in HtmlSaveOptions class provides options for a user to decide how to export a CSS file with the HTML file using CssExportType enumeration. CssExportType enumeration allows a user to export CSS to a separate file (in the additional folder), within the style tag in HTML, or in the style attribute inside an HTML element.

    Note: If unlicensed version of DsExcel is used:

    • While exporting a workbook to HTML: An "Evaluation warning" sheet is appended to the workbook along with an "Evaluation warning" message at the head of each worksheet.
    • While exporting a worksheet or range to HTML: An "Evaluation warning" message is added at the head of worksheet or range file.

    Export workbook to HTML

    The save method of IWorkbook interface can be used to export a workbook to HTML file.

    Refer to the following example code to export workbook to a zip folder containing workbook's HTML file and folder carrying additional files.

    C#
    Copy Code
    // Create a zip file stream
    FileOutputStream outputStream = new FileOutputStream("SaveWorkbookToHTML.zip");
    // Create a new workbook
    Workbook workbook = new Workbook();
    workbook.open("NetProfit.xlsx");
    // Save workbook to html format
    workbook.save(outputStream, SaveFileFormat.Html);

    Export worksheet to HTML

    The save method of IWorkbook interface can be used to export a worksheet to HTML file. The headings and gridlines of the worksheet can also be exported by using setExportHeadings and setExportGridlines methods of HtmlSaveOptions class. The setExportSheetName method can be used to define which worksheet needs to be exported.

    Refer to the following example code to export worksheet to a zip folder containing worksheet's HTML file and a folder carrying additional files.

    Java
    Copy Code
    // Create a zip file stream
    FileOutputStream outputStream = null;
    outputStream = new FileOutputStream("SaveWorksheetToHTML.zip");
    
    // Create a new workbook
    Workbook workbook = new Workbook();
    workbook.open("ProjectTracker.xlsx");
    HtmlSaveOptions options = new HtmlSaveOptions();
    
    // Set exporting row/column headings
    options.setExportHeadings(true);
    
    // Set exporting gridlines
    options.setExportGridlines(true);
    
    // Export first sheet
    options.setExportSheetName(workbook.getWorksheets().get(0).getName());
    
    // Set exported html file name
    options.setExportFileName("HiringDetails");
    workbook.save(outputStream, options);

     

    A worksheet can also be exported to a single HTML file when the specific methods of HtmlSaveOptions class are set, as in the code below.

    Java
    Copy Code
    // Create a workbook
    Workbook workbook = new Workbook();
    
    // Open an xlsx file
    workbook.open("ProjectTracker.xlsx");
    
    // Create HtmlSaveOptions
    HtmlSaveOptions options = new HtmlSaveOptions();
    
    // Export first sheet
    options.setExportSheetName(workbook.getWorksheets().get(0).getName());
    
    // Set exported image as base64
    options.setExportImageAsBase64(true);
    
    // Set the css export type to internal CSS.
    options.setCssExportType(CssExportType.Internal);
    
    // Or, set the css export type to inline CSS.
    // options.setCssExportType(CssExportType.Inline);
    
    // Set not to export single tab in html
    options.setExportSingleTab(false);
    
    // Save first worksheet to html
    workbook.save("SaveWorksheetToSingleHTML.html", options);

    Export worksheet range to HTML

    The save method of IWorkbook interface can be used to export any range of a worksheet to HTML file. The setExportArea method of HtmlSaveOptions class can be used to define the range which needs to be exported.

    Refer to the following example code to export range in a worksheet to a zip folder containing range's HTML file and a folder carrying additional files.

    Java
    Copy Code
    // Get detail range and set style.
    for (IPivotLine item : pivottable.getPivotRowAxis().getPivotLines()) {
        if (item.getLineType() == PivotLineType.Subtotal) {
            item.getPivotLineCells().get(0).getRange().getInterior().setColor(Color.GetGreenYellow());
        }
    }

     

    A range in a worksheet can also be exported to a single HTML file when the specific methods of HtmlSaveOptions class are set, as in the code below.

    Java
    Copy Code
    // Create a new workbook
    Workbook workbook = new Workbook();
    workbook.open("ProjectTracker.xlsx");
    
    // Create HtmlSaveOptions
    HtmlSaveOptions options = new HtmlSaveOptions();
    
    // Specify exported sheet name
    options.setExportSheetName(workbook.getWorksheets().get(0).getName());
    
    // Set export area
    options.setExportArea("D2:G23");
    
    // Set exported image as base64
    options.setExportImageAsBase64(true);
    
    // Set the css export type to internal CSS.
    options.setCssExportType(CssExportType.Internal);
    
    // Or, set the css export type to inline CSS.
    // options.setCssExportType(CssExportType.Inline);
    
    // Set not to export single tab in html
    options.setExportSingleTab(false);
    
    // Save the specified range of first worksheet to html
    workbook.save("WorksheetRangeToHTML.html", options);
    Note: setExportCssSeparately method is now obsolete, but the existing applications with this property will continue to function properly.

    Limitations

    The following features are not supported while exporting to HTML file: