Exporting C1Chart to an Excel File

Exporting of ComponentOne Chart for WPF to an image and pdf file is explained in the documentation. With this blog, we will discuss how to export the C1WpfChart to an excel file. The approach is as follows :

  1. Export Chart to memorystream object using C1.WPF.C1Chart.Extended.
  2. Create Excel file using the above memorystream object with C1Excel control for WPF.

Save C1WPFChart to MemoryStream

First step is to save the C1Chart to an memorystream. We need to add reference to C1.WPF.C1Chart.Extended.dll in the project for this. The code for this looks as follows :

c1Chart1.View.Background = Brushes.White;  
System.IO.MemoryStream ms = new System.IO.MemoryStream();  
c1Chart1.SaveImage(ms, ImageFormat.Png);

Save MemoryStream to Excel File

To save to an excel file, we will use C1XLBook object of C1.WPF.Excel class library. The memorystream from the above step is then rendered into a BitmapImage object which is used in WritableBitmap object. This WritableBitmap object is then added the excel sheet and the excel file is saved using C1XLBook.Save() method.

//Save Chart image in excel file  
C1XLBook wb = new C1XLBook();  
BitmapImage bmps =new BitmapImage();  
bmps.StreamSource = ms;  
WriteableBitmap img = new WriteableBitmap(bmps);  
XLSheet sheet = wb.Sheets[0];  
sheet[0, 0].Value = img ;  

Download the attached sample for detailed implementation. Download Sample


GrapeCity Developer Tools
comments powered by Disqus