Setting the title of an Excel chart programmatically

How do I set the title of a chart using C # and Interop?

Actually trying:

            Excel.Range chartRange;

            Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlsSheet.ChartObjects(Type.Missing);
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
            Excel.Chart chartPage = myChart.Chart;

            chartRange = xlsSheet.Range[xlsSheet.Cells[1, 1], xlsSheet.Cells[ar.GetLength(0), ar.GetLength(1)]];
            chartPage.SetSourceData(chartRange, Excel.XlRowCol.xlRows);
            chartPage.ChartType = Excel.XlChartType.xl3DColumn;
            chartPage.Location(Excel.XlChartLocation.xlLocationAsNewSheet, oOpt);

            chartPage.HasTitle = true;
            chartPage.ChartTitle.Text = "HeaderText";

      

Providing sweet

"System.Runtime.InteropServices.COMException"

HRESULT error E_FAIL was returned from a COM component call

+3


source to share


2 answers


Found the answer myself.



Just set the header before the chartPage file is filled with information.

+3


source


Instead, chartPage.HasTitle = true;

another way is to apply some chart layout containing a title field before setting the title. In case of xl3DColumn Chart:



chartPage.ApplyLayout (1)
chartPage.ChartTitle.Text = "HeaderText"

      

0


source







All Articles