How to: Export a Document to HTML
- 3 minutes to read
Important
The Workbook class is defined in the DevExpress.Docs.v24.2.dll assembly. Add this assembly to your project to use the Workbook API. You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this assembly in production code.
The example below demonstrates how to save a worksheet as HTML file using the Workbook.ExportToHtml method. To specify export options, create an instance of the HtmlDocumentExporterOptions class and pass it as a parameter to the ExportToHtml method.
Worksheet worksheet = workbook.Worksheets["Grouping"];
workbook.Worksheets.ActiveWorksheet = worksheet;
HtmlDocumentExporterOptions options = new HtmlDocumentExporterOptions();
// Specify the part of the document to be exported to HTML.
options.SheetIndex = worksheet.Index;
options.Range = "B2:G7";
// Export the active worksheet to a stream as HTML with the specified options.
using (FileStream htmlStream = new FileStream("OutputWorksheet.html", FileMode.Create))
{
workbook.ExportToHtml(htmlStream, options);
}
System.Diagnostics.Process.Start("OutputWorksheet.html");
Asynchronous Export
Use the Workbook.ExportToHtmlAsync method to asynchronously export a workbook to HTML format.
Important
Take into account the following when you call this method:
The events fired by this method call may occur in a different thread than the target operation.
The operation is not thread safe (the document should not be accessed simultaneously by different threads). Wait until the operation is completed before you continue to work with the document (for example, use the
await
operator).
The code sample below asynchronously exports the first worksheet of the loaded XLSX document to HTML using the CancellationToken object:
private async void ConvertXlsx2HtmlWithCancellation()
{
using (CancellationTokenSource source = new CancellationTokenSource(10000))
{
using (Workbook workbook = new Workbook())
{
try
{
await workbook.LoadDocumentAsync("Document.xlsx", source.Token);
await workbook.ExportToHtmlAsync("Result.html", 0, source.Token);
}
catch (OperationCanceledException)
{
// Your code to handle cancellation.
}
}
}
}
Calculate Formulas Before Export
The default calculation mode for a Workbook is Manual. This mode implies that the Spreadsheet component does not recalculate formulas before it generates an HTML file. Call the Workbook.Calculate or Workbook.CalculateFull method to calculate all formulas in the workbook before you export it to HTML.
using (Workbook workbook = new Workbook())
{
// Load a document.
// ...
// Modify the document.
// ...
// Calculate formulas in the document.
workbook.Calculate();
// Export the first worksheet to HTML.
workbook.ExportToHtml("Result.html", 0);
}