다음 코드를 사용하여 테이블 이름 (워크 시트 이름)과 Excel 스프레드 시트에서 열 이름을 검색하는 것은 매우 행운이었습니다.
Private Sub GetWorksheetData
Dim xlBaseConnStr1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=XLS;Extended Properties=""Excel 8.0;HDR=Yes"""
Dim xlBaseConnStr2 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=XLS;Extended Properties=""Excel 12.0 Xml;HDR=YES"""
Dim xlName As String
Dim conStr As String
Dim oDia As New OpenFileDialog
oDia.ShowDialog()
xlName = oDia.FileName
If xlName = "" Then
Exit Sub
End If
Dim extType As String = Path.GetExtension(xlName)
Select Case extType
Case ".xls"
conStr = xlBaseConnStr1.Replace("XLS", xlName)
Case ".xlsx"
conStr = xlBaseConnStr2.Replace("XLS", xlName)
Case Else
MessageBox.Show("Unrecognized file type")
Exit Sub
End Select
Dim dtSheets As New DataTable
Using cn As New OleDbConnection(conStr)
cn.Open()
dtSheets = cn.GetSchema("Columns")
End Using
DataGrid1.ItemsSource = dtSheets.DefaultView
End Sub
위의 코드는 임의의 Excel 스프레드 시트에서 다음 데이터를 반환합니다.
당신은이 작업을 수행하려면 다음과 같은 네임 스페이스를 가져와야합니다 : 액세스하려는 스프레드 시트 매크로가있는 경우
이
Imports System.Data.OleDb
Imports System.Data
Imports Microsoft.Win32
Imports System.IO
, 위의 코드가 실패 할 수 있습니다.
OleDbDataReader는 모든 Excel 2010 파일을 읽을 수 있어야합니다. 구체적으로 문제의 원인은 무엇입니까? OleDbDataReader는 전달, 읽기 전용이므로 매우 빠릅니다. – Stewbob
im 'objConn.GetOleDbSchemaTable (OleDbSchemaGuid.Columns, Nothing)을 사용하여 Excel 시트에서 열 이름을 가져 오는 동안'예기치 않은 외부 데이터베이스 드라이버 오류 (1) '오류가 발생했습니다. – chinnu