private void button4_Click(object sender, EventArgs e)
{
Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook oWB;
Excel.Worksheet oWS;
oWB = oXL.Workbooks.Add(Missing.Value);
oWS = (Excel.Worksheet)oWB.Worksheets.get_Item(1);
Excel.Range oResizeRange;
object misValueDelComp = System.Reflection.Missing.Value;
oResizeRange = oWS.get_Range("A1:C20", Missing.Value).get_Resize(Missing.Value,Missing.Value);
Excel.Chart chart = AddChart(oWB, " mychart ", "the chart title", Excel.XlChartType.xlColumnClustered, oResizeRange, Excel.XlRowCol.xlRows);
oWB.SaveAs("TestGraph.xls", Excel.XlFileFormat.xlWorkbookNormal, misValueDelComp, misValueDelComp, misValueDelComp, misValueDelComp, MyExcel.XlSaveAsAccessMode.xlExclusive, misValueDelComp, misValueDelComp, misValueDelComp, misValueDelComp, misValueDelComp);
}
public static Excel.Chart AddChart(Excel.Workbook workbook, string chartSheetName, string title, Excel.XlChartType chartType, Excel.Range dataRange, Excel.XlRowCol byRowOrCol)
{
Excel.Chart chart;
chart = (Excel.Chart)workbook.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
chart.ChartType = chartType;
chart.Location(Excel.XlChartLocation.xlLocationAutomatic, chartSheetName);
chart.SetSourceData(dataRange, byRowOrCol);
chart.HasTitle = true;
chart.ChartTitle.Text = title;
return chart;
}
|