我想寫一個程序在Excel中通過C#創建圖表。由C#創建Excel圖表的例外情況
我寫的程序,它在win7pro和Office 2007
正常工作,但是當我在一個系統與Windows XP SP(3)和Office 2003執行程序,它拋出以下錯誤:
Unhandled Exception: System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC
at Microsoft.Office.Interop.Excel._Chart.ChartWizard(Object Source, Object Gallery, Object Format, Object PlotBy, Object CategoryLabels, Object SeriesLabels, Object HasLegend, Object Title, Object CategoryTitle, Object ValueTitle, Object
ExtraTitle)
at ConsoleApplication2.Report.createChart(Worksheet ws, String chartTitle, String xName, String yName)
at ConsoleApplication2.Report.createReport(List`1 data, String chartTitle, String xName, String yName)
at CreateExcelWorksheet.Main()
這對我創建圖表的代碼如下:
protected void createChart(Worksheet ws,string chartTitle,string xName,string yName)
{
ChartObjects chartObjs = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject chartObj = chartObjs.Add(100, 10, 300, 300);
Chart xlChart = chartObj.Chart;
Range rg;
rg = ws.get_Range(startCell,endCell);
xlChart.ChartWizard(rg, XlChartType.xlXYScatterSmooth, 1, XlRowCol.xlColumns, 0, 0, true, chartTitle, xName, yName, "");
}
和錯誤行是最後一個。
沒有人知道我該怎麼辦?
此外,當我用下面的行替換最後一行程序工作,但我需要設置一些屬性,如xName和yName和ChartTitle的圖表。有誰知道我該怎麼做?
xlChart.ChartType = XlChartType.xlXYScatterSmoothNoMarkers;
xlChart.SetSourceData(rg, Type.Missing);
謝謝
我應該怎麼做呢? – JGC 2010-05-13 17:20:36