2013-07-10 7 views
1

Excel 시트를 DataGridview로 가져 오려고하는데 오류가 발생합니다 : "데이터베이스 또는 개체가 읽기 전용입니다." 그러나 참조하는 통합 문서에 읽기 전용 특성이 적용되지 않았습니다. 즉, 내 응용 프로그램이 실행중인 경우 연결되어있는 통합 문서가 이미 열려 있으므로 이것이 내가이 오류와 충돌하는 이유라고 생각합니다. 통합 문서가 열려 있으므로 데이터 집합을 채울 때 읽기 전용으로 시스템에 나타납니다.Excel 시트를 DataGridview로 가져 오기 - OleDB를 사용하려면 통합 문서를 닫아야합니까?

이 가정에 맞습니까? 연결하려는 통합 문서가 열려 있으면 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)  

답변

1

당신이 connstring을 변경하려고 했습니까; set ReadOnly = True

그래도 작동하지 않으면 통합 문서의 복사본을 만든 다음 해당 복사본을 쿼리 할 수 ​​있습니다. 그렇게하면 마지막으로 저장된 통합 문서를 가져옵니다. 문제가 발생하면 복사하기 전에 열려있는 통합 문서에서 저장 작업을 수행 할 수 있습니다.

또는 코드를 실행하기 전에 사용자에게 통합 문서를 닫으라고 요청하십시오.

+0

통합 문서는 응용 프로그램을 사용하는 동안 항상 열려 있어야합니다. 시트를 새 통합 문서로 복사하고 필요하지 않은 열을 제거하고 저장하고 닫은 다음 쿼리를 수행하고 파일을 삭제했습니다. 나는 내일 기회가있을 때 코드를 게시 할 것이다. 재 보증에 감사드립니다. 나는 당신을 투표 하겠지만 분명히 나는 ​​아직 할 수 없다. –

1
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