Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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());
}
	
}

Posted

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?

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...