How to Convert Excel to HTML in C#
Source: Dev.to
Introduction
Displaying tabular data on the web can often be a challenge. While Excel spreadsheets are powerful for data organization and analysis, their native .xlsx or .xls formats are not directly renderable by web browsers. Converting Excel to HTML using C# bridges the gap between rich Excel data and web‑friendly formats. This guide shows how to use Spire.XLS for .NET to transform Excel data into clean, accessible HTML.
Why Convert Excel to HTML?
- Web Display – HTML is the native language of the web, allowing seamless embedding of data in web applications, dashboards, or reports without requiring Excel on the client.
- Cross‑Platform Compatibility – HTML works in all browsers and operating systems, ensuring anyone can view the data.
- Ease of Sharing – A simple HTML file is often more convenient to distribute than an Excel spreadsheet when only viewing is needed.
- Reduced Client‑Side Dependencies – No need for Excel viewers or plugins, simplifying deployment and improving user experience.
- Common Use Cases – Generating online reports, data‑driven web pages, product catalogs, or integrating financial data into web‑based portfolio trackers.
Installing Spire.XLS for .NET
The easiest way to add Spire.XLS to your C# project is via NuGet:
Install-Package Spire.XLS
Alternatively, search for Spire.XLS in the NuGet Package Manager GUI and install it.
Converting an Excel File to HTML
Project Setup
- Create a new C# project (e.g., a console application).
- Add a reference to Spire.XLS (
using Spire.Xls;).
Core Steps
- Load the workbook – instantiate a
Workbookobject and callLoadFromFile(). - Save to HTML – use
SaveToFile()with theFileFormat.Htmlenum.
Complete Code Example
using Spire.Xls;
using System;
using System.IO;
namespace ExcelToHtmlConverter
{
class Program
{
static void Main(string[] args)
{
// Path to your input Excel file
string excelFilePath = "SampleData.xlsx";
// Path for the output HTML file
string htmlFilePath = "OutputExcel.html";
// Create a Workbook object
Workbook workbook = new Workbook();
try
{
// Load the Excel file
workbook.LoadFromFile(excelFilePath);
// Save the entire workbook to an HTML file
workbook.SaveToFile(htmlFilePath, FileFormat.Html);
Console.WriteLine($"Successfully converted '{excelFilePath}' to '{htmlFilePath}'.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
finally
{
// Dispose of the workbook object to release resources
workbook.Dispose();
}
Console.ReadKey(); // Keep console open
}
}
}
Code Explanation
Workbook workbook = new Workbook();– Initializes a new workbook instance.workbook.LoadFromFile(excelFilePath);– Loads the existing Excel file.workbook.SaveToFile(htmlFilePath, FileFormat.Html);– Converts and saves the workbook as an HTML document.
Saving a Specific Worksheet to HTML
If you need only a particular sheet, access it via workbook.Worksheets[index] and use SaveToHtml() with optional HTML settings.
// Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
// Set HTML options (e.g., embed images)
HTMLOptions options = new HTMLOptions
{
ImageEmbedded = true // Embeds images directly into the HTML
};
// Save the sheet to an HTML file with options
using (FileStream fileStream = new FileStream("OutputSheet.html", FileMode.Create))
{
sheet.SaveToHtml(fileStream, options);
}
This approach creates a self‑contained HTML page with embedded images, useful for standalone web pages.
Conclusion
Converting Excel files to HTML in C# is a powerful technique for making data accessible on the web. Spire.XLS for .NET simplifies the process, offering flexible options for whole‑workbook or single‑worksheet conversions. By following the steps in this guide, you can integrate Excel data into web applications, reports, and dashboards, enhancing cross‑platform compatibility and user experience. Start using programmatic Excel‑to‑HTML conversion in your projects today.