如何在使用Oledb的同时使用其他用户阅读Excel?

我有一个共享驱动器的Excel,我的应用程序正在使用Oledb连接从Excel中读取数据到DataGridView。

cn = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;" + "data source=" + skuPath + ";Extended Properties=""Excel 12.0;HDR=YES;""") q1 = "select * from [" + year + "$B4:AM300]" da = New System.Data.OleDb.OleDbDataAdapter(q1, cn) Try cn.Open() da.Fill(ds, "Table1") Catch e As OleDb.OleDbException Dim errorMsg As String Dim i As Integer errorMsg = "" For i = 0 To e.Errors.Count - 1 errorMsg += "Index #" & i.ToString() & ControlChars.Cr _ & "Message: " & e.Errors(i).Message & ControlChars.Cr _ & "NativeError: " & e.Errors(i).NativeError & ControlChars.Cr _ & "Source: " & e.Errors(i).Source & ControlChars.Cr _ & "SQLState: " & e.Errors(i).SQLState & ControlChars.Cr Next i End Try cn.Close() dt = ds.Tables(0) 

当excel文件已经被另一个用户打开时,你会在excel中得到这个通知: 在这里输入图像说明

在这些情况下,我的代码在最后一行返回这个错误:

 An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll Additional information: Cannot find table 0. 

所以我明白是因为文件正在使用,那么整个连接没有任何回应,因此数据表是空的。 我发现了一些方法来确定一个文件是否正在使用,但没有关于如何从正在使用的文件读取。 可能吗? 如果是的话如何?

请记住,我只需要阅读文件,如果它可能总是打开它只是一个很好的!

你不能。 即使使用ODBC(只读),也必须先closures打开的文件。 参考: http : //forums.asp.net/t/1083489.aspx?open+a+Microsoft+Jet+OLEDB+4+0+connection+to+excel+file+read+only