C#/VB.NET: Convert Excel to HTML

When you create an Excel table and want to publish it online as a web page, the simplest way is to convert it to an HTML file. This article will demonstrate how to convert Excel to HTML programmatically from the following two aspects using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Convert Excel to HTML

Spire.XLS for .NET supports converting a specific Excel worksheet to HTML using Worksheet.SaveToHtml() method. Detailed steps are listed below.

  • Create a Workbook instance.
  • Load an Excel sample document using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[] property
  • Save the worksheet as an HTML file using Worksheet.SaveToHtml() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace XLSToHTML

{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            
            //Load an Excel sample document
            workbook.LoadFromFile(@"sample.xlsx");

            //Get the first worksheet of the document 
            Worksheet sheet = workbook.Worksheets[0];
           
            //Save the worksheet to HTML
            sheet.SaveToHtml("ExcelToHTML.html");
        }
    }
}

C#/VB.NET: Convert Excel to HTML

Convert Excel to HTML with Images Embedded

The following are steps to convert an Excel worksheet to HTML with images embedded.

  • Create a Workbook instance.
  • Load an Excel sample document using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[] property.
  • Create an HTMLOptions instance.
  • Set the ImageEmbedded as true to embed images to HTML.
  • Save the worksheet as an HTML file using Worksheet.SaveToHtml() method.
  • C#
  • VB.NET
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet;

namespace XLSToHTML

{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();             
   
            //Load an Excel sample document
            workbook.LoadFromFile(@"sample.xlsx");
   
            //Get the first worksheet of the document 
            Worksheet sheet = workbook.Worksheets[0];
  
            //Create an HTMLOptions instance
            HTMLOptions options = new HTMLOptions();
  
            //Embed images to HTML
            options.ImageEmbedded = true;
  
            //Save the worksheet to HTML
            sheet.SaveToHtml("XLS2HTML.html", options);
        }
    }
}

C#/VB.NET: Convert Excel to HTML

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.