正确使用get_range来制作图表

时间:2015-12-08 19:33:06

标签: c# excel-interop

我正在使用以下代码尝试收费并且它正在抛出一个com异常。我正试图制作一张图表。有什么明显的东西我的代码做错了吗?该错误在get_range行上抛出。

      static void makechart(int counter, int startcount, Worksheet sourcesheet)
    {
        Range chartrange;
        string rangestr = "G" + startcount.ToString() + ":G" + counter.ToString() + ",I" + startcount.ToString() + ":I" + counter.ToString();
        ChartObjects xlcharts =     (ChartObjects)sourcesheet.ChartObjects(Type.Missing);
        ChartObject myChart = (ChartObject)xlcharts.Add(10, 80, 300, 250);
        Chart chartPage = myChart.Chart;
        chartrange = objsheet.get_Range(rangestr,Type.Missing);
        //chartrange = sourcesheet.get_Range("G1", "B" + counter + 1);

        chartPage.SetSourceData(chartrange, Type.Missing);
        chartPage.ChartType = XlChartType.xlBarClustered;


    }

1 个答案:

答案 0 :(得分:0)

这应该有效:

替换:

string rangestr = "G" + startcount.ToString() + ":G" + counter.ToString() + ",I" + startcount.ToString() + ":I" + counter.ToString();

使用:

string range1 = "G" + startcount.ToString() + ":G" + counter.ToString();
string range2 = "I" + startcount.ToString() + ":I" + counter.ToString();

chartrange = objsheet.get_Range(rangestr,Type.Missing);

使用:

chartrange = objsheet.get_Range(range1,range2);