Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用C#Excel互操作设置饼图标题时出错_C#_Excel_Charts_Interop - Fatal编程技术网

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

尝试使用C#Excel互操作设置饼图标题时出错,c#,excel,charts,interop,C#,Excel,Charts,Interop,我正在使用Excel互操作创建Excel饼图。图表的创建是正确的,但是当我去设置标题时,我得到一个运行时异常,它表示:“这个对象没有标题。” 以下是我正在使用的代码: Excel.ChartObjects dayTypeWorksheetCharts = (Excel.ChartObjects)dayTypeWorksheet.ChartObjects(Type.Missing); int dayTypeDistributionChartLeft = 10; int da

我正在使用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);
当我注释掉设置图表标题的行时,错误就会消失,“图表标题”就是图表的标题

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


你知道发生了什么吗?

我想你必须先将
HasTitle
属性设置为true,否则没有帮助。已尝试添加命令“daytypedributionchartpage.HasTitle=true;”虽然该命令本身不会导致异常,但取消注释设置图表标题文本的命令仍然会导致异常。