【问题标题】:Accessing Scrollbar control on excel sheet using C#使用 C# 访问 Excel 工作表上的滚动条控件
【发布时间】:2018-06-30 04:26:33
【问题描述】:

我想使用 在我的 Excel 工作表中添加一个滚动条控件。我不想使用Windows.Forms.Scrollbar,而是 Excel 提供的那个。

添加控件没问题 - 我的问题是访问创建的控件以定义最小值、最大值和单元格引用。这是我的代码:

    public static void AddScrollBar(Ex.Worksheet ExSH, int StartCellRow, int StartCellColumn, int EndCellRow, int EndCellColumn, int ReferenceRow, int ReferenceColumn, int MaxValue, int MinValue = 0, int CurrentValue = 0)
    {
        int Left, Top, Right, Bottom;
        if(StartCellRow > EndCellRow || StartCellColumn > EndCellColumn) { throw new System.Exception("EROR: Please check Start- and Endcell reference or flip it!"); }

        Left = (int)(ExSH.Cells[StartCellRow, StartCellColumn] as Ex.Range).Left;
        Top = (int)(ExSH.Cells[StartCellRow, StartCellColumn] as Ex.Range).Top;
        Right = (int)((ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Left + (ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Width);
        Bottom = (int)((ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Top + (ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Height);

        Ex.ControlFormat SB = ExSH.Shapes.AddFormControl(Ex.XlFormControl.xlScrollBar, Left, Top, Right - Left, Bottom - Top) as Ex.ControlFormat;
        SB.Value = CurrentValue; /*ERROR: Object reference not set to an instance of an object*/
        SB.Min = MinValue;
        SB.Max = MaxValue;
        SB.LinkedCell = (ExSH.Cells[ReferenceRow, ReferenceColumn] as Ex.Range).ToString();
    }

错误发生在第 12 行 (SB.Value = ...)
当我注释行 SB.Value = ...SB.LindedCell = ... 时,代码运行良好。打开创建的 Excel 工作簿并选择依赖的工作表,我可以找到创建的滚动条应该在的位置。 当我在创建滚动条后中断时,SB 是。

有人可以帮我吗?我不是很擅长提前 的东西。

提前谢谢你, 一月

【问题讨论】:

    标签: c# office-interop c# excel controls scrollbar office-interop


    【解决方案1】:

    啊,我找到了。 SB 是 Ex.Shape。然后,您可以通过 SB.ControlFormat.Min、Max、Value 等访问参数。

    这是工作代码:

        public static void AddScrollBar(Ex.Worksheet ExSH, int StartCellRow, int StartCellColumn, int EndCellRow, int EndCellColumn, int ReferenceRow, int ReferenceColumn)
        {
            int Left, Top, Right, Bottom;
            if(StartCellRow > EndCellRow || StartCellColumn > EndCellColumn) { throw new System.Exception("EROR: Please check Start- and Endcell reference or flip it!"); }
    
            Left = (int)(ExSH.Cells[StartCellRow, StartCellColumn] as Ex.Range).Left;
            Top = (int)(ExSH.Cells[StartCellRow, StartCellColumn] as Ex.Range).Top;
            Right = (int)((ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Left + (ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Width);
            Bottom = (int)((ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Top + (ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Height);
    
            Ex.Shape SB = ExSH.Shapes.AddFormControl(Ex.XlFormControl.xlScrollBar, Left, Top, Right - Left, Bottom - Top);
            SB.ControlFormat.Value = CurrentValue;
            SB.ControlFormat.Min = MinValue;
            SB.ControlFormat.Max = MaxValue;
            SB.ControlFormat.LinkedCell = "A" + ReferenceRow.ToString();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2011-10-28
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多