How to add the Chart sheet in Excel using C#

c#, excel

Solution

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);

Problem

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

Original source