来自HRESULT的异常:0x800A03EC创建Excel文件时出错

时间:2015-08-13 13:38:19

标签: c# excel excel-interop

我已经创建了Windows服务,需要在每2小时后创建一次Excel。 但它给我一个错误如下

Exception from HRESULT: 0x800A03EC

首次创建excel文件。但第二次出现错误。 尝试了许多事情但失败了。请帮帮我。

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005.

在第一次出现错误后也会出现此错误。

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);
        }
    }

当我运行服务时,它第一次创建excel文件但是在间隔发生时。它给出了上述错误。

1 个答案:

答案 0 :(得分:1)

杯。

我已经看到0x800A03EC错误很多次了,它可能意味着什么......

你可能想试试这个......

  • 获取此free C# library的副本,该副本使用创建.xlsx文件 OpenXML库,而不是VSTO。它使用OpenXmlWriter库来写入文件,因此如果有大量数据,不会出现内存不足问题。

  • 通过像以前一样填充ds DataSet来创建Excel文件,然后使用一行代码:

    CreateExcelFile.CreateExcelDocument(ds,“YourExcelFilename.xlsx”);

如果您不想沿着这条路走下去,我建议您处理objexcelapp变量。这是一个COM对象,如果你没有专门杀死它,它可能会保持打开/使用状态。

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

多年来我们遇到了大量的VSTO问题,现在,尽可能少地使用它。

希望这有帮助。