Mostly, when we talk about exporting chart to an Excel sheet, we expect Chart to be exported to Excel sheet as an Image. In case you are looking to export C1Chart for Silverlight as an Image, you can refer to this documentation link. However, there can be alternative scenario, where we would like the Chart to be exported in Text Format. This blog just looks to cater to this exporting functionality which exports a Bound C1Chart for Silverlight data to an Excel Sheet.

Binding Chart Data

To implement Chart Binding, you can refer to this documentation link. For the blog sample, following data binding code has been used.



            DataTable dTable = new DataTable();  
            dTable.Columns.Add("Month");  
            dTable.Columns.Add("Sales");  

            DataRow dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "January", 2000 };  
            dTable.Rows.Add(dRow);  

            dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "Feburary", 3423 };  
            dTable.Rows.Add(dRow);  

            dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "March", 2394 };  
            dTable.Rows.Add(dRow);  

            dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "April", 5920 };  
            dTable.Rows.Add(dRow);  

            dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "May", 4302 };  
            dTable.Rows.Add(dRow);  

            dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "June", 5222 };  
            dTable.Rows.Add(dRow);  

            dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "July", 6213 };  
            dTable.Rows.Add(dRow);  

            dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "August", 7990 };  
            dTable.Rows.Add(dRow);  

            dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "September", 6928 };  
            dTable.Rows.Add(dRow);  

            dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "October", 5021 };  
            dTable.Rows.Add(dRow);  

            dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "November", 4321 };  
            dTable.Rows.Add(dRow);  

            dRow = dTable.NewRow();  
            dRow.ItemArray = new object[] { "December", 3232 };  
            dTable.Rows.Add(dRow);  

            c1Chart1.Data.ItemNameBinding = new Binding("Month");  
            c1Chart1.Data.Children.Add(new DataSeries() { ValueBinding = new System.Windows.Data.Binding("Sales"), Name = "Sales" });  
            c1Chart1.Data.ItemsSource = dTable.DefaultView;  
            c1Chart1.ChartType = ChartType.Column;  

            c1Chart1.View.AxisY.AutoMin = false;  
            c1Chart1.View.AxisY.Min = 1000;  
            c1Chart1.View.AxisY.AutoMax = false;  
            c1Chart1.View.AxisY.Max = 8000;  
            c1Chart1.View.AxisY.MajorUnit = 1000;  


Exporting Chart Data

Now there is no special technique to export the data; apart from looping through Chart Elements. To get the Axis label strings, get the TextBlock elements from AxisX UIElement collection and read their 'Text' property. Similarly for values, retrieve the Bar Elements for each series and read the DataPoint property. See the given code to understand the actual implementation.



public C1.Silverlight.Excel.C1XLBook ExportChartExcel(C1.Silverlight.Chart.C1Chart _chart)  
{  
   C1XLBook wb = new C1XLBook();  
   wb.Sheets.Add("Chart Data");  

   int _row = 1;  

   IList<DependencyObject> _itemNameList = new List<DependencyObject>();  
   VTreeHelper.GetChildrenOfType((\_chart.View.AxisX as Panel), typeof(TextBlock), ref \_itemNameList);  

   wb.Sheets[0][0, 0].Value = "Month";  
   foreach (var \_itemNameObject in \_itemNameList.ToList())  
   {  
      wb.Sheets[0][\_row, 0].Value = (\_itemNameObject as TextBlock).Text;  
      _row += 1;  
   }  

   foreach (DataSeries \_dataSeries in \_chart.Data.Children)  
   {  
      _row = 1;  
      wb.Sheets[0][\_row - 1, 1].Value = \_dataSeries.Name;  

      IList<DependencyObject> _seriesChildObject = new List<DependencyObject>();  
      VTreeHelper.GetChildrenOfType((\_dataSeries as Panel), typeof(Bar), ref \_seriesChildObject);  

      foreach (var cvl in _seriesChildObject.ToList())  
      {  
         wb.Sheets[0][_row, 1].Value = (cvl as Bar).DataPoint.Value;  
         _row += 1;  
      }  
   }  

   return wb;  
}  

I believe lot of developers might be interested to look at this blog implementation. However, they should consider this just a suggestive way to export the data. There are lot of scope for modifications as per the customization done by the developers using Templates in the Chart. They will accordingly have to change the elements to be retrieve to get the desired values. Refer to the attached sample for complete implementation. Download Sample