如何滚动到SpreadsheetGear WorkBookView控件中的列?

时间:2019-04-26 17:14:34

标签: vb.net spreadsheetgear

我每周必须校对30-35个电子表格。

我需要查看W列和W列右侧的列。当我最初显示电子表格时,我会看到A-P列。我手动滚动到W列,然后开始校对数据。

我单击下一步按钮,该按钮将转到下一个文件。我的代码加载了下一个电子表格:

WorkbookView1.ActiveWorkSheet = Workbook_Obj.ActiveWorksheet.
WorkbookView1.Update()

有时候水平滚动位置保持不变,我可以看到W列和W列右侧的一些列。

但是有时水平滚动位置会更改为W列的左侧,而我看不到W列了。

我会做这样的事情:

' 23 = Column W
Col_Obj = WorkbookView1.Cells(0, 23)
' Or - find column by header text in row = 0.
Col_Obj - WorkbookView1.FindByText("Name")
Col_Obj.HorizontalScroll = Col_Obj.Location

谢谢埃德

1 个答案:

答案 0 :(得分:0)

您可以设置IWorksheetWindowInfo。ScrollColumn(请注意,此属性使用从零开始的索引,即列A为索引0,列B为1,依此类推),它将设置显示在窗户。请注意,如果您需要设置行滚动位置,也有一个ScrollRow property

当涉及到WorkbookView时(例如您的情况),可能会影响许多其他因素,例如ScrollColumn之类的设置属性是否实际上会在WorkbookView上生效(这与以下事实有关:可以将同一工作表附加到多个WorkbookView控件,并且每个WorkbookView可以保留这些“窗口信息”属性的副本。此外,在这方面,SpreadsheetGear 2012和2017的工作方式也存在一些行为差异。下面的代码在大多数情况下应该可以使用,但也希望在您的情况下也可以使用,尽管在某些情况下可能无法使用...例如,如果您使用的是SpreadsheetGear 2017,并且将此工作表附加到多个WorkbookViews上(我猜您不是);但如果是这样,我可以更新此答案以更好地适应您的特定情况。

' Make local variable to the desired worksheet
Dim activeWorksheet = Workbook_Obj.ActiveWorksheet

' Reference cell to select and scroll to
Dim cellW1 = activeWorksheet.Cells("W1")

' Select the cell (this call alone can sometimes also automatically
' trigger the cell to be scrolled to as well, though it may not depending
' on the circumstances; and it may not make this cell the leftmost in the
' window...
cellW1.Select()

' ...to avoid any problems like mentioned above, explicitly set ScrollColumn 
' to the selected cell.  Set ScrollRow as well if desired.
activeWorksheet.WindowInfo.ScrollColumn = cellW1.Column

' Attach workbook *AFTER* doing the above.  For a variety of reasons I
' won't go into, attaching the worksheet before setting the above "window
' info" options may not have any effect.
WorkbookView1.ActiveWorksheet = Workbook_Obj.ActiveWorksheet