Excel to HTML Conversion
14 Oct 20245 minutes to read
XlsIO provides support to convert a worksheet or entire workbook to HTML with basic formatting preserved. The supporting formats in this 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 open 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";
//Create stream to store HTML file.
Stream stream = new MemoryStream();
//Save an Excel sheet as HTML file
worksheet.SaveAsHtml(stream);
//Save a workbook as HTML file
workbook.SaveAsHtml(stream, Syncfusion.XlsIO.Implementation.HtmlSaveOptions.Default);
stream.Dispose();
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 an Excel sheet as HTML file
sheet.SaveAsHtml("Sample.html");
//Save the workbook as HTML file
workbook.SaveAsHtml("Sample.html", Syncfusion.XlsIO.Implementation.HtmlSaveOptions.Default);
}
Using excelEngine As ExcelEngine = 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 an Excel sheet as HTML file
sheet.SaveAsHtml("Sample.html")
'Save a workbook as HTML file
workbook.SaveAsHtml("Sample.html", Syncfusion.XlsIO.Implementation.HtmlSaveOptions.Default)
End Using
Save Options
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 open 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";
//Create the instant for SaveOptions
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.TextMode = HtmlSaveOptions.GetText.DisplayText;
worksheet.UsedRange.AutofitColumns();
#region Save as HTML
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.html"), FileMode.Create, FileAccess.Write);
workbook.SaveAsHtml(outputStream, saveOptions);
#endregion
//Dispose streams
outputStream.Dispose();
}
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 instant for SaveOptions
HtmlSaveOptions options = new HtmlSaveOptions();
options.TextMode = HtmlSaveOptions.GetText.DisplayText;
options.ImagePath = "../../Images/";
//Save the sheet as HTML
sheet.SaveAsHtml("Sample.html", options);
workbook.SaveAs("Output.xlsx");
}
Using excelEngine As ExcelEngine = 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 instant for SaveOptions
Dim options As New HtmlSaveOptions()
options.TextMode = HtmlSaveOptions.GetText.DisplayText
options.ImagePath = "../../Images/"
'Save the sheet as HTML
sheet.SaveAsHtml("Sample.html", options)
workbook.SaveAs("Output.xlsx")
End Using
A complete working example to save an Excel worksheet as HTML file using HtmlSaveOptions in C# is present on this GitHub page.