最近我在 Excel 2016 上遇到了一个问题。在我关闭 excel 2016 环境中的连接字符串后,系统仍然锁定 excel 无法编辑(比如正在使用的文件)。但代码在 excel 2007 和 2010 环境中运行良好(连接关闭后,该文件已准备好供我的下一个功能打开和编辑)。
在 excel 2016 环境中,文件似乎需要在自动解锁后锁定 75 秒,但在 excel 2007 和 2010 环境中不会发生这种情况
我应该在哪里改变它?
Public Function gf_ExcelConnection(ByRef strLocation As String, ByRef sWorksheetName As String, _
ByRef lExcelDataset As DataSet, ByRef iLastRow As Integer, _
ByRef sColumn As String) As Boolean
Dim strExcelConn As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strLocation & _
"; Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;"""
Dim ExcelConn As New System.Data.OleDb.OleDbConnection(strExcelConn)
Dim ExcelCommand As OleDbCommand = New OleDbCommand
Dim ExcelDataAdapter As New OleDbDataAdapter
Try
gf_ExcelConnection = True
ExcelCommand.CommandText = "SELECT * FROM [" + sWorksheetName + "A1:" & sColumn & iLastRow & "] "
ExcelDataAdapter.SelectCommand = ExcelCommand
ExcelDataAdapter.SelectCommand.Connection = ExcelConn
MsgBox(103)
ExcelConn.Open()
ExcelDataAdapter.Fill(lExcelDataset, "ImportTable")
ExcelConn.Close()
ExcelConn.Dispose()
MsgBox(104)
Catch ex As Exception
gf_ExcelConnection = False
gclsError.gf_ErrorHandler("InvalidIncorrectFile", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error, False)
ExcelConn.Close()
Finally
ExcelConn.Close()
ExcelConn.Dispose()
ExcelConn = Nothing
MsgBox(105)
End Try
End Function