下面的代碼是打開excel文件,並從中讀取它看完之後,我要處理的Excel文件可以刪除它:如何釋放excel文件在vb.net
Dim strNewPath As String = Server.MapPath("~/UploadedExcel/" & strFileName & strFileType)
'Connection String to Excel Workbook
If strFileType.Trim = ".xls" Then
connString2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strNewPath & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=2"""
ElseIf strFileType.Trim = ".xlsx" Then
connString2 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strNewPath & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
End If
query = "SELECT * FROM [Sheet1$]"
'Create the connection object
conn2 = New OleDbConnection(connString2)
'Open connection
If conn2.State = ConnectionState.Closed Then conn2.Open()
'Create the command object
cmd = New OleDbCommand(query, conn2)
da = New OleDbDataAdapter(cmd)
ds = New DataSet()
da.Fill(ds, "Staff")
' up.insertExcel(ds)
da.Dispose()
你有沒有試過'使用conn2 =新的OleDbConnection ...'? –