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

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

问题描述

我正在写与Excel文件运行的应用程序。
我需要一个功能来删除表。
我必须使用汇编Microsoft.Office.Interop.Excel.dll。

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:

未能加载文件或程序集办公,版本= 14.0.0.0,
  文化=中性公钥= 71e9bce111e9429c'或之一其
  依赖

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

据我了解,当一台机器上没有安装微软Office出现这个问题。
客户不想安装在服务器上买的MS Office不是不惜任何代价。

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.

我开发的计算机上安装可再发行主互操作程序集这里建议: HTTP://forums.asp。净/ T / 1530230.aspx / 1
并重新编译我的项目。

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样品:<​​/ P>

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;
}

例外

与CLSID检索COM类工厂组件
  {00024500-0000-0000-C000-000000000046}失败,原因是以下
  错误:80040154没有注册类(从HRESULT异常:
  0x80040154的(REGDB_E_CLASSNOTREG))。

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

发生就行了。

app = new Excel.Application();

任何人都可以就如何获得此功能工作的成功?

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

推荐答案

而无需安装微软Office无法使用的Microsoft.Office.Interop.Excel。

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

在谷歌刚刚寻找一些图书馆,它允许修改XLS或XLSX:

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

  • http://code.google.com/p/excellibrary/
  • http://simpleooxml.codeplex.com/ (only xlsx)

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

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