Excel interop The server threw an exception. (Exception from HRESULT: 0x80010105 (charting in excel)

I am currently trying to chart my S&P, M2, CC data in excel, through C#. However my code keeps crashing at this line below and throwing this kind of error (The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)))

Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10,80,300,250);// this are just test numbers

The first thing I did was create a fourth sheet in my excel workbook, however for some reason my code places my sheet right before my active sheet, so it looks "Chart 4" "Something 1" "Something 2" "Something 3". Not sure if this is anything to worry about, but just throwing that out there.

Here is all my code for this function, I wanted to throw this code in a class of its own, a class design to handle the creation of the excel chart but out of desperation decided to move it back to my class where I handle the creation of the excel doc, thinking that would work but it was all for nothing.



public void createChart()

{

Excel.Range chartRange;

Excel.ChartObjects xlCharts = (Excel.ChartObjects)getWorkSheet(4).ChartObjects(misValue);

Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10,80,300,250); <-------- Exception thrown here

Excel.Chart chartPage = myChart.Chart;

chartPage.ChartType = Excel.XlChartType.xlLine;

chartRange = worksheet.get_Range("B4","B100");

chartPage.SetSourceData(chartRange,Excel.XlRowCol.xlColumns);

}

Also I have read somewhere that someone created the new sheet, than created the chart on that sheet, but switch back to what I am guessing is the active sheet to define the range for the chart, not sure why that would work. Every example I've looked at has the same line of code as me, so it is most likely dealing with the COM and I am not very familiar with.

I've also read that it might have something to do with the GC, but I don't see how that is possible considering where it broke at.


Thanks for any help, I've been on this problem for almost a week now. If you need more of my code to figure help solve the problem I will be glad to post as much of it as you need.
 
Thanks, for taking time out to help with this issue. I've already narrowed it down to the COM, but because I am getting exceptions "out the hole" it hard to figure out what is causing the certain exception to be thrown. I spent days looking at a lot of the information you have sent and have had no luck. The SAME error can can be thrown on different issues not related to what I am doing.
 

alain

Older and Wiser
There is another solution. Ditch Excel and C# altogether. You shouldn't be dealing with COM. it's one of the worst things created by Microsoft.
 
I wish that this was possible, but the firm I am interning for wanted all of the data to be fed in and calculated through excel from c#.
 

Daniel Duffy

C++ author, trainer
I think you are using .NET 3.0 which does not support optional parameters. Use MISSING parameter as the test case here shows. Furthermore, you need to cast the C# 'Object' to the correct type.

BTW you don't need COM explicitly because .NET wrappers are created automatically. In that sense Excel integration is a lot easier than before.

http://www.datasimfinancial.com/forum/viewtopic.php?t=20



I

Excel.ChartObjects xlCharts = (Excel.ChartObjects)getWorkSheet(4).ChartObjects(misValue);

Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10,80,300,250); <-------- Exception thrown here



II

ch.ChartWizard(ws.get_Range(String.Format("A1:B{0}", i), Type.Missing), Excel.XlChartType.xlCylinderCol, 1,
Excel.XlRowCol.xlColumns, 1, 0, true, "Chart with values of each row",
"Rows", "Value", "Extra Title");

// Create chart on same worksheet and format the chart using the chart wizard.
ch=(ws.ChartObjects(Type.Missing) as Excel.ChartObjects).Add(150, 0, 400, 400).Chart;
 
Avoid Excel Interop

Excel Interop has many issues, especially when used in server environment, like scalability, security and resource management issues.

To avoid these subtle issues, I recommend you take a look at this Excel .NET library.

Here is a sample Excel C# code how to export DataGrid to Excel from your Excel ASP.NET application:

C++:
// Get DataTable that DataGrid is bound to.
var dataTable = (DataTable)dataGrid.DataSource;

// Create new ExcelFile.
var ef = new ExcelFile();
// Add new worksheet to the file.
var ws = ef.Worksheets.Add(dataTable.TableName);
// Insert the data from DataTable to the worksheet starting at cell "A1".
ws.InsertDataTable(dataTable, "A1", true);

// Stream file to browser.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls");
ef.SaveXls(Response.OutputStream);
Response.End();
 
Top