-Govert
To post to this group, send email to exc...@googlegroups.com.
Hi Teja,
If you are using the COM interfaces to populate the cells, the overhead of the COM calls to Excel overwhelm any other aspect of the performance, and you should expect similar performance whether using .NET or VBA.
Using the C API, you can improve the performance by using the native ExcelReference type, and settings the values all at once with a 2D object array.
One snag is that you need to be in a ‘macro’ context to call the C API, which your Worksheet_Open event handler is not. Easiest is to make a Sub and then call it via Application.Run.
Another approach is to use the ExcelAscynUtil.QueueAsMacro helper to switch to a macro context, but that’s a bit harder to explain now.
So your code might look like this:
Imports ExcelDna.Integration
Imports Microsoft.Office.Interop.Excel
Public Class TestingEvents
Implements IExcelAddIn
Dim WithEvents Application As Application
Public Sub AutoOpen() Implements IExcelAddIn.AutoOpen
Application = ExcelDnaUtil.Application
End Sub
Public Sub AutoClose() Implements IExcelAddIn.AutoClose
End Sub
Private Sub Application_NewWorkbook(Wb As Workbook) Handles Application.NewWorkbook
MsgBox("Opened workbook: " + Wb.Name)
Application.Run("DumpValues")
End Sub
End Class
Public Module DumpData
Sub DumpValues()
' Set target range (0-based row and column indices)
Dim target As New ExcelReference(0, 449, 0, 149, "Sheet1")
Dim values(,) As Object = New Object(449, 149) {}
' Fill in values
For i = 0 to 449
For j = 0 to 149
values(i,j) = String.Format("({0}, {1})", i, j)
Next
Next
Target.SetValue(values)
End Sub
End Module
-Govert
Hi Teja,
If you are using the COM interfaces to populate the cells, the overhead of the COM calls to Excel overwhelm any other aspect of the performance, and you should expect similar performance whether using .NET or VBA.
Teja
...&n
Hi Teja,
The ExcelReference.SetValue(…) does not use the COM API, but goes through the native C API instead.
I’m not sure what the performance would be when doing one cell at a time, relative to using the COM Range.Value call.
I know nothing about merged cells, aside from the fact that they’re to be avoided completely.
If you have to write formatting, that can be done through the C API as well, but it’s more difficult than using the COM interfaces, and still only one cell at a time.
(Maybe you can set Conditional Formatting for the range, instead of set the colours explicitly.)
Another way to do this would be to create a workbook file directly (e.g. using the ClosedXML library), then copy and paste the data (with formatting).
I still don’t know how merged cells would work with a copy/paste.
I don’t think it’s easy to detect window scroll events.
It all sounds rather complicated …
But to summarize the answer to your original question:
Excel-DNA gives you access to the C API, which is a more restricted than the COM object model, but can sometimes be faster. For using the COM object model, the .NET code should have similar performance to VBA code.
Regards,
--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
exceldna+u...@googlegroups.com.
...
Private Sub Application_WorkbookOpen(Wb As Workbook) Handles Application.WorkbookOpen
--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at https://groups.google.com/group/exceldna.
Hi Teja,
COM events work will fine with your Excel-DNA add-in. You need to reference the Primary Interop Assembly (via NuGet you can add the Excel-DNA.Interop package).Then to get hold of the root Application object, you cann ExcelDnaUtil.Application. This returns the right COM Application object for your Excel instance.I paste a small example below of how your event handler might work.Regards,Govert'''''''''''''''''''''''''' VB.NET event handler sample '''''''''''''''''''''''''''
Imports Microsoft.Office.Interop.ExcelImports ExcelDna.Integration' This class has a WithEvents Application field, allowing it to easily handle' any Application events.' An instance is created in the AutoOpen handler below.' A singleton instance could also be created in a 'Shared Sub New' constructor.Public Class EventTest
Dim WithEvents Application As Application
Public Sub New()
Application = ExcelDnaUtil.ApplicationEnd Sub
Private Sub Application_WorkbookOpen(Wb As Workbook) Handles Application.WorkbookOpen
MsgBox("Opened workbook: " + Wb.Name)
End SubEnd Class' This class implements the Excel-DNA interface IExcelAddIn,' so the AutoOpen will be called when the Add-In is loaded.' We just create a new instance of the event handling class.Public Class AddInTestImplements IExcelAddInDim applicationEvents As EventTest
Public Sub AutoOpen() Implements IExcelAddIn.AutoOpen
applicationEvents = New EventTest
End SubPublic Sub AutoClose() Implements IExcelAddIn.AutoCloseEnd Sub
End Class
Maybe something like this.
-Govert
// ---------------------
using Microsoft.Office.Interop.Excel;
using ExcelDna.Integration;
public class AddIn : IExcelAddIn
{
Application _app;
public void AutoOpen()
{
_app = (Application)ExcelDnaUtil.Application;
_app.WorkbookOpen += _app_WorkbookOpen;
}
public void AutoClose()
{
}
void _app_WorkbookOpen(Workbook Wb)
{
System.Windows.Forms.MessageBox.Show($"Opening {Wb.FullName}");
--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/a3e9102c-0e2e-4978-96d3-7dba189255f2n%40googlegroups.com.