尝试使用C#Excel Interop设置饼图标题时出错

时间:2013-04-06 21:53:54

标签: c# excel charts interop

我正在使用Excel Interop创建Excel饼图。正在正确创建图表,但是当我设置标题时,我得到一个运行时异常,指出:“此对象没有标题。”。

以下是我正在使用的代码:

 Excel.ChartObjects dayTypeWorksheetCharts = (Excel.ChartObjects)dayTypeWorksheet.ChartObjects(Type.Missing);

     int dayTypeDistributionChartLeft = 10;
     int dayTypeDistributionChartTop = 10;
     int dayTypeDistributionChartWidth = 700;
     int dayTypeDistributionChartHeight = 500;

     Excel.ChartObject dayTypeDistributionChart = (Excel.ChartObject)dayTypeWorksheetCharts.Add(
        dayTypeDistributionChartLeft,
        dayTypeDistributionChartTop,
        dayTypeDistributionChartWidth,
        dayTypeDistributionChartHeight);

     Excel.Chart dayTypeDistributionChartPage = dayTypeDistributionChart.Chart;
     dayTypeDistributionChartPage.ChartType = Excel.XlChartType.xlPie;

     Excel.SeriesCollection seriesCollection = (Excel.SeriesCollection) dayTypeDistributionChartPage.SeriesCollection(Type.Missing);
     Excel.Series series1 = seriesCollection.NewSeries();

     Excel.Range startingLabelCell = (Excel.Range)dayTypeWorksheet.Cells[2, DayTypeWorksheetColumnIndices.title];
     Excel.Range endingLabelCell = (Excel.Range)dayTypeWorksheet.Cells[10, DayTypeWorksheetColumnIndices.title];
     Excel.Range labelRange = (Excel.Range)dayTypeWorksheet.get_Range(startingLabelCell, endingLabelCell);

     Excel.Range startingDataCell = (Excel.Range)dayTypeWorksheet.Cells[2, DayTypeWorksheetColumnIndices.frequencyPercentage];
     Excel.Range endingDataCell = (Excel.Range)dayTypeWorksheet.Cells[10, DayTypeWorksheetColumnIndices.frequencyPercentage];
     Excel.Range dataRange = (Excel.Range)dayTypeWorksheet.get_Range(startingDataCell, endingDataCell);

     series1.XValues = labelRange;
     series1.Values = dataRange;
        dayTypeDistributionChartPage.ChartTitle.Text = "Day Type Distribution";
     series1.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, true, false, false, false, false, false, false, false, Type.Missing);

当我注释掉设置图表标题的行时,错误消失,“图表标题”是图表的标题。

谷歌搜索带我到这个link,但答案给了我一个不同的错误,并没有解决我的问题。

任何想法发生了什么?

0 个答案:

没有答案