ChartControl.ExportToXls(Stream) Method
Creates an XLS document with a chart inserted as an image and sends it to a stream.
Namespace: DevExpress.XtraCharts
Assembly: DevExpress.XtraCharts.v20.1.UI.dll
NuGet Package: DevExpress.Win.Charts
Declaration
Parameters
Name | Type | Description |
---|---|---|
stream | Stream | A Stream object to which the created document is exported. |
Remarks
To show the standard Print dialog, use the ChartControl.Print method. The dialog allows end-users to print the chart, select the printer (if required), specify the range of pages to print, the number of copies, etc.
To display the DevExpress Print Preview use one of the following methods.
Method | Description |
---|---|
ChartControl.ShowPrintPreview | Creates the print document and displays the Print Preview of the document. |
ChartControl.ShowRibbonPrintPreview | Creates the print document and displays the Print Preview with the Ribbon toolbar of the document. |
To export the chart, use the appropriate ExportTo~ method (e.g., ChartControl.ExportToHtml, ChartControl.ExportToPdf, etc.)
Important
Note that, exporting to raster and vector images is implemented by the Chart and does not require any library.
The chart can be previewed, printed and exported to other formats only if the Printing library is available. Make sure you add a reference to the DevExpress.XtraPrinting.v20.1 assembly.
Also note that chart export to PDF requires the DevExpress.Pdf.Core library to be available.
Example
The following example exports a chart as an image to an XLS document. In this example, the form contains the Export to File and Export to Stream buttons that allow you to save the XLS document with the chart image to a file or a stream.
Follow the steps below to implement this scenario:
Add a Chart Control object to the form. Make sure that the Chart Control Name is chartControl1 in the Visual Studio Properties window. For more information, see the following topic: How to: Add a Chart to a Windows Forms Application.
Create a Series object in the Form1_Load event handler. Use the ChartControl.Series.Add method to add this series to the chart.
Specify the ChartControl.DataSource, Series.ArgumentDataMember, and Series.ValueDataMembers properties to populate the chart with data. In this example, the GetSales method returns the chart data source.
Add two SimpleButtons to the form. Set the first button’s Text property to Export to File and the second button’s property to Export to Stream.
The Chart Control uses the DevExpress Printing Library to export a chart. To use this library, add references to the following assemblies to the project:
- DevExpress.XtraPrinting.v20.1,
- DevExpress.Printing.v20.1.Core.
Handle the first button’s Click event and use the ChartControl.IsPrintingAvailable property to check whether the chart can be exported. Call the ChartControl.ExportToXls(System.String) method to export the chart to a file. Pass the path to this file as a parameter to this method.
Handle the second button’s Click event and use the ChartControl.IsPrintingAvailable property to check whether the chart can be exported. Call the
ChartControl.ExportToXls(System.IO.Stream)
method to export the chart to a stream. This example uses FileStream, and you can open the file where the stream is saved to check the export result.
using DevExpress.XtraCharts;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
namespace ExportToXLS {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
Series series = new Series("Series1", ViewType.Bar);
chartControl1.Series.Add(series);
chartControl1.DataSource = GetSales();
series.ArgumentDataMember = "Region";
series.ValueDataMembers.AddRange(new string[] { "Sales" });
}
private DataTable GetSales() {
int prevYear = DateTime.Now.Year - 1;
DataTable table = new DataTable();
table.Columns.Add("Region", typeof(string));
table.Columns.Add("Sales", typeof(decimal));
table.Rows.Add("Asia", 4.2372D);
table.Rows.Add("Australia", 1.7871D);
table.Rows.Add("Europe", 3.0884D);
table.Rows.Add("North America", 3.4855D);
table.Rows.Add("South America", 1.6027D);
return table;
}
private void simpleButton1_Click(object sender, EventArgs e) {
if (chartControl1.IsPrintingAvailable) {
// The XLS file name.
string fileName = "Output.xls";
// Path to an XLS file.
string filePath = "c:\\temp";
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
string fullPath = String.Format("{0}\\{1}", filePath, fileName);
// Exports to an XLS file.
chartControl1.ExportToXls(fullPath);
}
}
private void simpleButton2_Click(object sender, EventArgs e) {
if (chartControl1.IsPrintingAvailable) {
// The XLS file name.
string fileName = "Output.xls";
// Path to an XLS file.
string filePath = "c:\\temp";
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
string fullPath = String.Format("{0}\\{1}", filePath, fileName);
// Exports to a stream as XLS.
FileStream xlsStream = new FileStream(fullPath, FileMode.Create);
chartControl1.ExportToXls(xlsStream);
xlsStream.Close();
}
}
}
}
Related GitHub Examples
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the ExportToXls(Stream) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.