【发布时间】: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()。因此,任何异常都会破坏您的应用程序。 -
哦,明白了。谢谢
标签: c# asp.net excel interopservices