【发布时间】:2012-12-19 02:17:38
【问题描述】:
我的 asp 项目 (C#) 有问题。
我遇到以下错误:检索具有 CLSID {00024500-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败,原因是以下错误:80040154 尝试时抛出从服务器生成一个excel文件。我已经看过一些关于它的帖子,我不知道它们是否适用于我,也无法解决我的问题。让我给你看我的代码并解释一下。
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
private Excel.Application oXL;
private Excel._Workbook oWB;
private ExcelFile f;
protected void bExcel_Click(object sender, EventArgs e)
{
if (Session["mode"] == null)
Response.Redirect(accueil);
// Set the values
oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
f = new ExcelFile((Excel._Worksheet)oWB.ActiveSheet);
oXL.Visible = true;
/* displaying data from a gridview, non revelant here (?) */
oXL.Visible = true;
f = null;
oXL = null;
oWB = null;
}
以及错误的解释(第 225 行):
Erreur source:
Ligne 224 : // Set the values
Ligne 225 : oXL = new Excel.Application();
Ligne 226 : oWB = (Excel._Workbook) oXL.Workbooks.Add(Missing.Value));
Ligne 227 : f = new ExcelFile((Excel._Worksheet)oWB.ActiveSheet);
好的,所以当我尝试从服务器生成 excel 文件时遇到了这个问题,但当我使用本地版本运行调试模式时却没有(代码有效)。我公司的所有工作站都在计算机上安装了 MS Office Excel,但我正在使用的服务器上没有,我的问题是有没有不必在服务器上安装 Excell 的解决方案?
我尽量避免在服务器上安装 Excel,因为服务器不是来自我的服务,所以我想避免它(但如果它是唯一(和最好/安全)的解决方案,我会这样做)如果我能够。 我尝试将库复制到服务器上的程序集文件夹中(因为所有工作站都相同,所以引用应该工作?)但我找不到我必须授予允许从服务器访问库的权限(我希望我没有解释得太糟糕)。
提前致谢,
编辑:有一点我不明白,就是在之前已经写好的代码中,Excel 应用程序可以这样启动:
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=registrationData.xls");
// Pour les caractères spéciaux (é, à, etc...)
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
using (StringWriter sw = new StringWriter())
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table table = new Table();
TableRow tRow = new TableRow();
/* Adding each information of the grid using tRow.Cells.Add() */
table.RenderControl(htw);
// Response needs the Htmlwriter
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
这在没有安装 Excel 的情况下工作,因为(虽然我不确定)它只调用 Excel 应用程序,并用缓冲区 (StringWriter) 填充它。但我不想使用表格,因为表格使用起来很无聊,而且我已经使用 Interop 制作了文件...... 如果我能找到使用安装在服务器上的程序集的权限,并且知道所有工作站都有 Excel,我应该能够像这段代码一样使用它吗?
【问题讨论】:
-
IME,您必须在托管您的应用程序的机器上安装您调用其 COM 接口的应用程序。
-
否;没有 Excel 就不能使用 Excel。而且,在服务器上使用 Excel 是一个非常糟糕的主意。