1

我有一个 Excel 的 VSTO 加载项,它用数据填充工作表中的列表对象,我希望工作表自动滚动以显示列表的底部(如果用户正在查看另一个工作表,则无需强制将焦点放在此工作表上)。有没有办法以编程方式做到这一点?

谢谢 <333

4

3 回答 3

5

注意:此答案取自Ben Stabile在已接受答案下的评论;以防其他人正在寻找“正确”的答案,但没有碰巧看到评论。我尝试了 Ben 的建议,它完全实现了 Excel 中的“Go To”。

activeCell.Select()即使它不在视图中,也会简单地选择单元格。正确的方法是使用类似的东西:Application.ActiveWindow.ScrollRow = range.Rows.Count.

于 2018-01-26T10:34:45.377 回答
1

我想你想要这样的东西:

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 对象,该对象等于右下角使用的单元格,并调用Select();此 Range 上的方法以使其成为活动单元格。

于 2012-09-28T09:29:50.337 回答
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);
于 2017-11-09T09:05:18.673 回答