C# 如何在没有安装 MS Office 的机器上使用 Microsoft.Office.Interop.Excel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11448197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:53:46  来源:igfitidea点击:

How to use Microsoft.Office.Interop.Excel on a machine without installed MS Office?

c#exceldllinterop

提问by John Wales

I'm writing an application which works with excel files. I need a feature to delete a sheet. I have to use an assembly Microsoft.Office.Interop.Excel.dll.

我正在编写一个适用于 excel 文件的应用程序。我需要一个功能来删除工作表。我必须使用程序集 Microsoft.Office.Interop.Excel.dll。

It's running fine on developer machine but when I try to deploy it on server I'm getting an error:

它在开发人员机器上运行良好,但是当我尝试在服务器上部署它时出现错误:

Could not load file or assembly 'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies

无法加载文件或程序集“office,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c”或其依赖项之一

I understand that problem occurs when MS Office is not installed on a machine. Customer don't want to install and buy MS Office on a server not at any price.

我了解当计算机上未安装 MS Office 时会出现问题。客户不想不惜任何代价在服务器上安装和购买 MS Office。

I install "Redistributable Primary Interop Assemblies" on developer machine as advised here: http://forums.asp.net/t/1530230.aspx/1and compile my project again.

我按照此处的建议在开发人员机器上安装“可再发行主互操作程序集”:http: //forums.asp.net/t/1530230.aspx/1并再次编译我的项目。

Code sample:

代码示例:

public bool DeleteSheet(string tableName)
{
    Excel.Application app = null;
    Excel.Workbooks wbks = null;
    Excel._Workbook _wbk = null;
    Excel.Sheets shs = null;

    bool found = false;

    try
    {
        app = new Excel.Application();
        app.Visible = false;
        app.DisplayAlerts = false;
        app.AlertBeforeOverwriting = false;

        wbks = app.Workbooks;
        _wbk = wbks.Add(xlsfile);
        shs = _wbk.Sheets;
        int nSheets = shs.Count;

        for (int i = 1; i <= nSheets; i++)
        {
            Excel._Worksheet _iSheet = (Excel._Worksheet)shs.get_Item(i);
            if (_iSheet.Name == tableName)
            {
                _iSheet.Delete();
                found = true;

                Marshal.ReleaseComObject(_iSheet);
                break;
            }
            Marshal.ReleaseComObject(_iSheet);
        }

        if (!found)
            throw new Exception(string.Format("Table \"{0}\" was't found", tableName));

        _wbk.SaveAs(connect, _wbk.FileFormat, Missing.Value, Missing.Value, Missing.Value,
        Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    }
    finally
    {
        _wbk.Close(null, null, null);
        wbks.Close();
        app.Quit();

        Marshal.ReleaseComObject(shs);
        Marshal.ReleaseComObject(_wbk);
        Marshal.ReleaseComObject(wbks);
        Marshal.ReleaseComObject(app);
    }
    return true;
}

An exception

一个例外

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

由于以下错误,检索具有 CLSID {00024500-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败:80040154 类未注册(来自 HRESULT 的异常:0x80040154 (REGDBREGD_E)CL)。

occurs on the line

在线上发生

app = new Excel.Application();

Can anyone advise on how to get this feature working successfully?

任何人都可以就如何使此功能成功运行提供建议吗?

采纳答案by user1519979

You can't use Microsoft.Office.Interop.Excel without having ms office installed.

没有安装 ms office 就不能使用 Microsoft.Office.Interop.Excel。

Just search in google for some libraries, which allows to modify xls or xlsx:

只需在 google 中搜索一些库,它允许修改 xls 或 xlsx:

回答by Philippe Grondier

If the "Customer don't want to install and buy MS Office on a server not at any price", then you cannot use Excel ... But I cannot get the trick: it's all about one basic Office licence which costs something like 150 USD ... And I guess that spending time finding an alternative will cost by far more than this amount!

如果“客户不想不惜任何代价在服务器上安装和购买 MS Office”,那么您就不能使用 Excel ......但我无法理解:这完全是一个基本的 Office 许可证,费用约为 150美元 ... 我猜花时间寻找替代品的成本将远远超过这个数额!

回答by Ashish

you can create a service and generate excel on server and then allow clients download excel. cos buying excel license for 1000 ppl, it is better to have one license for server.

您可以创建服务并在服务器上生成excel,然后允许客户端下载excel。cos 购买 1000 ppl 的 excel 许可证,最好有一个服务器许可证。

hope that helps.

希望有帮助。

回答by miro

Look for GSpread.NET. You can work with Google Spreadsheets by using API from Microsoft Excel. You don't need to rewrite old code with the new Google API usage. Just add a few row:

寻找 GSpread.NET。您可以使用 Microsoft Excel 中的 API 来使用 Google 电子表格。您无需使用新的 Google API 用法重写旧代码。只需添加几行:

Set objExcel = CreateObject("GSpreadCOM.Application");

app.MailLogon(Name, ClientIdAndSecret, ScriptId);

It's an OpenSource project and it doesn't require Office to be installed.

这是一个开源项目,不需要安装 Office。

The documentation available over here http://scand.com/products/gspread/index.html

此处提供的文档http://scand.com/products/gspread/index.html