簡體   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