将C1Chart数据导出到Excel

时间:2021-01-16 14:17:55

大多数情况下,当我们说将图表导出到Excel时,意思是将Chart当成图片导出到Excel中。如果是这样,你可以参考帮助文档中保存和导出C1Chart章节

不过,也有另一种情况,当你想把图表中的数据用文本格式导出到Excel时,应该怎么办?本文就是针对这个需求提供的解决办法,将C1Chart绑定的数据导出到Excel表单中。

绑定Chart数据

关于Chart数据绑定,你可以参考文档。本文Demo中使用如下代码进行数据绑定:

            DataTable dTable = new DataTable();
dTable.Columns.Add("时间");
dTable.Columns.Add("销量"); DataRow dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "一月", };
dTable.Rows.Add(dRow); dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "二月", };
dTable.Rows.Add(dRow); dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "三月", };
dTable.Rows.Add(dRow); dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "四月", };
dTable.Rows.Add(dRow); dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "五月", };
dTable.Rows.Add(dRow); dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "六月", };
dTable.Rows.Add(dRow); dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "七月", };
dTable.Rows.Add(dRow); dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "八月", };
dTable.Rows.Add(dRow); dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "九月", };
dTable.Rows.Add(dRow); dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "十月", };
dTable.Rows.Add(dRow); dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "十一月", };
dTable.Rows.Add(dRow); dRow = dTable.NewRow();
dRow.ItemArray = new object[] { "十二月", };
dTable.Rows.Add(dRow); c1Chart1.Data.ItemNameBinding = new Binding("时间");
c1Chart1.Data.Children.Add(new DataSeries() { ValueBinding = new System.Windows.Data.Binding("销量"), Name = "销量" });
c1Chart1.Data.ItemsSource = dTable.DefaultView;
c1Chart1.ChartType = ChartType.Column; c1Chart1.View.AxisY.AutoMin = false;
c1Chart1.View.AxisY.Min = ;
c1Chart1.View.AxisY.AutoMax = false;
c1Chart1.View.AxisY.Max = ;
c1Chart1.View.AxisY.MajorUnit = ;

将C1Chart数据导出到Excel

导出Chart数据

现在看来,除了遍历Chart Element获取数据,没有别的办法可以导出Chart数据了。通过AxisX UIElement集合中拿到TextBlock元素并读取它的“Text”属性以获得标签轴的数据。同样的,检索Bar Elements获得每个系列数据然后读取“DataPoint”属性。实际执行请参考如下代码:

public C1.Silverlight.Excel.C1XLBook ExportChartExcel(C1.Silverlight.Chart.C1Chart _chart)
{
C1XLBook wb = new C1XLBook();
wb.Sheets.Add("Chart Data"); int _row = ; IList<DependencyObject> _itemNameList = new List<DependencyObject>();
VTreeHelper.GetChildrenOfType((_chart.View.AxisX as Panel), typeof(TextBlock), ref _itemNameList); wb.Sheets[][, ].Value = "Month";
foreach (var _itemNameObject in _itemNameList.ToList())
{
wb.Sheets[][_row, ].Value = (_itemNameObject as TextBlock).Text;
_row += ;
} foreach (DataSeries _dataSeries in _chart.Data.Children)
{
_row = ;
wb.Sheets[][_row - , ].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[][_row, ].Value = (cvl as Bar).DataPoint.Value;
_row += ;
}
} return wb;
}

将C1Chart数据导出到Excel

以上只是一个实现办法,仅供参考。我相信程序猿们在面对不同需求时,还会有各自的修改和实现办法。根据不同的要求,获取不同的值。完整示例请参考如下Demo: