在我的ASP.Net Web应用程序项目中,我已经用数据库中的一些数据填充现有的Excel 2003电子表格(.xls)并将其发送给用户/客户端。我正在使用Excel Interop。到目前为止,我已经能够填写电子表格并在Visual Studio 2013 / IIS-Express上的调试下运行它时打开它。
当我在Windows 2012上运行的IIS上发布相同的应用程序并通过客户端通过Internet访问它时,虽然服务器确实将文件发送到客户端,但它没有填充任何数据。我检查了服务器日志,发现抛出以下异常:
System.Runtime.InteropServices.COMException(0x80080005):
使用CLSID检索组件的COM类工厂 {00024500-0000-0000-C000-000000000046}由于以下原因而失败 错误:80080005服务器执行失败(来自HRESULT的异常: 0x80080005(CO_E_SERVER_EXEC_FAILURE))。在 System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)....
我已设置网站文件夹的权限,以允许以下帐户完全控制:
此外,在阅读相关内容时,我还为Excel应用程序设置了DCOM配置,以便为上述帐户设置以下权限:
应用程序设置为使用Windows身份验证(身份),并且从客户端计算机,我使用我的帐户凭据登录到应用程序。
以下是我用来填充Excel spreasheet的代码:
public bool FillExcel()
{
bool result = false;
try
{
string newName = "stage 2.xls";
if (!Directory.Exists(Server.MapPath("~") + @"\Files\xls\"))
Directory.CreateDirectory(Server.MapPath("~") + @"\Files\xls\");
if (System.IO.File.Exists(Server.MapPath("~") + @"\Files\xls\" + newName))
{
System.IO.File.Delete(Server.MapPath("~") + @"\Files\xls\" + newName);
}
System.IO.File.Copy(Server.MapPath("~") + @"\Files\stage2Copy.xls", Server.MapPath("~") + @"\Files\xls\" + newName);
List<Sheet1Model> sheet1ModelList = getSheet1Data(); // Model to fill the data from database to be filled into Excel
Application excelApplication = new Application();
Workbook wb = excelApplication.Workbooks.Open(Server.MapPath("~") + @"\Files\xls\" + newName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Worksheet sheet = (Worksheet)wb.Sheets["Sheet1"];
sheet.Select(Type.Missing);
Range excelRange = sheet.UsedRange;
int rNumber = 3;
string[] firstWorksheetHeaders = GetRange("A" + rNumber + ":HH" + rNumber + "", sheet);
int rn = 4; // start from row 4 in Excel
foreach (Sheet1Model sheetModel in sheet1ModelList)
{
sheet.Cells[rn, 1] = sheetModel.CustomerName
rn++;
}
String Path = Server.MapPath("~") + @"Files\xls\" + newName;
wb.SaveAs(Path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.Close();
Marshal.ReleaseComObject(sheet);
Marshal.ReleaseComObject(wb);
Marshal.ReleaseComObject(excelApplication);
result = true;
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch (Exception ex)
{
logger.Info(ex.ToString());
}
return result;
}
如何删除此错误并填写excel表?