I've created Windows service which needs to create Excel after every 2 hrs. But its is giving me an error as follows
Exception from HRESULT: 0x800A03EC
First Time it creates excel file.but 2nd time it gives error. tried many things but failed. please help me out.
Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005.
this error also comes after first error came.
public void WriteExcel()
{
string ExcelGen = "ExcelGen";
try
{
string fileNm = DateTime.Now.ToString("dd-MM-yyyy_HH") + ".xls";
string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads\\" + fileNm;
string ServiceDbName = ConfigurationManager.AppSettings["ServiceDBName"].ToString();
string ServiceLMName = ConfigurationManager.AppSettings["ServiceTable"].ToString();
int Cnt = Service1.Counter;
Service1.AddLog("EXCEL STEP 1");
Microsoft.Office.Interop.Excel.Application objexcelapp = new Microsoft.Office.Interop.Excel.Application();
objexcelapp.Application.Workbooks.Add(Type.Missing);
objexcelapp.Columns.ColumnWidth = 25;
Service1.AddLog("EXCEL STEP 1.1");
MySqlConnection Conn = new MySqlConnection(ConfigurationManager.AppSettings["Conn"].ToString());
MySqlCommand inCmd = new MySqlCommand("select HT_LeadCode as 'LeadCode',right(lead_phone1,10) as 'Mobile',idg_fnc_GetDispositionDescription(lead_service_id, lead_last_dial_status) as 'Status' from " + ServiceDbName + "." + ServiceLMName + " where lead_status ='F' and HT_LeadCode <> '' and ifnull(HT_UpldFlag,'N') = 'N'", Conn);
Conn.Open();
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(inCmd);
da.Fill(ds);
Service1.AddLog( "EXCEL STEP 2");
string leadCodes = "";
foreach (System.Data.DataTable table in ds.Tables)
{
for (int i = 1; i < table.Columns.Count + 1; i++)
{
Service1.AddLog(" i : " + i.ToString());
objexcelapp.Cells[1, i] = table.Columns[i - 1].ColumnName;
}
for (int j = 1; j < table.Rows.Count+1; j++)
{
for (int k = 1; k < table.Columns.Count+1; k++)
{
Service1.AddLog("j & k : " + j.ToString()+ " & " + k.ToString());
objexcelapp.Cells[j + 2, k + 1] = table.Rows[j].ItemArray[k].ToString();
if(k==0)
leadCodes += table.Rows[j].ItemArray[k].ToString() + ",";
}
}
}
Service1.AddLog("LeadCodes : "+leadCodes);
leadCodes = leadCodes.Substring(0, leadCodes.Length - 1);
Service1.AddLog("LeadCodes : " + leadCodes);
inCmd = new MySqlCommand("update " + ServiceDbName + "." + ServiceLMName + " set HT_UpldFlag = 'Y' where lead_status ='F' and HT_LeadCode <> '' and ifnull(HT_UpldFlag,'N') = 'N' ", Conn);
inCmd.ExecuteNonQuery();
Service1.AddLog( "EXCEL STEP 3");
Service1.AddLog( "'" + path + "'" + " File is Created");
objexcelapp.ActiveWorkbook.SaveCopyAs(path);
objexcelapp.ActiveWorkbook.Saved = true;
objexcelapp.Quit();
Conn.Close();
Service1.AddLog( "EXCEL STEP 4");
Service1.AddLog( "UPLOAD THREAD STARTING");
Activity act = new Activity();
act.Upload();
}
catch (Exception ex)
{
Service1.AddLog( "WriteExcel Err : " + ex.Message);
}
}
When I Run service, very first time it creates excel file but when the interval takes place. it gives above mentioned error.