从HRESULTexception:0x800A03EC错误创buildExcel文件时

我创build了Windows服务,需要每2小时创build一次Excel。 但是它给了我一个错误如下

Exception from HRESULT: 0x800A03EC 

第一次创buildexcel文件,但是第二次出错。 尝试了很多东西,但失败了 请帮助我。

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

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

咕嘟咕嘟。

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

你可能想试试这个…

  • 抓取这个免费的C#库的副本,它使用OpenXML库而不是VSTO创build.xlsx文件。 它使用OpenXmlWriter库来写入文件,所以如果有大量的数据,你不会得到内存不足的问题。

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

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

如果你不想走这条路,我build议你处理你的objexcelappvariables。 这是一个COM对象,如果你没有专门杀死它,它可能保持打开/正在使用中。

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

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

希望这可以帮助。