【发布时间】:2018-06-30 04:26:33
【问题描述】:
我想使用 c# 在我的 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 是。
有人可以帮我吗?我不是很擅长提前office-interop 的东西。
提前谢谢你, 一月
【问题讨论】:
标签: c# office-interop c# excel controls scrollbar office-interop