By John Wales


2012-07-12 08:52:15 8 Comments

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?

4 comments

@miro 2016-05-30 13:01:28

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:

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

app.MailLogon(Name, ClientIdAndSecret, ScriptId);

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

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

@EAmez 2017-05-16 09:03:18

As I can see for the code you've posted, you must somehow log in the "Google Systems". So the alternative to install an Office Licence is register in Google. I know it's for free and that but... I don't like the idea to make my application log in Google Services.

@Ashish 2013-01-02 07:58:02

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.

hope that helps.

@kaushalparik27 2014-09-15 10:41:29

developing office automation in client server architecture is not recommended.

@user1519979 2012-07-12 08:59:48

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:

@Govert 2012-07-12 09:12:55

Yes - the interop assemblies just tell .NET how to call the Excel COM libraries. They don't have any functionality. Another popular one for reading/writing .xlsx files: closedxml.codeplex.com

@John Wales 2012-07-12 09:40:36

Thanks. I will try these libs.

@CZahrobsky 2014-08-08 17:09:41

As it happens, you cannot even BUILD a project which contains references to Microsoft.Office.Core without also installing Office. The other document-level Interop assembly references seem to compile but not run. My work-around has been to delete the explicit Microsoft.Office.Core references and replace them with with dynamic and var keywords which allow late references AFTER intellisense has done its job on the developer's machine.

@ehh 2016-01-27 15:42:13

I have office installed and I get the same issue. Can you please help?

@Philippe Grondier 2012-07-12 09:04:09

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!

@Christian Sauer 2013-01-02 08:25:00

Using Excel on a Server is pad practice. First: It is a unesscessary vulnerabillity. Second it is terribly slow, writing and reading documents take ages - great if you have more than one user... Third: Office is not intended for completely automated usage. There are highcups who are creating messageboxes etc. That is "not so good" on servers...

@Alan B 2013-02-20 16:34:54

"second it is terribly slow, writing and reading documents take ages" - Not in my experience. Also what if customers want to use Excel, MS Office in full or indeed any other software via thin client? It'll have to go on a server in that scenario.

@Jonas Söderström 2013-07-08 04:08:23

As a consultant I would find it very embarrasing to tell the client that they would have to install MS Office on their CI servers. It's not so much about the money as about the hassle and the WTF-factor. And it might not be only one server, perhaps they have 5,10,20 virtual CI servers. Then we are talking more money and administration costs than $150.

@strike_noir 2013-12-05 09:33:50

I agree with with @JonasSöderström it is not about considering single Office license

Related Questions

Sponsored Content

42 Answered Questions

[SOLVED] How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

  • 2008-09-29 22:30:28
  • mistrmark
  • 1043581 View
  • 1796 Score
  • 42 Answer
  • Tags:   c# .net excel file-io

41 Answered Questions

[SOLVED] How do I properly clean up Excel interop objects?

27 Answered Questions

[SOLVED] How to enumerate an enum?

39 Answered Questions

4 Answered Questions

[SOLVED] How to create an instance of Excel if Excel is not installed

1 Answered Questions

1 Answered Questions

[SOLVED] Including References for Installation

  • 2014-05-20 12:48:46
  • user3215251
  • 723 View
  • 0 Score
  • 1 Answer
  • Tags:   c# dll reference

1 Answered Questions

Are there known issues with Excel 2010 and Microsoft.Office.Interop.Excel 11?

1 Answered Questions

Sponsored Content