1

我正在使用 Excel 互操作创建一个 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);

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

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

有什么想法吗?

4

0 回答 0