我有一个用于Excel的VSTO加载项,用数据填充工作表中的列表对象,我希望工作表自动滚动以显示列表的底部(如果用户是,则不强制将焦点放在此工作表上看着另一个)。有没有办法以编程方式执行此操作?
谢谢< 333
答案 0 :(得分:3)
注意:这个答案取自 Ben Stabile 在接受的答案下的评论;以防万一其他人正在寻找'正确'的答案,但没有看到评论。 我尝试了Ben所建议的,它完全实现了Excel中的“Go To”。
activeCell.Select()
只会选择单元格,即使它不在视图中。正确的方法是使用类似:Application.ActiveWindow.ScrollRow = range.Rows.Count.
答案 1 :(得分:0)
我想你想要这样的东西:
Worksheet worksheet = Application.Sheets[2]; //Index of the sheet you want to change the selected cell on
if (worksheet == Application.ActiveSheet)
{
Excel.Range range = worksheet.UsedRange;
int rows = range.Rows.Count;
int columns = range.Columns.Count;
Excel.Range activeCell = worksheet.Cells[rows, columns];
activeCell.Select();
}
只需选择包含数据的工作表,检查这是否是用户当前正在使用的工作表,如果是,则创建一个等于工作表的已用单元格的Range对象,然后获取列的数量和行,并创建第二个Range对象,该对象等于右下角使用的单元格,并调用此Range上的Select();
方法使其成为活动单元格。
答案 2 :(得分:0)
//代码"拆分","冻结","添加过滤器","列自动适应"对于工作簿中的所有工作表
//path were excel file is kept
string ResultsFilePath = @"C:\\Users\\krakhil\\Desktop\\FolderName\\FileNameWithoutExtension";
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(ResultsFilePath);
ExcelApp.Visible = true;
//Looping through all available sheets
foreach (Excel.Worksheet ExcelWorksheet in ExcelWorkbook.Sheets)
{
//Selecting the worksheet where we want to perform action
ExcelWorksheet.Select(Type.Missing);
//Making sure first row is selected - else split and freeze will happen
//On the visible part and not from the top
Excel.Range activeCell = ExcelWorksheet.Cells[1, 1];
activeCell.Select();
//Applying auto filter to Row 10
activeCell = (Excel.Range)ExcelWorksheet.Rows[10];
activeCell.AutoFilter(1,
Type.Missing,
Excel.XlAutoFilterOperator.xlAnd,
Type.Missing,
true);
//Split the pane and freeze it
ExcelWorksheet.Application.ActiveWindow.SplitRow = 10;
ExcelWorksheet.Application.ActiveWindow.FreezePanes = true;
//Auto fit all columns
ExcelWorksheet.Columns.AutoFit();
//Releasing range object
Marshal.FinalReleaseComObject(activeCell);
}
//saving excel file using Interop
ExcelWorkbook.Save();
//closing file and releasing resources
ExcelWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(ExcelWorkbook);
ExcelApp.Quit();
Marshal.FinalReleaseComObject(ExcelApp);