如何使用C#在Excel中添加图表工作表

时间:2021-02-08 20:16:05

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

我已经创建了sheet1并在工作表中填充了一些数据,使用来自sheet1的数据我想创建一个图表工作表并绘制数据

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

在此先感谢Excel Automation

1 个解决方案

#1


2  

Try this (UNTESTED)

试试这个(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);

#1


2  

Try this (UNTESTED)

试试这个(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);