簡體   English   中英

RPC服務器不可用。 (來自HRESULT的異常:0x800706BA)-Excel

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

我試圖使用asp.net c#將數據寫入excel模板文件。 但是我得到了例外。

ExceptionType:“ 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多行。

不能完全在您的開發道路上搖擺不定,但是我發現互操作庫非常不可靠。 我在ClosedXML方面取得了更大的成功。 我每天寫成千上萬的行,並且從未遇到過與ClosedXML相關的問題。 https://github.com/ClosedXML/ClosedXML

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM