Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we write a macro in C# for Excel

Tags:

c#

excel

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.

like image 724
gizgok Avatar asked Sep 03 '10 08:09

gizgok


People also ask

Are there macros in C?

Predefined Macros in C. There are some predefined macros present in C. And it cannot be modified. Below are some predefined macros.

What are macro in C?

A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions need not be terminated by a semi-colon(;).

Can you use a macro in a macro C?

Short answer yes. You can nest defines and macros like that - as many levels as you want as long as it isn't recursive.

Can a macro call itself in C?

You can't have recursive macros in C or C++.


2 Answers

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)

like image 151
abatishchev Avatar answered Oct 09 '22 09:10

abatishchev


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.

like image 32
Joe Avatar answered Oct 09 '22 07:10

Joe