To Read and Write to Excel file

string strFilePath = FileUpload1.PostedFile.FileName;
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ strFilePath+";Extended Properties=Excel 8.0;";

//write into excel file
OleDbConnection oldbcon=new OleDbConnection(connectionString);
OleDbCommand oldc = new OleDbCommand("Insert into [Age$](Name,Age) values('Nirav',23)", oldbcon);
oldbcon.Open();
int i=oldc.ExecuteNonQuery();
oldbcon.Close();

//Read from Excel file
DataSet cities = new DataSet();
OleDbDataAdapter oledbAd = new OleDbDataAdapter("Select * from [Age$]", connectionString);
oledbAd.Fill(cities);
GridView1.DataSource = cities;
GridView1.DataBind();

//The Excel File

Comments

Yograj said…
It's very helpfull code.


Thanks.
Yograj Pandey

Popular posts from this blog

Chanakya's Quotes - Worth reading a million times…

Why the Elephants Don't Run?