[c#] How to use Microsoft.Office.Interop.Excel on a machine without installed MS Office?

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?

This question is related to c# excel dll interop

The answer is


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!


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.


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


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to excel

Python: Pandas pd.read_excel giving ImportError: Install xlrd >= 0.9.0 for Excel support Converting unix time into date-time via excel How to increment a letter N times per iteration and store in an array? 'Microsoft.ACE.OLEDB.16.0' provider is not registered on the local machine. (System.Data) How to import an Excel file into SQL Server? Copy filtered data to another sheet using VBA Better way to find last used row Could pandas use column as index? Check if a value is in an array or not with Excel VBA How to sort dates from Oldest to Newest in Excel?

Examples related to dll

The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing while starting Apache server on my computer PHP 7: Missing VCRUNTIME140.dll How to fix PHP Warning: PHP Startup: Unable to load dynamic library 'ext\\php_curl.dll'? WampServer: php-win.exe The program can't start because MSVCR110.dll is missing msvcr110.dll is missing from computer error while installing PHP installing JDK8 on Windows XP - advapi32.dll error The program can’t start because MSVCR71.dll is missing from your computer. Try reinstalling the program to fix this program ImportError: DLL load failed: %1 is not a valid Win32 application. But the DLL's are there Loading DLLs at runtime in C# Missing `server' JVM (Java\jre7\bin\server\jvm.dll.)

Examples related to interop

Exception from HRESULT: 0x800A03EC Error How to use Microsoft.Office.Interop.Excel on a machine without installed MS Office? How do I import from Excel to a DataSet using Microsoft.Office.Interop.Excel? How can I make SQL case sensitive string comparison on MySQL? Better way to cast object to int Write Array to Excel Range How do I properly clean up Excel interop objects? How to call shell commands from Ruby