77

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?

pnuts
  • 58,317
  • 11
  • 87
  • 139
John Wales
  • 1,265
  • 2
  • 14
  • 20
  • Well I see this is an older post - so maybe too late - but I have had to do this in the past and I used Open XML Formats: https://support.office.com/en-us/article/open-xml-formats-and-file-name-extensions-5200d93c-3449-4380-8e11-31ef14555b18 – Jon Vote Dec 06 '18 at 19:50

4 Answers4

70

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:

Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
user1519979
  • 1,854
  • 15
  • 26
  • 12
    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: http://closedxml.codeplex.com/ – Govert Jul 12 '12 at 09:12
  • Thanks. I will try these libs. – John Wales Jul 12 '12 at 09:40
  • 3
    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. – CZahrobsky Aug 08 '14 at 17:09
  • I have office installed and I get the same issue. Can you please help? – ehh Jan 27 '16 at 15:42
  • 1
    XLSX SDK from Microsoft: https://learn.microsoft.com/en-us/office/open-xml/open-xml-sdk – Der_Meister May 21 '20 at 16:17
2

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!

Philippe Grondier
  • 10,900
  • 3
  • 33
  • 72
  • 21
    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... – Christian Sauer Jan 02 '13 at 08:25
  • "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. – Alan B Feb 20 '13 at 16:34
  • 20
    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. – Jonas Söderström Jul 08 '13 at 04:08
  • 2
    I agree with with @JonasSöderström it is not about considering single Office license – strike_noir Dec 05 '13 at 09:33
2

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.

Ashish
  • 29
  • 1
  • 2
    developing office automation in client server architecture is not recommended. – kaushalparik27 Sep 15 '14 at 10:41
  • Each device using the service is required to have a license. Microsoft is quite flexible with how a person uses their own license. However, this doesn't extend to buying one single use license and then sharing it with +1000 other people. – Yogi Feb 19 '21 at 13:17
1

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

JoelC
  • 3,664
  • 9
  • 33
  • 38
miro
  • 101
  • 2
  • 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. – EAmez May 16 '17 at 09:03