Hi. I'm trying to set the size and position of a chart in a worksheet, se code example further down. I get an unhandeled exception then I try to access the ChartArea.Top - property within the chart. How can this be done?
C# with VS 2005, .NET2.0, Office 2003.
public override void ExcelReport() { System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); Excel.Application excel = new Excel.Application(); if (excel != null) { // Create worksheet 1 excel.Application.DisplayAlerts = true; excel.Application.WindowState = Excel.XlWindowState.xlMaximized; Excel.Workbook wb = excel.Workbooks.Add(System.Type.Missing); Excel.Worksheet ws1 = (Excel.Worksheet)wb.Worksheets[1]; ws1.Name = "WS1"; for (int i = 1; i <= 10; i++) { ws1.Cells[i, 1] = i; ws1.Cells[i, 2] = i * i; }
// Create chart Excel.Chart ch1 = (Excel.Chart)excel.Charts.Add(Type.Missing, ws1, Type.Missing, Type.Missing); ch1.ChartType = Excel.XlChartType.xlColumnClustered; ch1.HasLegend = false; ch1.Name = "Graph"; Excel.Series s = (Excel.Series)ch1.SeriesCollection(1); s.XValues = ws1.get_Range(ws1.Cells[1, 1], ws1.Cells[10, 1]); s.Values = ws1.get_Range(ws1.Cells[1,2], ws1.Cells[10, 2]);
// Create worksheet 2 Excel.Worksheet ws2 = (Excel.Worksheet)wb.Worksheets.Add(Type.Missing, ws1, Type.Missing, Type.Missing); ws2.Name = "WS2"; // Move chart to worksheet 2 ch1.Location(Excel.XlChartLocation.xlLocationAsObject, "WS2"); // Locate chart within worksheet double top = ch1.ChartArea.Top; /* Crashes here, message: System.Runtime.InteropServices.COMException was unhandled Message="Exception from HRESULT: 0x800401A8" Source="Microsoft.Office.Interop.Excel" ErrorCode=-2147221080 StackTrace: at Microsoft.Office.Interop.Excel._Chart.get_ChartArea() at Fallodd_data.Fallodd_dynatest.ExcelReport() in C:\Documents and Settings\einarv\My Documents\Visual Studio 2005\Projects\Fallodd_data\Fallodd_data\Fallodd_data.cs:line 883 * . * . at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() */
} }
|
|