Import Multiple Unknown Excel Spreadsheets in .Net
|
Excel VBADiscuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Import Multiple Unknown Excel Spreadsheets in .Net
I am trying to create an application that can read multiple sheets out of an Excel Workbook. Opening the connection and reading the data out of a known sheet is no problem. I am running into a wall when trying to extract the data from an unknown sheet and multiple sheets. I want to be able to read the data from all of the sheets in a workbook. Here is the code i am currently using:
MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=" & FileLoadName & "; " & _
"Extended Properties=Excel 8.0;")
' Select the data from Sheet1 of the workbook.
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [Sheet1$]", MyConnection)
DS = New System.Data.DataSet
MyCommand.Fill(DS)
'######################################
'Populate grid on GUI
'######################################
DataGrid1.DataSource = DS.Tables(0).DefaultView
This brings in all of the data on Sheet1, but doesn't display Sheet2 or Sheet3!!! Any help would be appreciated.
You'll probably need to use the Excel API to find the sheet number, and the number of columns (fields won't be the same). If you know there is three, you can union the results, only if the fields are the same.