Convert Excel to HTML

The code example above demonstrates how to convert Excel to HTML using C#. The HtmlExportOptions class is used to customize how the HTML output is generated. The available options are:

  • OutputRowNumbers property: Indicates whether to show row numbers in the resulting HTML file.
  • OutputColumnHeaders property: Indicates whether to show column headers in the resulting HTML file.
  • OutputHiddenRows property: Indicates whether to display hidden rows in the resulting HTML file.
  • OutputHiddenColumns property: Indicates whether to display hidden columns in the resulting HTML file.
  • OutputLeadingSpacesAsNonBreaking property: Indicates whether to show leading spaces (extra spaces before the first character in a cell) as non-breaking spaces in the resulting HTML file.

Here is an example code block using these options:

using System;
using System.IO;
using Aspose.Cells;

class Program
{
    static void Main()
    {
        // Load the Excel file
        Workbook workbook = new Workbook("input.xlsx");

        // Create an instance of HtmlSaveOptions
        HtmlSaveOptions options = new HtmlSaveOptions();

        // Set the HtmlExportOptions properties based on your requirements
        options.OutputRowNumbers = true; // Show row numbers in the HTML output
        options.OutputColumnHeaders = true; // Show column headers in the HTML output
        options.OutputHiddenRows = false; // Do not display hidden rows
        options.OutputHiddenColumns = false; // Do not display hidden columns
        options.OutputLeadingSpacesAsNonBreaking = true; // Show leading spaces as non-breaking

        // Define the output HTML file path
        string outputFilePath = "output.html";

        // Save the workbook as an HTML file with the specified options
        workbook.Save(outputFilePath, options);

        // Inform the user
        Console.WriteLine($"Excel file has been successfully converted to HTML and saved to {outputFilePath}");
    }
}
using System;
using System.IO;
using Aspose.Cells;

class Program
{
    static void Main()
    {
        // Load the Excel file
        Workbook workbook = new Workbook("input.xlsx");

        // Create an instance of HtmlSaveOptions
        HtmlSaveOptions options = new HtmlSaveOptions();

        // Set the HtmlExportOptions properties based on your requirements
        options.OutputRowNumbers = true; // Show row numbers in the HTML output
        options.OutputColumnHeaders = true; // Show column headers in the HTML output
        options.OutputHiddenRows = false; // Do not display hidden rows
        options.OutputHiddenColumns = false; // Do not display hidden columns
        options.OutputLeadingSpacesAsNonBreaking = true; // Show leading spaces as non-breaking

        // Define the output HTML file path
        string outputFilePath = "output.html";

        // Save the workbook as an HTML file with the specified options
        workbook.Save(outputFilePath, options);

        // Inform the user
        Console.WriteLine($"Excel file has been successfully converted to HTML and saved to {outputFilePath}");
    }
}
Imports System
Imports System.IO
Imports Aspose.Cells

Friend Class Program
	Shared Sub Main()
		' Load the Excel file
		Dim workbook As New Workbook("input.xlsx")

		' Create an instance of HtmlSaveOptions
		Dim options As New HtmlSaveOptions()

		' Set the HtmlExportOptions properties based on your requirements
		options.OutputRowNumbers = True ' Show row numbers in the HTML output
		options.OutputColumnHeaders = True ' Show column headers in the HTML output
		options.OutputHiddenRows = False ' Do not display hidden rows
		options.OutputHiddenColumns = False ' Do not display hidden columns
		options.OutputLeadingSpacesAsNonBreaking = True ' Show leading spaces as non-breaking

		' Define the output HTML file path
		Dim outputFilePath As String = "output.html"

		' Save the workbook as an HTML file with the specified options
		workbook.Save(outputFilePath, options)

		' Inform the user
		Console.WriteLine($"Excel file has been successfully converted to HTML and saved to {outputFilePath}")
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation:

  • Loading the Excel File: The Excel file input.xlsx is loaded into a Workbook object.
  • HtmlSaveOptions: An instance of HtmlSaveOptions is used to configure HTML export settings.
  • Setting Options: Various properties such as OutputRowNumbers, OutputColumnHeaders, etc., are set to customize the HTML output.
  • Saving the HTML File: The workbook is saved as an HTML file at the specified output path using the configured options.
  • Output: The program outputs a confirmation message indicating the HTML file's location.