Creating Excel with C# and dealing with various Excel versions - COM interop methogd

Posted on 2013-09-16
Medium Priority
Last Modified: 2013-09-16
I need to create a C# windows application that will read and write and also creates Excel work books.

I like the COM interop method for its simplicity.

I am planning to create simple Excel sheets , but need some minor formatting to columns.
I am happy to create my files with xls extension as opposed to xlsx, so it can be opened in Excel 2007 etc.

But when I use COM interop , do I need to have the same version of MS Office  as the end user ? such as office 2010 ?

Can I not use office 2010 on my development PC and then deploy the necessary dlls along with my application so that the app will work on any office version.
Question by:indikad
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 1800 total points
ID: 39498405
No. That would mean distributing Excel 2010 itself... and Microsoft would stop making money.

The solution, although not interesting at all, is to let go early binding (using specific objects such as Microsoft.Office.Interop.Excel.Workbook), and work in late binding, which means using Object variables. This is discussed by Microsoft at http://msdn.microsoft.com/en-us/library/0tcf61s1.aspx. And lucky you, the sample code is with Excel.

This is very bad for a programmer however, because you lose a lot of features, such as IntelliSense and compile time checking.

What I do when I need to work like that, is usually to code and test with Early Binding, so that I have all my usual tools to work with. I then go back and replace the type by Object in all my Excel variables declarations, as well as replace the New for those variable by CreateObject. A pain in a big application, but it is usually worth it in debugging time during development.

One trick, when possible, is to delegate as much work as possible to macros in the Workbook, using templates to create new workbooks if necessary. This not only simplifies your work, but is also usually faster. Your .NET code simply calls the Run method of the Application object, passing parameters as needed, and let the macro do the job.

Author Closing Comment

ID: 39498474
Thanks - I will try this.

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this article, I will demonstrate that how to do a PST migration from Exchange Server to Office 365. This method allows importing one single PST, or multiple PST's at once.
Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question