I'm creating a windows service that will create excel sheets, I've used Microsoft.Office.Interop.Excel but got this Exception
Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE
I've attached a photo here
public void insertIntoSheet(string Name, DataTable dt)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
//MessageBox.Show("Excel is not properly installed!!");
return;
}
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
for (int i = 1, j = 0; j < dt.Rows.Count; i++, j++)
{
xlWorkSheet.Cells[i, 1] = dt.Rows[j][0].ToString();
xlWorkSheet.Cells[i, 2] = dt.Rows[j][1].ToString();
xlWorkSheet.Cells[i, 3] = dt.Rows[j][2].ToString();
xlWorkSheet.Cells[i, 4] = dt.Rows[j][3].ToString();
xlWorkSheet.Cells[i, 5] = dt.Rows[j][4].ToString();
xlWorkSheet.Cells[i, 6] = dt.Rows[j][5].ToString();
}
//xlWorkSheet.Cells[1, 1] = "Sheet 1 content";
xlWorkBook.SaveAs(String.Format("d:\\{0}.xls", Name), Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
Class_Library.WriteErrorLog("Inserting the sheet method finished");
//MessageBox.Show("Excel file created , you can find the file d:\\csharp-Excel.xls");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
Class_Library.WriteErrorLog(ex.Message);
//MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
NOTES: I've used the same code as a test in windows application and it worked as expected.
I've tried to write Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
and also tried to use the Name Excel only like Excel.Application xlApp = new Excel.Application();
Try Edit DCOMConfig
1.In DCOMCNFG, right click on the My Computer and select properties.
2.Go to Component /MyComputer/DCOMConfig
3.Go to Microsoft Excel Application property Security
4.In launch and Activation Permissions, click "Custamize" and add Network Service to it and give it "Local launch" and "Local Activation" permission. give same As Acess Permissions and cofiguration Permissions Press OK and thats it. You can run my application now.
Go to Excel's options / trust center / Macro settings and check "Trust access to the VBA project object model"
User contributions licensed under CC BY-SA 3.0