如何在没有安装MS Office的机器上使用Microsoft.Office.Interop.Excel? [英] How to use Microsoft.Office.Interop.Excel on a machine without installed MS Office?

查看:221
本文介绍了如何在没有安装MS Office的机器上使用Microsoft.Office.Interop.Excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



它在开发机器上运行正常,但是当我尝试在服务器上部署它我收到一个错误:


无法加载文件或程序集的office,版本= 14.0.0.0,
文化=中性,PublicKeyToken = 71e9bce111e9429c'或其
依赖之一


我明白,当MS Office没有安装机器。
客户不想在服务器上以任何价格安装和购买MS Office。



我在开发者机器上安装可再分发主要互操作程序集建议在这里: http://forums.asp.net/t/1530230.aspx/1
并再次编译我的项目。



代码示例:

 code> 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; (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异常(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);
}
返回true;
}

一个例外


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


发生在行

  app = new Excel.Application(); 

任何人都可以建议如何使此功能成功工作?

解决方案

您不能在没有安装ms Office的情况下使用Microsoft.Office.Interop.Excel。



只需在google中搜索一些库,可以修改xls或xlsx:




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.

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

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.

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

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)).

occurs on the line

app = new Excel.Application();

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

解决方案

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

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

这篇关于如何在没有安装MS Office的机器上使用Microsoft.Office.Interop.Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆