Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add the Chart sheet in Excel using C#

Tags:

c#

excel

I have created the sheet1 and populated some data in the sheet, using the data from sheet1 i want to create a chart sheet with ploting the data

try
{
    app = new Excel.Application();
    app.Visible = true;
    workbook = app.Workbooks.Add(1);
    worksheet = (Excel.Worksheet)workbook.Sheets[1];

    PopulateDateInExcel(pathtologsfolder, startdate, enddate);
    // create a chart

    Excel.Range chartRange;
    object misValue = System.Reflection.Missing.Value;
    Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
    Excel.ChartObject myChart = (Excel.ChartObject)workbook.Charts[2];
    Excel.Chart chartPage = myChart.Chart;
    chartRange = worksheet.get_Range("AN1", "AP6");
    chartPage.SetSourceData(chartRange, misValue);
    chartPage.ChartType = Excel.XlChartType.xl3DLine; 
}
catch (Exception e)
{
    //Console.Write("Error");
}
finally
{

}

Thanks in advance, Excel Automation

like image 954
User123 Avatar asked Aug 14 '12 17:08

User123


1 Answers

Try this (UNTESTED)

Excel.ChartObject myChart = (Excel.ChartObject)charts.Add(10, 70, 250, 250);

instead of

Excel.ChartObject myChart = (Excel.ChartObject)workbook.Charts[2];

and then once your chart is created, move it to a chart sheet using this code

chart.Location(XlChartLocation.xlLocationAsNewSheet, Type.Missing);
like image 180
Siddharth Rout Avatar answered Oct 28 '22 22:10

Siddharth Rout