Document Solutions for Excel, .NET 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 properties 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. These properties are listed below:

    Class Option Description
    Export Options HTMLSaveOptions AddTooltipText Indicates whether to add tooltip text when the data can't be fully displayed. The default value is false.
    AttachedFilesUrlPrefix Specifies the URL prefix of attached files, such as images in the HTML file.
    CellCssPrefix Sets the prefix of the CSS name. The default value is null.
    CssExportType Specifies the way in which the stylesheet (CSS) is exported.
    Encoding Sets encoding type. The default encoding type is Encoding.UTF8.
    ExportArea Indicates which area would be exported to HTML. This is used for exporting a specific area of a worksheet. Only take effect when HTMLSaveOptions.ExportSheetName is not null.
    ExportCssSeparately Indicates whether to export the worksheet style to CSS separately. The default value is true. When this property is set to false. CSS style data is exported directly to each worksheet and separate stylesheet.css files are not created.
    ExportDocumentProperties Indicates whether to export document properties. The default value is true.
    ExportFileName Indicates the name of the HTML file. Only for saving to HTML stream, and the default value is "workbook".
    ExportGridlines Indicates whether to export the gridlines. The default value is false.
    ExportHeadings Indicates whether to export headings when saving a file to HTML. The default value is false.
    ExportHiddenWorksheet Indicates whether to export the hidden worksheet. The default value is false.
    ExportImageAsBase64 Specifies whether images are saved in Base64 format to HTML. The default value is false. When this property is set to true, image data is exported directly to the img elements, and separate files are not created.
    ExportSheetName Indicates which worksheet would be exported to HTML. This is used for exporting specific worksheets of workbook.
    ExportSingleTab Indicates whether to export the single tab when the file only has one worksheet or ExportSheetName is set. The default value is false.
    FileFormat Represents the format in which the workbook is saved.
    IsExportComments Indicates whether to export comments when saving a file to HTML. The default value is false.
    IsFullPathLink Indicates whether to use a full path link in HTML. Only for exporting to HTML file. The default value is false.
    IsWidthScalable Indicates whether to use a scalable unit to describe the column width when exporting a file to HTML. The default value is false.
    LinkTargetType Indicates the type of target attribute in <a> link. The default value is HyperLinkTargetType.Blank.
    PageTitle The title of the HTML page.
    TableCssId Sets the prefix of the type CSS name, such as tr, td, and so on; they are contained in the table element, which has the specific TableCssId attribute. The default value is null.
    IncludeAutoMergedCells Indicates whether to include the automatically merged cells. The default value is false.

    CssExportType property 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
    FileStream outputStream = new FileStream("saveworkbooktohtml.zip", FileMode.Create);
    
    //create a new workbook
    var workbook = new Workbook();
    
    workbook.Open("pricelist.xlsx");
    
    //save workbook to html format
    workbook.Save(outputStream, SaveFileFormat.Html);
    
    //close the zip stream
    outputStream.Close();

    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 ExportHeadings and ExportGridlines properties of HTMLSaveOptions class. The ExportSheetName property 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.

    C#
    Copy Code
    //create a zip file stream
    FileStream outputStream = new FileStream("saveworksheettohtml.zip", FileMode.Create);
    
    //create a new workbook
    var workbook = new Workbook();
    
    workbook.Open("hiringpositions.xlsx");
    
    HtmlSaveOptions options = new HtmlSaveOptions();
    
    //set exporting row/column headings
    options.ExportHeadings = true;
    
    //set exporting gridlines
    options.ExportGridlines = true;
    
    //export first sheet
    options.ExportSheetName = workbook.Worksheets[0].Name;
    
    //set exported html file name
    options.ExportFileName = "hiringdetails";
    
    workbook.Save(outputStream, options);
    
    //close the zip stream
    outputStream.Close();

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

    C#
    Copy Code
    // Create a new workbook.
    var workbook = new GrapeCity.Documents.Excel.Workbook();
    
    // Open en Excel file.
    FileStream fileStream = new FileStream("BreakEven.xlsx", FileMode.Open);
    workbook.Open(fileStream);
    
    IWorksheet worksheet = workbook.Worksheets[0];
    
    // Initialize HtmlSaveOptions.
    HtmlSaveOptions options = new HtmlSaveOptions();
    
    // Export first sheet.
    options.ExportSheetName = workbook.Worksheets[0].Name;
    
    // Set exported image as base64.
    options.ExportImageAsBase64 = true;
    
    // Set the css export type to internal CSS.
    options.CssExportType = CssExportType.Internal;
    
    // Or, set the css export type to inline CSS.
    // options.CssExportType = CssExportType.Inline;
    
    // Set not to export single tab in HTML.
    options.ExportSingleTab = 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 an HTML file using ExportArea property of HTMLSaveOptions class, which defines the specific area of a specific worksheet to be exported. This property only works when ExportSheetName is assigned.

    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.

    C#
    Copy Code
    //create a zip file stream
    FileStream outputStream = new FileStream("saverangetohtml.zip", FileMode.Create);
    
    //create a new workbook
    var workbook = new Workbook();
    
    workbook.Open("projecttracker.xlsx");
    
    HtmlSaveOptions options = new HtmlSaveOptions();
    
    //export first sheet
    options.ExportSheetName = workbook.Worksheets[0].Name;
    
    //set export area
    options.ExportArea = "D2:G23";
    
    //set exported html file name
    options.ExportFileName = "range";
    
    //set html with scalable width
    options.IsWidthScalable = true;
    
    workbook.Save(outputStream, options);
    
    //close the zip stream
    outputStream.Close();

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

    C#
    Copy Code
    // Create a new workbook.
    var workbook = new GrapeCity.Documents.Excel.Workbook();
    
    // Open en Excel file.
    FileStream fileStream = new FileStream("BreakEven.xlsx", FileMode.Open);
    workbook.Open(fileStream);
    
    IWorksheet worksheet = workbook.Worksheets[0];
    
    // Initialize HtmlSaveOptions.
    HtmlSaveOptions options = new HtmlSaveOptions();
    
    // Export first sheet.
    options.ExportSheetName = workbook.Worksheets[0].Name;
    
    // Set export area.
    options.ExportArea = "D2:G23";
    
    // Set exported image as base64.
    options.ExportImageAsBase64 = true;
    
    // Set the css export type to internal CSS.
    options.CssExportType = CssExportType.Internal;
    
    // Or, set the css export type to inline CSS.
    // options.CssExportType = CssExportType.Inline;
    
    // Set not to export single tab in HTML.
    options.ExportSingleTab = false;
    
    // Save the specified range of first worksheet to HTML.
    workbook.Save("SaveSpecificRangetoSingleHTML.html", options);
    Note: ExportCssSeparately property 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: