3
Answers

Reading excel using windows service

Toral Kapadia

Toral Kapadia

15y
10.3k
1

Hi
I am developing a windows service which sends mail to the employees. It reads data from the excel sheets. I had developed windows applicaion for the same and it works fine. I converted this code in to windows service and now it is giving me error as "System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.
at EMailSendingService.EMailSendingService.OpenExcelWorkbook(String fileName)
at EMailSendingService.EMailSendingService.GetExcelSheetNames(String strFilePath)."
I gave the rights to the excel application in registry and also changed the settings in the DCOM configuration. But still no luck.
It reaches till OpenWorkbook function, enters in it and then gives the exception.

Attaching the code:

private string GetExcelSheetNames(string strFilePath)
{
WriteError("before GetExcelSheetNames");
string strTemp = string.Empty;
try
{
OpenExcelWorkbook(strFilePath);
_sheet = (Excel.Worksheet)_sheets[1];
strTemp = _sheet.Name + "$";
Kill(_sheet);
_book.Close(false, Type.Missing, Type.Missing);
Kill(_book);
_app.Quit();
Kill(_app);
}
catch (Exception ex)
{
WriteError(ex.ToString());
}
finally
{
if (_sheet != null)
_sheet = null;
if (_sheets != null)
_sheets = null;
if (_book != null)
_book = null;
if (_books != null)
_books = null;
if (_app != null)
_app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
WriteError("after GetExcelSheetNames");
return strTemp;
}


protected void OpenExcelWorkbook(string fileName)
{
WriteError("before OpenExcelWorkbook");
_app = new Excel.Application();
if (_book == null)
{
_books = _app.Workbooks;
//_book = _books.Open(fileName, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, 0, 0, true);
_book = _books.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
_sheets = _book.Worksheets;
}
WriteError("after OpenExcelWorkbook");
}

This is the log file:

System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.
at EMailSendingService.EMailSendingService.OpenExcelWorkbook(String fileName)
at EMailSendingService.EMailSendingService.GetExcelSheetNames(String strFilePath)
after GetExcelSheetNames

Exception has occured: System.Data.OleDb.OleDbException: Syntax error in FROM clause.
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.ExecuteReader()
at EMailSendingService.EMailSendingService.ReadPunchReport()

its very urgent.


please help me.


Answers (3)