C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » Windows »

to open both the .xls & .xlsx files.


Posted Date: 01 Nov 2009      Posted By: Dhanya       Member Level: Silver     Points: 1   Responses: 1



Here I want to open both the .xls & .xlsx files.How can I open both these files.
I will paste here my friend's 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");
}
}





Responses

Author: Anil Kumar Pandey    02 Nov 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

hi,

you can use the Microsoft 12.0 object library for xls and xlsx, and in the code change this as


if (Path.GetExtension(FileName) == ".xls") || Path.GetExtension(FileName) == ".xlsx")
{
}


Thanks & Regards
Anil Kumar Pandey



Post Reply
You must Sign In to post a response.
Next : To add the contents of a selected excel file to the datagridview.
Previous : Connection string for the database
Return to Discussion Forum
Post New Message
Category: Windows

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use