开发者

Can we write a macro in C# for Excel

开发者 https://www.devze.com 2023-01-14 13:16 出处:网络
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开发者_如何学JAVA use C# and if doing that would help

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开发者_如何学JAVA use C# and if doing that would help me speed up the process.


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)


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.


You can try my ESharper add-in to write an Excel automation command in C#. It is easier than creating a separate C# application or an add-in, and you will have access to both Excel object model and Excel C API with more options for performance optimization.

0

精彩评论

暂无评论...
验证码 换一张
取 消