Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Excel chart title programmatically

How to set a chart title with 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";

Giving sweet

"System.Runtime.InteropServices.COMException"

Error HRESULT E_FAIL has been returned from a call to a COM component

like image 733
zaimen Avatar asked Jul 24 '15 15:07

zaimen


2 Answers

Found the answer on my own.

Just set the title before the chartPage is filled with information.

like image 88
zaimen Avatar answered Oct 20 '22 01:10

zaimen


Instead of chartPage.HasTitle = true; another way is by applying some chart layout containing a Title field before setting the title. In the case of xl3DColumn Chart :

chartPage.ApplyLayout (1)
chartPage.ChartTitle.Text = "HeaderText"
like image 22
Nor El Malki Avatar answered Oct 20 '22 00:10

Nor El Malki