Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly 'Office, Version=15.0.0.0'

I use Vs2013. I have created application in which I use Excel file as an input and get contact from the file. Everything is working in my computer. I have Vs2013. Windows 8.1, Ms office 2007 & 2013.
When I run my application in any other computer, it throws

Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bc111e9429c' or one of its dependencies. The system cannot find the file specified

As per my application requirement, I need to use Excel file from Office 2007 to 2013.

I have referred few StackOverflow links but I am not getting result. I am stuck. Please suggest me how to solve this.

like image 749
NJ Bhanushali Avatar asked Sep 04 '15 13:09

NJ Bhanushali


5 Answers

Your other machine needs to have the corresponding version of Office installed. 15.0.0.0 should correspond to Office 2013 - that needs to be installed on your target machine (other versions of Office may not work). This almost certainly means you're using MSOffice interop libraries, which only work if office is installed and against the same version.

Alternatively, you could refactor your code to just directly read the Excel XML.

like image 83
Dan Field Avatar answered Oct 24 '22 10:10

Dan Field


I got solution by changing Excel.dll version. I was using 15.0.0.0 and now I changed it to 12.0.0.0 and its working fine. I got dll from Add reference > Browse > C: > Windows > assembly > GAC > Microsoft.Office.Interop.Excel > 12.0.0.0_etc > Microsoft.Office.Interop.Excel.dll

like image 25
NJ Bhanushali Avatar answered Oct 24 '22 11:10

NJ Bhanushali


I also got this error message even if I have Office 2010 and I don't have Microsoft.Office.Interop.Excel folder under GAC.
I found the solution for my case here: https://www.add-in-express.com/forum/read.php?FID=5&TID=15525
You have to reference two dll-files from these folders:
C:\Windows\assembly\GAC_MSIL\Microsoft.Vbe.Interop\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll
and
C:\Windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\OFFICE.DLL

like image 12
Pinokio Avatar answered Oct 24 '22 11:10

Pinokio


I created a batch file to fix this issue. See script below:

    echo off
        cls
        color 1f
        echo Checking for Administrator elevation.
        openfiles>nul 2>&1

            if %errorlevel% EQU 0 goto isadmin

                COLOR 4f
            echo.    You are not running as Administrator.
            echo.    This tool cannot do it's job without elevation.
            echo.
            echo.    You need run this tool as Administrator.
            echo.

            echo.Press any key to continue . . .
            pause>nul
        exit
        :isadmin
        if exist c:\windows\assembly\GAC_MSIL\office\16.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=16
    if exist c:\windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=15
    if exist c:\windows\assembly\GAC_MSIL\office\14.0.0.0__71e9bce111e9429c\OFFICE.DLL set officever=14

    md c:\windows\assembly\GAC_MSIL\office\12.0.0.0__71e9bce111e9429c
    xcopy c:\windows\assembly\GAC_MSIL\office\%officever%.0.0.0__71e9bce111e9429c c:\windows\assembly\GAC_MSIL\office\12.0.0.0__71e9bce111e9429c /s/y
pause
like image 6
Moses Hull Avatar answered Oct 24 '22 10:10

Moses Hull


I had the same issue you should include the following package (Syncfusion.XlsIO) instead of (Microsoft.Office.interop.Excel) cuz it only support excel 2013, but the first one 'Syncfusion.XlsIO' does support Excel 2016;

=> using Syncfusion.XlsIO;

Here is the following code :

  using (ExcelEngine excelEngine = new ExcelEngine())
                {
                        IApplication application = excelEngine.Excel;
                        application.DefaultVersion = ExcelVersion.Excel2016;
                        IWorkbook workbook = application.Workbooks.Create(1);
                        IWorksheet worksheet = workbook.Worksheets[0];
                        //Adding text to a cell
                        for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
                        {
                           worksheet.Range[1, i].Text = dataGridView1.Columns[i - 1].HeaderText;
                        }

                        for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
                        {
                            for (int j = 0; j < dataGridView1.Columns.Count; j++)
                            {
                                worksheet.Range[i + 2, j + 1].Text = dataGridView1.Rows[i].Cells[j].Value.ToString();
                            }
                        }
                        //Saving the workbook to disk in XLSX format
                        Stream excelstream = File.Create(Path.GetFullPath(@"MyExcelFile.xlsx"));
                        workbook.SaveAs(excelstream);
                        excelstream.Dispose();
                }
like image 2
jadar_m Avatar answered Oct 24 '22 10:10

jadar_m