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.XX.X,
- DevExpress.Printing.vXX.X.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.
(you will be redirected to DevExpress.com to submit your response)
