knowing the excel sheet name

rfazendeiro

Centurion
Joined
Mar 8, 2004
Messages
110
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


Code:
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());
	}
		
}
 
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?
 
Back
Top