| Sender |
Dhanya
|
| Recipient(s) |
vipul
|
| Date |
01 Nov 2009
|
Thanks
|
Hai vipul,
Thank you for your response.Here I want to open both the .xls & .xlsx files.How can I open both these files. I will paste here my code.please check it..
public void setExcelFileAsDataSourceToDataGridView(string FileName) { OleDbConnection objConn; OleDbDataAdapter oleDA; DataSet ds; //Check Whether file is xls file or not if (Path.GetExtension(FileName) == ".xlsx") { try { //Create a OLEDB connection for Excel file string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FileName + ";" + "Extended Properties=Excel 8.0;";
objConn = new OleDbConnection(connectionString); if (objConn.State == ConnectionState.Closed) { objConn.Open();
oleDA = new OleDbDataAdapter("select * from []", objConn); ds = new DataSet(); //Fill the Data Set oleDA.Fill(ds); //Set DataSource of DataGridView dataGridView1.DataSource = ds.Tables[0]; ds.Dispose(); oleDA.Dispose(); } objConn.Dispose(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show("Please select Excel File"); } }
|