2012-08-14 47 views
5

yo he creado al sheet1 y poblada algunos datos en la hoja, utilizando los datos de Sheet1 Quiero crear una hoja de gráfico con ploting los datosCómo agregar la hoja de gráfico en Excel usando C#

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 
{ 

} 

Gracias por adelantado, automatización de Excel

+1

¿Cuál es tu pregunta? –

+0

quiero crear una hoja Agregar un nuevo gráfico trazando los datos ..... Pero obteniendo una excepción al agregar la hoja de gráfico Excel.ChartObject myChart = (Excel.ChartObject) workbook.Charts [2]; – User123

Respuesta

2

Prueba esto (sin probar)

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

en lugar de

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

y luego una vez creada la tabla, moverlo a una hoja de gráfico que utiliza este código

chart.Location(XlChartLocation.xlLocationAsNewSheet, Type.Missing); 
Cuestiones relacionadas