asp excel错误80040154

时间:2013-01-04 16:07:33

标签: c# asp.net excel com

我的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文件时,我遇到了这个问题,但是当我使用本地版本运行debbugging模式时(代码工作),我没有。我公司的所有工作站都在计算机上安装了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,我应该能够像这段代码一样使用它吗?

2 个答案:

答案 0 :(得分:4)

Interop is not supported in sever-scenarios by MS

有许多选项可以在服务器上没有Interop /安装Excel的情况下读取/编辑/创建Excel文件:

MS提供免费的OpenXML SDK V 2.0 - 请参阅http://msdn.microsoft.com/en-us/library/bb448854%28office.14%29.aspx(仅限XLSX)

这可以读取+写入MS Office文件(包括Excel)。

另一个免费选项见http://www.codeproject.com/KB/office/OpenXML.aspx(仅限XLSX)

如果你需要更多像处理旧的Excel版本(如XLS,不仅仅是XLSX),渲染,创建PDF,公式等,那么有不同的免费和商业库,如ClosedXML(免费,仅限XLSX), EPPlus(免费,仅限XLSX),Aspose.CellsSpreadsheetGearLibXLFlexcel等。

答案 1 :(得分:0)

由于我的实习本周结束(是的,有点迟到才能找到类似的东西),我现在无法处理任何新事物,为时已晚,所以我只是还原更改并使用之前的创建Excel的过程文件(虽然我已经改进了。)

我相信,修复此问题的方法可能是授予从服务器使用Interop程序集然后调用用户Excel程序的权限,但我无法设置权限(它没有工作,不知道为什么)。现在为时已晚,我会做一些维护并确保项目的连续性。

感谢你们的答案。