Problem with Excel file used in C# application /*System.Runtime.InteropServices.COMException*/

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi. Ive made an application that puts data from sensors in an excel file. The method used for creating the excel file in C# is:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; class CreateExcelDoc
{
<span style="color:Blue; public Microsoft.Office.Interop.Excel.Application app = <span style="color:Blue; null;
<span style="color:Blue; public Microsoft.Office.Interop.Excel.Workbook workbook = <span style="color:Blue; null;
<span style="color:Blue; public Microsoft.Office.Interop.Excel.Worksheet worksheet = <span style="color:Blue; null;
<span style="color:Blue; public Microsoft.Office.Interop.Excel.Range workSheet_range = <span style="color:Blue; null;
<span style="color:Blue; object misValue = System.Reflection.Missing.Value;
<span style="color:Blue; public CreateExcelDoc()
{
createDoc();
}
<span style="color:Blue; public <span style="color:Blue; void createDoc()
{
<span style="color:Blue; try
{


app = <span style="color:Blue; new Microsoft.Office.Interop.Excel.Application();
app.Visible = <span style="color:Blue; false;
<span style="color:Blue; object misValue = System.Reflection.Missing.Value;
<span style="color:Blue; string Path = <span style="color:#A31515; @"C:UsersRaduDocumentsMonitorizare.xls";
workbook = app.Workbooks.Open(Path,
misValue,
misValue,
misValue,
misValue,
misValue,
misValue,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
misValue,
misValue,
misValue,
misValue,
misValue,
misValue,
misValue);

worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(misValue, misValue, misValue, misValue);



}
<span style="color:Blue; catch (Exception e)
{
app = <span style="color:Blue; new Microsoft.Office.Interop.Excel.Application();
app.Visible = <span style="color:Blue; false;
workbook = app.Workbooks.Add(1);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
}
<span style="color:Blue; finally
{

}
}
[/code]

I have no problem creating and adding the data to the excel. The problem is when I open the excel file and close it , while the application is running i get an error An unhandled exception of type System.Runtime.InteropServices.COMException
occurred in System.Windows.Forms.dll where the data adding should have been made. Does anybody know how to remedy this?
Thank you.

View the full article
 
Back
Top