Excel C# Com 图表多系列问题

发布于 2024-08-05 14:51:04 字数 1926 浏览 6 评论 0原文

请有人帮我解决这个问题,因为这让我发疯!

我正在实用地使用 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;

       }

Please can someone help me with this as it's driving me nuts!

I'm creating an excel chart using C# and the COM interface pragmatically.

I've created the chart using the chart wizard.

I want to then add more series to this chart. I can add the series but the extra data is on new columns and they are not automatically created.

Am I going about this the wrong way?

Add Chart:

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;
    }

And to add series:

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;

       }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最好是你 2024-08-12 14:51:04

尝试这样的事情。它对我来说非常有效。

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";

等等。

try something like this. It works for me pretty well.

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;

you can then change parameters of each series individually for example:

series1.Name = "some name";

and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文