Skip to main content
A newer version of this page is available. .

How to: Export a Document to HTML

  • 2 minutes to read

Important

The Workbook class is defined in the DevExpress.Docs.v19.2.dll assembly. Add this assembly to your project to use the workbook functionality. You need a license to the DevExpress Office File API or DevExpress Universal Subscription to use this assembly in production code. Refer to the DevExpress Subscription page for pricing information.

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’s call may occur in a different thread than the target operation.

  • The operation is not thread safe (documents should not be accessed simultaneously by different threads). Wait until the operation is completed before working with the document, i.e., 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.
          }
      }
  }
}
See Also