导入Excel工作表到datagridview – 工作簿需要closures才能使用OleDB?

我试图导入一个Excel工作表到datagridview,但我遇到了一个错误:“数据库或对象是只读的。 但是,我正在引用的工作簿没有应用只读属性。 话虽如此,我连接的工作簿已经打开,如果我的应用程序正在运行,所以我怀疑这是我遇到这个错误的原因。 工作簿是打开的,因此在尝试填充数据集时,系统显示为只读。

我对这个假设是否正确? 如果我连接的工作簿是打开的,他们是一种使用OleDB连接将Excel工作表导入到datagridview的方法吗? 如果没有,有没有其他的方式来填充这个datagridview,而不必通过我的工作表进行大规模的循环? 我的代码如下:

Try 'connect to Excel data source and set gridview equal to dataset (entire sheet should be visible in gridview) Dim MyConnection As System.Data.OleDb.OleDbConnection Dim MyCommand As System.Data.OleDb.OleDbDataAdapter Dim connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & StatVar.workbookName & ";Extended Properties=""Excel 12.0;HDR=YES;Readonly=False"";" 'may need to use different MS provider and lower OLEDB for .xls files (Microsoft.Jet.OLEDB4.0...Excel 8.0) kind of sketchy though MyConnection = New System.Data.OleDb.OleDbConnection(connstring) 'create a new data connection to Excel data source MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Budget$]", MyConnection) 'query the sheet - select all data MyCommand.TableMappings.Add("Table", "Table") 'map data selection as table StatVar.DtSet1 = New System.Data.DataSet 'create new data set MyCommand.Fill(StatVar.DtSet1) 'fill data set with table Form14.DataGridView1.DataSource = StatVar.DtSet1.Tables(0) 'populate gridview with data set table MyConnection.Close() Form14.ShowDialog() Catch exc As Exception MessageBox.Show("There was a problem loading this database. Please contact an administrator if the problem continues." & vbNewLine & vbNewLine & "Error: " & exc.Message) End Try 

我遇到这里的错误:

 MyCommand.Fill(StatVar.DtSet1) 

你有没有尝试改变connstring; 设置ReadOnly = True

如果这不起作用,您可以制作工作簿的副本,然后查询该副本。 如果你这样做,它会得到你上次保存的工作簿。 如果这是一个问题,您可以在复制之前在打开的工作簿上进行保存。

或者更好的是,只需要让用户在运行代码之前closures工作簿。

  Try Cursor = Cursors.WaitCursor 'can't populate gridview with an open file using OLEDB - need to export the Budget sheet to a new file, close it, connect with OLEDB, then delete the temp Budget.xls file just created 'copy Budget sheet to new workbook, delete unneeded columns, save file as Budget.xls, close workbook Dim filenm = "W:\TOM\ERIC\Budget Temp\Budget.xls" StatVar.xlApp.Sheets("Budget").Copy() StatVar.xlApp.ActiveWorkbook.Sheets("Budget").Columns("C:DY").Delete() StatVar.xlApp.ActiveWorkbook.SaveAs("W:\TOM\ERIC\Budget Temp\Budget.xls") StatVar.xlApp.ActiveWorkbook.Close(True) 'connect to Excel data source and set gridview equal to dataset (entire sheet should be visible in gridview) Dim MyConnection As System.Data.OleDb.OleDbConnection Dim MyCommand As System.Data.OleDb.OleDbDataAdapter Dim connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & filenm & ";Extended Properties=""Excel 12.0;HDR=YES;Readonly=False"";" 'may need to use different MS provider and lower OLEDB for .xls files (Microsoft.Jet.OLEDB4.0...Excel 8.0) kind of sketchy though MyConnection = New System.Data.OleDb.OleDbConnection(connstring) 'create a new data connection to Excel data source MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Budget$A:F]", MyConnection) 'query the sheet - select all data in columns A:F MyCommand.TableMappings.Add("Table", "Table") 'map data selection as table StatVar.DtSet1 = New System.Data.DataSet 'create new data set MyCommand.Fill(StatVar.DtSet1) 'fill data set with table Form14.DataGridView1.DataSource = StatVar.DtSet1.Tables(0) 'populate gridview with data set table MyConnection.Close() 'delete temporary Budget.xls file created at beginning of procedure and show Budget Codes form File.Delete(filenm) Form14.TxtBoxAutoCode.Text = StatVar.xlApp.Sheets("Budget").Range("EU2").Text Form14.ShowDialog() Catch exc As Exception MessageBox.Show("There was a problem loading this database. Please contact an administrator if the problem continues." & vbNewLine & vbNewLine & "Error: " & exc.Message) Finally Cursor = Cursors.Default End Try