2

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.

1 Answer 1

1

Gulp.

I've seen that 0x800A03EC error plenty of times, and it can mean anything...

You might want to try this...

  • Grab a copy of this free C# library which creates .xlsx file using the OpenXML library, rather than VSTO. It uses the OpenXmlWriter library to write to the file, so if there's a lot of data, you won't get out-of-memory issues.

  • Create your Excel file by populating your ds DataSet as before, then using one line of code:

    CreateExcelFile.CreateExcelDocument(ds, "YourExcelFilename.xlsx");

If you don't want to go down this route, I'd recommend you dispose of your objexcelapp variable. This is a COM object which might remain open/in-use if you don't specifically kill it.

if (objexcelapp != null)
    Marshal.ReleaseComObject(objexcelapp);

We've had loads of VSTO problems over the years, and now, use it as little as possible.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.