← 返回所有博客文章

大多数情况下,当我们说将图表导出到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[] { "一月", 2000 };
            dTable.Rows.Add(dRow);

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

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

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

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

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

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

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

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

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

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

            dRow = dTable.NewRow();
            dRow.ItemArray = new object[] { "十二月", 3232 };
            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 = 1000;
            c1Chart1.View.AxisY.AutoMax = false;
            c1Chart1.View.AxisY.Max = 8000;
            c1Chart1.View.AxisY.MajorUnit = 1000;

 

导出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 = 1;

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

            wb.Sheets[0][0, 0].Value = "时间";
            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;
        }

 

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

SL_Chart_DataExport.rar (5.63 kb)