0

有没有办法让当前工作表在 excel 中成为在 vb.net 中更改工作表时显示的工作表?假设我在工作表 1 上并通过 vb.net 将其更改为工作表 2,如果我输入数据,它将显示在工作表 2 上,但为了查看数据,我必须实际转到 excel 文件并选择工作表 2为了更改正在显示的实际工作表页面。

4

1 回答 1

0

尝试了这段代码,它似乎对我有用。诀窍是方法sheet.Activate()

Sub Main
    Dim excelApp as Microsoft.Office.Interop.Excel.Application = _
                    new Microsoft.Office.Interop.Excel.Application()
    Try  
        Dim inFile As String = "D:\temp\test.xls"
        ' Show excel ' 
        excelApp.Visible = true
        Dim excelWorkbook As Microsoft.Office.Interop.Excel._Workbook = _
                             excelApp.Workbooks.Open(infile)  

        Dim x as Integer 
        for x = excelApp.Sheets.Count to 1 step -1 
            Dim sheet as _Worksheet = CType(excelApp.Sheets(x), _Worksheet)
            sheet.Activate()   ' Activate the sheet'
            Thread.Sleep(5000) ' Sleep for 5 seconds to see the sheet'
        Next
        Finally  
            if excelApp IsNot Nothing then
                excelApp.DisplayAlerts = false
                'excelApp.Quit();  'remove the comment to close excel'
            End if
        End try
 End Sub
于 2012-08-18T21:38:27.270 回答