Documents for Excel .NET Edition | GCDocuments
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.

    GcExcel 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.

    The ExportCssSeparately property in HtmlSaveOptions class exports the css file separately (in the additional folder), as its default value is true. However, it can be set to false so that the css style data is exported directly to each worksheet and separate css file is not created.

    Note: If unlicensed version of GcExcel 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
    Workbook workbook = new Workbook();
    
    //open an xlsx file         
    workbook.Open("productlist.xlsx");
    
    IWorksheet worksheet = workbook.Worksheets[0];
    
    //create HtmlSaveOptions
    HtmlSaveOptions options = new HtmlSaveOptions();
    
    //export first sheet
    options.ExportSheetName = workbook.Worksheets[0].Name;
    
    //set exported image as base64
    options.ExportImageAsBase64 = true;
    
    //set exported css style in html file
    options.ExportCssSeparately = false;
    
    //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 HTML file. The ExportArea property 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.

    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
    Workbook workbook = new Workbook();
    
    //open an xlsx file
    workbook.Open("netprofit.xlsx");
    IWorksheet worksheet = workbook.Worksheets[0];
    
    //create HtmlSaveOptions
    HtmlSaveOptions options = new HtmlSaveOptions();
    
    //specify exported sheet name
    options.ExportSheetName = workbook.Worksheets[0].Name;
    
    //set export area
    options.ExportArea = "D2:G23";
    
    //set exported image as base64
    options.ExportImageAsBase64 = true;
    
    //set exported css style in html file
    options.ExportCssSeparately = false;
    
    //set not to export single tab in html
    options.ExportSingleTab = false;
    
    //save the specified range of first worksheet to html
    workbook.Save("range.html", options);

    Limitations

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