How to detect excel assembly (version) and use it programmatically in C# - c#

I needed my application to export a dataGridView content to Excel. That's fine, all the help ended up by adding the Excel.Interop reference according to the installed version of Excel, then creating a new Excel Application and finally saving to a file (.xlsx).
However, it worked fine on machines with the same installed version of Office (Excel 2013). But, on machines with either 2007 or 2016 it does not work.
I know how to check which version is installed, but is there a way to detect and use the Interop no matter which version is installed? For example adding the DLL reference during runtime.
Thanks in advance

Related

Windows Form + Excel Worksheet

I've built a windows application that retrieves information from an Excel Worksheet. I've tested and had it working on a computer with Microsoft Office Package installed.
I have a little problem now, I will be deploying this program to multiple workstations but they have OpenOffice installed. My issue is, do these workstations need have Microsoft Office installed or would it work regardless?
Thank you all.
If you're referencing the MS Office object library, then they need to have it installed.
Edit: You can probably install Open Office on your machine, write a similar call for that object library, and have it choose based on the installed program(s).

Checking for Excel dll and loading dynamically

I am using Microsoft.Office.Interop.Excel for writing data to an Excel file, I have Excel 2010 installed on my machine. But I need the program to run on older versions of Excel..
The program should check the version installed in the particular machine and load the dll dynamically so as not to get a Missing Reference error..
If Excel 2003 is available, use it.
Else If a higher Excel version is available, use it
Else show an error message since this means that Excel is not installed in the machine
Is there a way to do this?
I am really having a hard time looking for a solution. Any help would be appreciated.
Thanks in advance..
Use NetOffice (http://msdn.microsoft.com/en-us/library/bb417255%28WinEmbedded.5%29.aspx), it really provides a nice abstraction to all Office Versions.

Read excel file with c# in 64-bit pc with Office 2003

I have a desktop application which has to read an excel file (either .xls or .xlsx).
When the application is installed in a Windows 7 pc with Office 2003 i cant read the excel file using Microsoft.ACE.OLEDB.12.0. I have the message that Microsoft.ACE.OLEDB.12.0 is not registered.
Using Microsoft.JET.OLEDB.4.0. doesnt help either.
So how can we read the excel file? I know one option is to build my application forcing it to compile to (x86) so it runs as a 32-bit application.
Ok, supposing i dont want to do so, is there any other option?
Thanks for any answers. Any comments are welcome!
You need the 64-bit JET/ACE driver installed:
http://www.microsoft.com/en-us/download/details.aspx?id=13255
Sounds like a job for Npoi. Stand alone library that can work with xls and xlsx files. No need to use those Jet libraries.
Ok, that will involve a bit of work, but you remove a dependency from something you can't control on user's machine.

Error when export Excel using Microsoft.Office.Interop.Excel

I use Microsoft.Office.Interop.Excel.dll COM to export excel. I distribute my application to clients. It runs well on my computer but on another computer, it cause a error.
I wonder that application package should be included Microsoft.Office.Interop.Excel.dll and the computer must be installed Microsoft Excel. Thanks.
P/S: My application is C# .net 2.0.
UPDATE: I am having a problem. I don't know the excel's version on the client computer. So, which version of PIA I will use?
If I build my application with Microsoft.Office.Interop.Excel.dll version 11 (suitable with Office 2003), but what problem will happen if the client computer have Office 2007. It will run or no? How to use only one Microsoft.Office.Interop.Excel.dll which run well on any client computers which have one of office 2003, 2007 or 2010.
Thanks.
Did you deploy the other necessary DLL's?
Interop.Microsoft.Office.Core.dll
Microsoft.Vbe.Interop.dll
EDIT:
You should see two references in your project: Microsoft.Office.Core and VBIDE. Click on these references and set the property "Local Copy" to true.
A complete refrence to installing and deploying the PIA can be found here: http://msdn.microsoft.com/en-us/library/aa159923%28office.11%29.aspx
On that website is also a link to a redistributable file you can include and run from your installer to install the PIA into the GAC.
If you want to use Office Automation you should always have Office installed. That's because the interop classes will create an instance of excel in the background and run all your actions on that instance.
If there is no office installed, it will thus not run properly. Please also keep in mind that using office automation on a server isn't the way to go (due to security issues and so on).
Are they using the same version of Excel? you could look into the PIA (Primary Interop Assembiles) to deploy with your app.

using ms office com objects in other systems without have office installed

i wrote a app for reading excel files.i had to add some references to Microsoft office Com Objects.
it worked good in my system,but when i wanted to use it in other system in which does`t had ms office installed ,
i encounter a problem.i also carried dll files that has been added to project . what should i do ?
In order to use the relevant object libraries you'll have to make sure that Office (Excel in your case) is installed on your client machine. If you have the option I'd suggest to switch to the Open Office XML format anyways. I recently had a similar problem with MS Word and the +work to switch to the new format has def. payed of.
Check MS Open XML SDK
Performace++ and not depending on Office installed...
The Office COM objects depend on Office. You should either arrange for Office to be installed on the other systems, or use a component for reading Excel files that doesn't need Excel installed, such as the ODBC Excel driver or a third-party Excel file reader.

Categories