Back to Devexpress

How to: Export a Chart to HTML and MHT

windowsforms-2507-controls-and-libraries-chart-control-examples-producing-output-how-to-export-a-chart-to-html-and-mht.md

latest3.4 KB
Original Source

How to: Export a Chart to HTML and MHT

  • Nov 13, 2018
  • 2 minutes to read

The following example demonstrates how to export a chart to HTML and MHT files. You may use similar methods in your applications to export a ChartControl as HTML of MHT.

csharp
using System.IO;
using DevExpress.XtraCharts;
using DevExpress.XtraPrinting;
// ...

private void ExportChartToHTML(ChartControl chart) {
    if (chart.IsPrintingAvailable) {
        // Create an object containing HTML export options.
        HtmlExportOptions htmlOptions = new HtmlExportOptions();

        // Set HTML-specific export options.
        htmlOptions.CharacterSet = "utf-8";
        htmlOptions.RemoveSecondarySymbols = false;
        htmlOptions.Title = "Unicode UTF-8 Example";

        // Export a chart to an HTML file.
        chart.ExportToHtml("OutputUnicode.html", htmlOptions);

        // Export a chart to a stream as HTML.
        using (FileStream htmlStream = new FileStream("OutputDefault.html", FileMode.Create)){
            chart.ExportToHtml(htmlStream, htmlOptions);
        }
    }
}

private void ExportChartToMHT(ChartControl chart) {
    if (chart.IsPrintingAvailable) {
        // Create an object containing MHT export options.
        MhtExportOptions mhtOptions = new MhtExportOptions();

        // Set MHT-specific export options.
        mhtOptions.CharacterSet = "iso-8859-1";
        mhtOptions.Title = "Unicode UTF-8 Example";

        // Export a chart to an MHT file.
        chart.ExportToMht("OutputUnicode.mht", mhtOptions);

        // Export a chart to a stream as MHT.
        using (FileStream mhtStream = new FileStream("OutputDefault.mht", FileMode.Create)){
            chart.ExportToMht(mhtStream, mhtOptions);
        }
    }
}
vb
Imports System.IO
Imports DevExpress.XtraCharts
Imports DevExpress.XtraPrinting
' ...
Private Sub ExportChartToHTML(ByVal chart As ChartControl)
    If chart.IsPrintingAvailable Then
        ' Create an object containing HTML export options.
        Dim htmlOptions As HtmlExportOptions = New HtmlExportOptions()

        ' Set HTML-specific export options.
        htmlOptions.CharacterSet = "utf-8"
        htmlOptions.RemoveSecondarySymbols = False
        htmlOptions.Title = "Unicode UTF-8 Example"

        ' Export a chart to an HTML file.
        chart.ExportToHtml("OutputUnicode.html", htmlOptions)

        ' Export a chart to a stream as HTML.
        Using htmlStream As FileStream = New FileStream("OutputDefault.html", FileMode.Create)
            chart.ExportToHtml(htmlStream, htmlOptions)
        End Using
    End If
End Sub

Private Sub ExportChartToMHT(ByVal chart As ChartControl)
    If chart.IsPrintingAvailable Then
        ' Create an object containing MHT export options.
        Dim mhtOptions As MhtExportOptions = New MhtExportOptions()

        ' Set MHT-specific export options.
        mhtOptions.CharacterSet = "iso-8859-1"
        mhtOptions.Title = "Unicode UTF-8 Example"

        ' Export a chart to an MHT file.
        chart.ExportToMht("OutputUnicode.mht", mhtOptions)

        ' Export a chart to a stream as MHT.
        Using mhtStream As FileStream = New FileStream("OutputDefault.mht", FileMode.Create)
            chart.ExportToMht(mhtStream, mhtOptions)
        End Using
    End If
End Sub