Excel C#Com Chart多系列问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Excel C#Com Chart多系列问题相关的知识,希望对你有一定的参考价值。

请有人帮我这个,因为它让我疯了!

我正在用C#和COM接口实用地创建一个excel图表。

我使用图表向导创建了图表。

我想在此图表中添加更多系列。我可以添加系列,但额外的数据是在新列上,它们不会自动创建。

我是以错误的方式来做这件事的吗?

添加图表:

public void MakeExcelChart(string startRange, string endRange, string chartTitle, string seriesName)
    {
        ExcelChart = (Excel.Chart)ExcelWBook.Charts.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        ExcelApp.Visible = true;
        ExcelChart.HasTitle = true;
        ExcelChart.ChartTitle.Text = chartTitle;

        ExcelRange = ExcelWSheet.get_Range(startRange, endRange);

        ExcelChart.ChartWizard(ExcelRange, Excel.XlChartType.xlColumnClustered, Missing.Value, Excel.XlRowCol.xlColumns, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

        ExcelChart.ApplyDataLabels(Microsoft.Office.Interop.Excel.XlDataLabelsType.xlDataLabelsShowBubbleSizes, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

        ExcelChart.ChartArea.Fill.OneColorGradient(Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal, 1, 1);

        GetSeriesCollection();

        ExcelSeries = ExcelSeriesCollection.Item(1);
        ExcelSeries.Name = seriesName;
    }

并添加系列:

public void AddSeries(string col1, string col2, string startRange, string endRange, string seriesName)
    {
        ExcelSeries = ExcelSeriesCollection.NewSeries();

        ExcelSeries.HasDataLabels = true;

        ExcelRange = ExcelWSheet.get_Range(col1+startRange, col1+endRange);

        ExcelSeries.XValues = ExcelRange;

        ExcelRange = ExcelWSheet.get_Range(col2+startRange, col2+endRange);

        ExcelSeries.Values = ExcelRange;

        ExcelChart.HasLegend = true;

        ExcelSeries.Name = seriesName;

       }
答案

尝试这样的事情。它非常适合我。

Excel.ChartObjects xlChart = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlChart.Add(1050, 865, 690, 265);
Excel.Chart chartPage = myChart.Chart;
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "title";
chartPage.HasLegend = true;

Excel.SeriesCollection oSeriesCollection = (Excel.SeriesCollection)myChart.Chart.SeriesCollection(misValue);
Excel.Series series1 = oSeriesCollection.NewSeries();
Excel.Series series2 = oSeriesCollection.NewSeries();
Excel.Series series3 = oSeriesCollection.NewSeries();

Excel.Range series1_range = xlWorkSheet.get_Range("start_range1","end_range1");
Excel.Range series2_range = xlWorkSheet.get_Range("start_range2","end_range2");
Excel.Range series3_range = xlWorkSheet.get_Range("start_range3","end_range3");

series1.Values = series1_range;
series2.Values = series2_range;
series3.Values = series3_range;

然后,您可以单独更改每个系列的参数,例如:

series1.Name = "some name";

等等。

另一答案

您需要扩展定义数据的Excel表。很好的例子是在http://blogs.msdn.com/vsod/archive/2009/06/15/creating-charts-in-word-and-powerpoint-using-newly-introduced-object-model-in-office-2007-service-pack-2.aspx

以上是关于Excel C#Com Chart多系列问题的主要内容,如果未能解决你的问题,请参考以下文章

Excel VBA Chart.Export

Excel VBA Chart.Export

将 JSON 解析为 Excel - LOOP

从 Excel 导入数据并在 Chart.js 中使用

excel散点图:如何用多组数据拟合一条曲线

旭日图(sunburst chart)绘制:R语言 & excel