简体   繁体   中英

How to access range selected in excel through ExcelDNA in C#?

I am noob with Excel DNA and C# programming. This must be one of the basic questions, and if it's already been answered on SO, I would be more than happy if I'm pointed in that direction. I am trying to send any range or cell that is active on Excel worksheet to the corresponding C# function when a button is clicked.

This is my Controller class.

using System;
using ExcelDna;
using Excel = Microsoft.Office.Interop.Excel;
//Other dependencies
namespace My_Prj
{
  [ComVisible(true)]
  public class RibbonController : ExcelRibbon
  {
     return @"
     <customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui'>
  <ribbon>
    <tabs>
      <tab id='tab1' label='Temp Tab'>
        <group id='group1' label='Temp Group'>
          <button id='button1' label='Attach Db' onAction='OnButton1Pressed'/>
          <button id='button2' label='Detach Db' onAction='OnButton2Pressed'/>
          <button id='button3' label='Write Data'   onAction='OnButton3Pressed'/>
        </group >
      </tab>
    </tabs>
  </ribbon>
</customUI>";
 }
public void OnButton1Pressed(IRibbonControl control)
    {
        bool status = Main.Attach();
        //+control.Id
        MessageBox.Show("DB session Attach status: [" + status + "] ");
    }
    public void OnButton2Pressed(IRibbonControl control)
    {
        bool status = Main.Detach();
        MessageBox.Show("DB session Detach status: [" + status + "]");
    }
    public void OnButton3Pressed(IRibbonControl control)
    {
        // Code to access range

    }

This is my Main class.

using System;
using ExcelDna;
using Excel = Microsoft.Office.Interop.Excel;
// Other dependencies
namespace My_Prj
{
   public class Main
   {
      // Class variables
      [ExcelFunction(Category = "Main", Description = "Attach DB", Name = "Attach_DB")]
      public static bool Attach()
      {
        //Connect to DB;
        return status;
      }
      [ExcelFunction(Category = "Main", Description = "Detach DB", Name = "Detach_DB")]
      public static bool Detach()
      {
        //Disconnect from DB;
        return status;
      }
      [ExcelFunction(Category = "Main", Description = "Write Data", Name = "Write_Data")]
      public static bool WriteData(Object[,] data)
      {
        //Write data;
      }
  }
}

Obviously, If I call the Write_Data function from excel and select the range, I am able to persist the data in DB. My goal is to click the third button after selecting the range or cell in excel, and have that data persisted in DB. My code is able to handle the different data(either one cell or a range of cells).

Please let me know if you need any additional info.

From your Ribbon event handlers, you can access the Excel COM API via ExcelDnaUtil.Application , get a reference to the active sheet, and get the selected range and inspect its values.

Install the ExcelDna.Interop NuGet package to make it easier (so you can get IntelliSense), and use the ExcelDnaUtil.Application instance to access the COM API.

eg

try
{
    var excelApp = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application;

    // Use excelApp to access the selected sheet, range, etc.
    // the same way you would do with VBA
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM