Search code examples
c#excelvbawpfdata-binding

Send Excel Data from VBA to C# for Real-Time WPF Binding


I have a simple question:

How can I, from a VBA script, send the current instance of my workbook to a C# application?

The idea behind this is to read the Excel data and enable real-time data binding with a WPF interface.

Best regards, CM


Solution

  • The only method for the .NET app to communicate with the open Excel workbook is Microsoft.Office.Interop.Excel:

    using System.Collections.Generic;
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace ExcelProgrammingWalkthruComplete {
        class Walkthrough
        {
            static void Main(string[] args)
            {
                // Create a list of accounts.
                var bankAccounts = new List<Account>
                {
                    new Account {
                                  ID = 345678,
                                  Balance = 541.27
                                },
                    new Account {
                                  ID = 1230221,
                                  Balance = -127.44
                                }
                };
    
                // Display the list in an Excel spreadsheet.
                DisplayInExcel(bankAccounts);
            }
    
            static void DisplayInExcel(IEnumerable<Account> accounts)
            {
                var excelApp = new Excel.Application();
                // Make the object visible.
                excelApp.Visible = true;
    
                // We need to communicate with the active workbook.
                Excel._Worksheet workSheet = excelApp.ActiveSheet;
    
                // Earlier versions of C# require explicit casting.
                //Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
    
                // Establish column headings in cells A1 and B1.
                workSheet.Cells[1, "A"] = "ID Number";
                workSheet.Cells[1, "B"] = "Current Balance";
    
                var row = 1;
                foreach (var acct in accounts)
                {
                    row++;
                    workSheet.Cells[row, "A"] = acct.ID;
                    workSheet.Cells[row, "B"] = acct.Balance;
                }
    
                workSheet.Columns[1].AutoFit();
                workSheet.Columns[2].AutoFit();
    
                // Call to AutoFormat in Visual C#. This statement replaces the
                // two calls to AutoFit.
                workSheet.Range["A1", "B3"].AutoFormat(
                    Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);
    
                // Put the spreadsheet contents on the clipboard. The Copy method has one
                // optional parameter for specifying a destination. Because no argument
                // is sent, the destination is the Clipboard.
                workSheet.Range["A1:B3"].Copy();
            }
        }
    
        public class Account
        {
            public int ID { get; set; }
            public double Balance { get; set; }
        }
    }
    

    It allows to interactively work in both Excel and your WPF app simultaneously and exchange data dynamically in both directions.