Excel to HTML Conversion

27 Jul 20265 minutes to read

XlsIO converts a worksheet or an entire workbook to HTML while preserving basic formatting. The features preserved during conversion are:

  • Styles
  • Hyperlinks
  • Images
  • 2D Charts

The following code example illustrates how to convert an Excel file to HTML.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  // Initialize Excel engine and create a new workbook
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];
  worksheet.Range["A1:M20"].Text = "Html Document";

  // Save a workbook as HTML file
  workbook.SaveAsHtml("Sample.html", Syncfusion.XlsIO.Implementation.HtmlSaveOptions.Default);

  workbook.Close();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet sheet = workbook.Worksheets[0];
  sheet.Range["A1:M20"].Text = "Html Document";

  // Save a workbook as HTML file
  workbook.SaveAsHtml("Sample.html", Syncfusion.XlsIO.Implementation.HtmlSaveOptions.Default);
}
Using excelEngine As New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Xlsx
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim sheet As IWorksheet = workbook.Worksheets(0)
  sheet.Range("A1:M20").Text = "Html Document"

  ' Save a workbook as HTML file
  workbook.SaveAsHtml("Sample.html", Syncfusion.XlsIO.Implementation.HtmlSaveOptions.Default)
End Using

HtmlSaveOptions

XlsIO also provides options to save a worksheet with the displayed text or value in the cell to HTML file. The following code example illustrates this.

using (ExcelEngine excelEngine = new ExcelEngine())
{
	// Initialize Excel engine and create a new workbook
	IApplication application = excelEngine.Excel;
	application.DefaultVersion = ExcelVersion.Xlsx;
	IWorkbook workbook = application.Workbooks.Create(1);
	IWorksheet worksheet = workbook.Worksheets[0];
	worksheet.Range["A1:M20"].Text = "Html Document";
	worksheet.UsedRange.AutofitColumns();

	// Create the instance of SaveOptions
	HtmlSaveOptions saveOptions = new HtmlSaveOptions();
	saveOptions.TextMode = HtmlSaveOptions.GetText.DisplayText;

	// Save the workbook as HTML
	workbook.SaveAsHtml(Path.GetFullPath("Output/Output.html"), saveOptions);
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet sheet = workbook.Worksheets[0];

  // Create the instance of SaveOptions
  HtmlSaveOptions options = new HtmlSaveOptions();
  options.TextMode = HtmlSaveOptions.GetText.DisplayText;
  options.ImagePath = "../../Images/";

  // Save the sheet as HTML
  sheet.SaveAsHtml("Sample.html", options);
}
Using excelEngine As New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Xlsx
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim sheet As IWorksheet = workbook.Worksheets(0)

  ' Create the instance of SaveOptions
  Dim options As New HtmlSaveOptions()
  options.TextMode = HtmlSaveOptions.GetText.DisplayText
  options.ImagePath = "../../Images/"

  ' Save the sheet as HTML
  sheet.SaveAsHtml("Sample.html", options)
End Using

A complete working example to save an Excel worksheet as HTML file using HtmlSaveOptions in C# is present on this GitHub page.

See also