Search code examples
.netexcelautomationcom-interop

Fastest way to access live Excel data/properties from another process with .NET


Fastest way to interface between live (unsaved) Excel data and C# objects features a very knowledgeable discussion about how to access Excel data efficiently. According to the answers two recommendations are to use Excel-Dna or Add-in Express.

I believe Excel-Dna and Add-in Express both require to run as an Excel add-in. In my case the business logic runs in another process and creates an Excel instance (New Excel.Application()). On changes in Excel it needs to update internal objects.

Getting the cell values from Excel can efficiently be done with DirectCast(Range.Value, Object(,)). Unfortunately, this is not possible for other properties like Range.Text or Range.NumberFormat. As expected, iterating the through cells is very slow.

What do you think is the most efficient way to access the Excel data from the main process? One idea I came up with is to add an add-in to Excel (using say Excel-Dna) which aggregates the data and then posts it to the main application. As I do not have much experience with cross-AppDomain marshalling I would appreciate an assessment if this is a viable approach or another one should be preferred.


Solution

  • Maybe you can read the file using one of the direct file readers, instead of going through Excel. (If need be you can automate Excel to save the file, or a copy of part of the file.)

    As a start I suggest you look at ClosedXML, but there are many similar projects. ClosedXML look like an active project and tries to mirror the COM interface. [I have no experience with it myself.]