rfazendeiro Posted October 7, 2005 Posted October 7, 2005 Hi to all, I'm currently loading an excel to a DataSet and show the information in a Datadrig. Every month a new excel with the same format arrives and my application will process the information. My problem is that the name of the excel sheets don't always come with the same name, and there is no pattern to those names. Is there anyway for me to know the sheet name before i make the select ?? I have this simple code to load the Excel file to a datagrid private void btnExcel_Click(object sender, System.EventArgs e) { try { System.Data.DataSet DS; System.Data.OleDb.OleDbDataAdapter MyCommand; System.Data.OleDb.OleDbConnection MyConnection; MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " + "data source=C:\\Book1.xls; " + "Extended Properties=Excel 8.0;"); MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [sheet1$]", MyConnection); DS = new System.Data.DataSet(); MyCommand.Fill(DS); MyConnection.Close(); this.dgExcel.DataSource = DS; } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } Quote
SonicBoomAu Posted October 9, 2005 Posted October 9, 2005 Couldn't you use a Open File Dialog box to select the file? Sorry Didn't read the question properly. Couldn't you use the active sheet instead of sheet1? Quote Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -- Rick Cook, The Wizardry Compiled
rfazendeiro Posted October 10, 2005 Author Posted October 10, 2005 And how do i do that?? what i do is just open a ODBC. how can i get the active sheet? Quote
herilane Posted October 10, 2005 Posted October 10, 2005 Worksheets in an Excel workbook are equivalent to tables in a database. Have a look here for examples of retrieving table information. Quote
rfazendeiro Posted October 12, 2005 Author Posted October 12, 2005 GREAT!!! that worked fine...thx for the help Quote
Recommended Posts