vb.net操作EXCEL,为什么设置了excelApp.Visible = False,excel仍然出现
在我读取excel中的值之后,过一段时间才显示,不是立刻出来,大约5秒左右.
以下是我的所有代码了.
Private Sub BtOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtOpenFile.Click
Dim myStream As Stream = Nothing
Try
OpenFileDialog1.InitialDirectory = "c:"
OpenFileDialog1.Filter = "Excel files (*.xls;*.xlsx)|*.xls;*.xlsx|Csv files(*.csv)|*.csv|All files (*.*)|*.*"
OpenFileDialog1.FilterIndex = 1
OpenFileDialog1.RestoreDirectory = True
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = OpenFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
Dim excelApp As Excel.Application = New Excel.Application ' CreateObject("Excel.Application")
excelApp.Visible = False
Dim excelWorkbook As Excel.Workbook
Dim excelSheet As Excel.Worksheet
Dim excelRange As Excel.Range '定义工作区域
excelWorkbook = excelApp.Workbooks.Open(OpenFileDialog1.FileName)
excelApp.Workbooks.Add(True)
excelSheet = excelWorkbook.Worksheets(1)
excelSheet.Activate()
MsgBox(excelSheet.Cells(3, 1).value.ToString, MsgBoxStyle.Information, "xs")
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Return
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
Catch ex As Exception
MsgBox(ex.Message)
Return
End Try
End Sub