开发者

exception in creating excel chart by C#

I'm trying to write a program to create a ch开发者_高级运维art in excel by C#.

I've wrote the program and it work correctly in win7pro and Office 2007

but when I execute the program in a system with windows XP sp(3) and office 2003 it throws below error:

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()

the code which create chart for me is below:

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, "");
    }

and the error line is the last one.

does anyone know what should I do?

in addition when I replace the last line with following lines the program works but I need to set some properties like xName and yName and ChartTitle for chart. does anyone know how can I do that?

xlChart.ChartType = XlChartType.xlXYScatterSmoothNoMarkers;
xlChart.SetSourceData(rg, Type.Missing);

thank you


Dude I had same problem with no full answer. You were on the right path and but you were missing some stuff.

Here is my solution to finally end this debate so that others won't have to suffer like I did. I won't go into detail on some of the objects type but it should be clear:

oChart.HasTitle = true;
oChart.ChartTitle.Text = "Chart Name";
oChart.ChartType = Excel.xlChartType.xlXYScatterSmooth;
oChart.SetSourceData(oRange, Excel.xlRowCol.xlColumns);

// This is the part you were missing
Excel.Axis xAxis = (Excel.Axis)oChart.Axes( Excel.xlAxisType.XlCatergory, Excel.xlAxisGroup.xlPrimary);
xAxis.HasTitle = true;
xAxis.AxisTitle.Text = "x";

Excel.Axis yAxis = (Excel.Axis)oChart.Axes( Excel.xlAxisType.XlValue, Excel.xlAxisGroup.xlPrimary);
yAxis.HasTitle = true;
yAxis.AxisTitle.Text = "y";

Good Luck


I guess you will have to use late binding to support both Office 2003 and 2007 since they have different versions of the Object Library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜