tags:

views:

71

answers:

5

I have to traverse about 25 sheets in excel for doing operations.I'm doing it using vba and finding it really slow,hence wanted to know if I could use C# and if doing that would help me speed up the process.

A: 

This link may be of help to you: http://support.microsoft.com/kb/302084

Saladin Akara
A: 

This article explains how to create and run an Excel Macro in C# at Runtime.

Kindness,

Dan

Daniel Elliott
Funny article. Calls `GC.Collect();` in the first line of code instead call it in the *last*!
abatishchev
It GC.Collects() the instance of the deleted file. Grant you it does look a little wierd though! :)
Daniel Elliott
A: 

What operations are you doing exactly? Firstly you don't event need to write the VBA code your self. You can simply record a macro in a workbook and save it in C:\Program Files\Microsoft Office\Office12\Xlstart. Excel will record an macro and make it available in all the sheets you open.

You can also tweak the auto generated VBA code in the above mentioned process to fit your need.

Personally, I don't think writing a C# add-in to do the work will speed up the task. VBA is a language specifically made to work with the Office Object Model. If that does not work fast enough then you need to look at what operation you are performing?

I think creating a C# add-in to manipulate Excel Object Model and maintaining it is too much to do. It will also involve a big learning curve.

Unmesh Kondolikar
A: 

Call C# from Excel workbook like VBA macro - you can't

Access Excel workbook from C# application - you can. This is called Microsoft Visual Studio Tools for Office (VSTO)

abatishchev
+1  A: 

Although you may be able to write an equivalent in C# to your current VBA macro using VSTO, I doubt very much if it would make any significant difference to performance.

In both cases, you'll be manipulating the same Excel COM objects and the execution time is likely to be very similar.

Your performance will depend mainly on the techniques you are using to manipulate the Excel objects. As a simple example, you can assign values from an Excel Range to a 2-dimensional array in a single statement: this is much faster than iterating through the rows and columns in code and copying values into your array one by one.

I'd recommend you post bits of the VBA code that you find too slow, and ask for tips on improving performance.

Joe