vb.net中的Excel工作表

时间:2012-08-18 21:18:53

标签: vb.net excel

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

1 个答案:

答案 0 :(得分: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