简体   繁体   English

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

[英]The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) - Excel

I tried to write data to a excel template file using asp.net c#. 我试图使用asp.net c#将数据写入excel模板文件。 But I got following exception. 但是我得到了例外。

ExceptionType: "System.Runtime.InteropServices.COMException" Message: "The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)" ExceptionType:“ System.Runtime.InteropServices.COMException”消息:“ RPC服务器不可用。(来自HRESULT的异常:0x800706BA)

Sometimes it works for small count of rows. 有时它适用于少量的行。 I cannot find what is the issue. 我找不到问题所在。

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

I want to write more than 500 rows at once. 我想一次写500多行。

Not to rock the boat on your development path entirely, but I find the interop libraries horribly unreliable. 不能完全在您的开发道路上摇摆不定,但是我发现互操作库非常不可靠。 I have had much greater success with ClosedXML. 我在ClosedXML方面取得了更大的成功。 I write tens of thousands of rows a day and have never had an issue related to ClosedXML. 我每天写成千上万的行,并且从未遇到过与ClosedXML相关的问题。 https://github.com/ClosedXML/ClosedXML https://github.com/ClosedXML/ClosedXML

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Runtime.InteropServices.COMException(0x800706BA):RPC服务器不可用。 (HRESULT异常:0x800706BA) - System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) WMI RPC服务器不可用。 (来自HRESULT的异常:0x800706BA) - WMI The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) RPC服务器不可用。 连接到远程计算机时(来自HRESULT的异常:0x800706BA) - The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) when connecting to remote computer 如何处理 System.Runtime.InteropServices.COMException (0x800706BA):RPC 服务器不可用。 (来自 HRESULT 的异常:0x800706BA) - How to handle System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) WMI:RPC服务器不可用。 (尝试连接到远程计算机时抛出HRESULT异常:0x800706BA) - WMI: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) throws when try to connect to remote machine LDAP 从域外重置密码。网络 C# 错误:RPC 服务器不可用。 (hresult 的异常:0x800706ba) - LDAP reset password from outside the domain network C# Error: RPC server is unavailable. (exception from hresult: 0x800706ba) 如何修复“CCertRequest::Submit:RPC 服务器不可用。0x800706ba”错误? - How to fix "CCertRequest::Submit: The RPC server is unavailable. 0x800706ba" error? 在错误0x800706BA之前捕获RPC锁定 - Catch RPC lock before error 0x800706BA 请阐明“服务器引发异常。 (来自HRESULT的异常:0x80010105(RPC_E_SERVERFAULT))” - Please clarify the meaning of “The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))” C# - Excel - Office Interop - 异常 HRESULT 0x80010100 (RPC_E_SYS_CALL_FAILED) - C# - Excel - Office Interop - Exception HRESULT 0x80010100 (RPC_E_SYS_CALL_FAILED)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM