繁体   English   中英

如何通过C#中的ExcelDNA访问excel中选择的范围?

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

我是 Excel DNA 和 C# 编程的菜鸟。 这一定是基本问题之一,如果已经在 SO 上得到回答,如果我被指出这个方向,我会非常高兴。 当单击按钮时,我正在尝试将 Excel 工作表上处于活动状态的任何范围或单元格发送到相应的 C# function。

这是我的 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

    }

这是我的主要 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;
      }
  }
}

显然,如果我从 excel 和 select 范围内调用 Write_Data function,我可以将数据保存在 DB 中。 我的目标是在 excel 中选择范围或单元格后单击第三个按钮,并将该数据保留在 DB 中。 我的代码能够处理不同的数据(一个单元格或一系列单元格)。

如果您需要任何其他信息,请告诉我。

从功能区事件处理程序中,您可以通过 ExcelDnaUtil.Application 访问Excel COM ExcelDnaUtil.Application ,获取对活动工作表的引用并检查其选定的范围。

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.

例如

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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