VSTO C#:读取当前的Cell值,该值始终为null [英] VSTO C#: Read the current Cell value which is always null

查看:195
本文介绍了VSTO C#:读取当前的Cell值,该值始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是excel addin编程的新手。 

I'm new for excel addin programming. 

场景:

我必须阅读活动表中活动单元格的内容。如果值是非空的,例如" ABC"在那个时候我必须禁用功能区栏中的一个项目,当用户清除该时间项目上的单元格时,应该在功能区栏中启用项目。

I have to read the content of Active cell in active sheet. if value is nonempty e.g. "abc" on that time I have to disable the one item in ribbon bar and when user has clear the cell on that time item should be enable item in ribbon bar.

我已经写了这段代码来获取当前单元格的值:

I have wrote this code to get the value of current cell:

     私人字符串CurrrentCellValue()

        {

            var BeginCell = string.Empty;

            var CellValue = string.Empty;



            var activeWorkBook = Globals.ThisAddIn.Application.ActiveWorkbook;



            if(activeWorkBook!= null)

            {

$
                var sheet = activeWorkBook.ActiveSheet as Worksheet;



                if(!sheet.ProtectContents)

                {

                    var range = Globals.ThisAddIn.Application.Selection为Range;

                    if(range!= null)

                    {

$
                        string address = range.get_Address();

                        string [] cells = address.Split(new char [] {':'});

                        BeginCell = cells [0] .Replace(" $","");



                        var activeSheet = Globals.ThisAddIn.Application.ActiveSheet;

                        range = activeSheet.Range(BeginCell);

                       范围项= range.get_Item(1);

                        CellValue = item.Value2;

                    }¥b $ b                }¥b $ b            }


           返回CellValue;



        }

     private string CurrrentCellValue()
        {
           var BeginCell = string.Empty;
            var CellValue = string.Empty;

            var activeWorkBook = Globals.ThisAddIn.Application.ActiveWorkbook;

            if (activeWorkBook != null)
            {

                var sheet = activeWorkBook.ActiveSheet as Worksheet;

                if (!sheet.ProtectContents)
                {
                    var range = Globals.ThisAddIn.Application.Selection as Range;
                    if (range != null)
                    {

                        string address = range.get_Address();
                        string[] cells = address.Split(new char[] { ':' });
                        BeginCell = cells[0].Replace("$", "");

                        var activeSheet = Globals.ThisAddIn.Application.ActiveSheet;
                        range = activeSheet.Range(BeginCell);
                        Range item = range.get_Item(1);
                        CellValue = item.Value2;
                    }
                }
            }

            return CellValue;

        }

 功能区栏中的项目:

  Item in ribbon bar:

 我已将命令(ICommand)与功能区栏中的项目一起使用

  I have used the command(ICommand) with item in ribbon bar

   _ribbonItem.ItemOpenCommand = new DelegateCommand(ItemButtonClick,canexecuteMethdod1);

  _ribbonItem.ItemOpenCommand = new DelegateCommand(ItemButtonClick, canexecuteMethdod1);

 我所做的是:

  what I have done is:

 在canexecuteMethdod1方法中,我已经调用了方法来读取当前单元格,如果单元格值在该时间不为空,则ItemButtonClick方法将不会执行,否则它将会执行。

  In canexecuteMethdod1 Method, I have called the method to read the current cell, if cell value is not null on that time ItemButtonClick method wont excute otherwise it will.

但问题我是面对当活动单元格中有一些值时,我将所有值都变为null。怎么能动态地读取活动单元格的内容。

But problem I'm facing that when there is some value in active cell I'm getting all the value as null. how can dynamically i read the content of active cell.

简而言之,我正在尝试复制excel的fx(插入函数)。

in short i'm trying to replicate the fx(insert function) of excel.

请帮忙。

vikas sharma AB

vikas sharma AB

推荐答案

您好,

我想我们可以处理
Worksheet.Change Event(Excel)
 或
Application.SheetChange事件(Excel)
动态返回活动内容单元格。 

I think we could handle Worksheet.Change Event (Excel) or Application.SheetChange Event (Excel) to dynamically return the content of active cell. 

例如

  private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.Application.WorkbookOpen += Application_WorkbookOpen;
            ((Excel.AppEvents_Event)this.Application).NewWorkbook +=
                new Excel.AppEvents_NewWorkbookEventHandler(Workbook_NewWorkbook);
        }

        private void Workbook_NewWorkbook(Excel.Workbook Wb)
        {
            ((Excel.Worksheet)Wb.ActiveSheet).Change +=
                new Excel.DocEvents_ChangeEventHandler(ActiveSheet_Change);
        }
        private void Application_WorkbookOpen(Excel.Workbook Wb)
        {
            ((Excel.Worksheet)Wb.ActiveSheet).Change +=
               new Excel.DocEvents_ChangeEventHandler(ActiveSheet_Change);
        }

        private void ActiveSheet_Change(Excel.Range Target)
        {         
        }


   private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.Application.SheetChange += Application_SheetChange;
        }
 private void Application_SheetChange(object Sh, Excel.Range Target)
        {
        }

问候,

Celeste


这篇关于VSTO C#:读取当前的Cell值,该值始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆