RPC 服务器不可用。 (来自 HRESULT 的异常:0x800706BA)- Excel

Posted

技术标签:

【中文标题】RPC 服务器不可用。 (来自 HRESULT 的异常:0x800706BA)- Excel【英文标题】:The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) - Excel 【发布时间】:2019-01-14 04:07:42 【问题描述】:

我尝试使用 asp.net c# 将数据写入 excel 模板文件。但我得到了以下异常。

异常类型:“System.Runtime.InteropServices.COMException” 消息:“RPC 服务器不可用。(来自 HRESULT 的异常:0x800706BA)”

有时它适用于少量行。我找不到问题所在。

try


int rowNumber = 5;  

DataSet ds = ClsSystem.getReviewResponse();     

Excel.Application myExcelApplication = null;
Excel.Workbook myExcelWorkbook = null ;
Excel.Worksheet myExcelWorkSheet = null;

//copy template
System.IO.File.Copy(excelFilePath, NewExcelFilePath);

myExcelApplication = new Excel.Application(); 
myExcelApplication.DisplayAlerts = false; 

myExcelWorkbook = (Excel.Workbook(myExcelApplication.Workbooks._Open(NewExcelFilePath, System.Reflection.Missing.Value,
            System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
            System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
            System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
            System.Reflection.Missing.Value, System.Reflection.Missing.Value)); // open the existing excel file

myExcelWorkSheet = (Excel.Worksheet)myExcelWorkbook.Worksheets[1]; 
myExcelWorkSheet.Name = "Test sheet";

for (int n = 0; n < ds.Tables.Count; n++)
    
        for (int i = 0; i < ds.Tables[n].Rows.Count; i++)
        

            myExcelWorkSheet.Cells[rowNumber, "A"] = ds.Tables[n].Rows[i]["HeaderID"].ToString();
            myExcelWorkSheet.Cells[rowNumber, "B"] = ds.Tables[n].Rows[i]["ResponseID"].ToString();
            myExcelWorkSheet.Cells[rowNumber, "C"] = ds.Tables[n].Rows[i]["Region"].ToString();
            myExcelWorkSheet.Cells[rowNumber, "D"] = ds.Tables[n].Rows[i]["Sector"].ToString();
            myExcelWorkSheet.Cells[rowNumber, "E"] = ds.Tables[n].Rows[i]["Facility"].ToString();
            myExcelWorkSheet.Cells[rowNumber, "F"] = ds.Tables[n].Rows[i]["Program"].ToString();
            myExcelWorkSheet.Cells[rowNumber, "G"] = ds.Tables[n].Rows[i]["AuditDate"].ToString();
            myExcelWorkSheet.Cells[rowNumber, "H"] = ds.Tables[n].Rows[i]["Auditor1"].ToString();

            rowNumber++;
        
        rowNumber++;
    

    try
        
            myExcelWorkbook.SaveAs(NewExcelFilePath, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
             System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive,
             System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                System.Reflection.Missing.Value, System.Reflection.Missing.Value); // Save data in excel


            myExcelWorkbook.Close(true, NewExcelFilePath, System.Reflection.Missing.Value); // close the worksheet
            myExcelApplication.Quit();


        
        catch (Exception ex)
        
            throw ex;
        
        finally
        
            if (myExcelWorkSheet != null) Marshal.ReleaseComObject(myExcelWorkSheet);

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

            if (myExcelApplication != null) Marshal.ReleaseComObject(myExcelApplication);
        
  
  catch (Exception ex)
    
        if (myExcelApplication != null)
        
            myExcelApplication.Quit(); // close the excel application
        

        throw ex;
    

我想一次写入超过 500 行。

【问题讨论】:

你应该总是ReleaseComObject你的COM对象以正确的顺序(移动finally块到try-catch块之外)。顺便说一句,catch (Exception ex) throw ex; 没用。 我无法解决最终转移到外部的问题。 这不是一个解决方案。这是一个建议。如果在第一个 try-catch 块中引发异常(例如:在您的一个 for 循环中) - 您的 COM 对象不会被释放。此外,如果在您的第​​二个 try-catch 块中引发异常 - 您调用已发布 COM 对象的 myExcelApplication.Quit()。因此,任何异常都会破坏您的应用程序。 哦,明白了。谢谢 【参考方案1】:

不要完全在您的开发道路上摇摆不定,但我发现互操作库非常不可靠。我在使用 ClosedXML 方面取得了更大的成功。我每天写数万行,从来没有遇到过与 ClosedXML 相关的问题。 https://github.com/ClosedXML/ClosedXML

【讨论】:

我很高兴听到您对 ClosedXML 没有任何问题,并且我同意 COM 互操作是一个地狱,但这究竟对这个问题有什么帮助呢? (这应该是评论,而不是答案) 答案是建议退出互操作并转到 ClosedXML。 建议不是答案,对吧?建议应该是评论。另外,由于这个答案仍然不被接受,我认为 ClosedXML 不是一个选项。让我再解释一下:例如,在现在之后的某个时间,有人会遇到同样的问题。然后有人会在谷歌上搜索“COMException HRESULT: 0x800706BA site:***.com”,发现这个问题有 1 个答案。如果有人也不能切换到 ClosedXML 怎么办?这个答案他还能接受吗?这就是为什么How do I write a good answer? 告诉我们,“不要那样做” 不是答案。

以上是关于RPC 服务器不可用。 (来自 HRESULT 的异常:0x800706BA)- Excel的主要内容,如果未能解决你的问题,请参考以下文章

Powershell - 调用远程 WMI 对象 - RPC 错误 HRESULT:0x800706BA(RPC 服务器不可用)

RPC 服务器不可用。 (HRESULT 异常:0x800706BA)连接到远程计算机时

Get-WmiObject : RPC 服务器不可用。 (HRESULT 异常:0x80070 6BA

WMI 的“访问被拒绝”异常

如何解决 消息过滤器指示应用程序正忙。 (来自 HRESULT 的异常:0x8001010A (RPC_E_SERVERCALL_RETRYLATER))

故障转移群集服务器上来自 SSIS 的“RPC 服务器不可用”错误