Last post Apr 21, 2009 06:23 PM by Prateek_Singh1982
Apr 20, 2009 08:12 PM|Prateek_Singh1982|LINK
I have to display data from excel sheet into my gridview and also I need to figure out a way to store data into excel files from my web page and performing editing if required. I am using C# 3.5, VS 2008.
Please help me with some sample code or any link where I can find more material to study.
Apr 20, 2009 09:06 PM|rajesh876|LINK
Look at the following code.It might help you.Ask me if you have any doubts.I am sending the working code to you.
protected void btnUpload_Click(object sender, EventArgs e)
pnlUpload.Visible = true; //display the file upload control after clicking the Upload button
protected void btnUploadFile_Click(object sender, EventArgs e)
//Reading and displaying the excel sheet data in gridview
OleDbConnection DBConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;" + " Data Source= " + Server.MapPath("~/App_Data/Running.xls") + ";" +
"Extended Properties=Excel 8.0;");
string SQLString = "SELECT * FROM [Sheet1$]";
OleDbCommand DBCommand = new OleDbCommand(SQLString, DBConnection);
IDataReader DBReader = DBCommand.ExecuteReader();
GridView1.DataSource = DBReader; //Assigning the Data from excel sheet to gridview
GridView1.DataBind(); //bind gridview
Label1.Text = "File Uploaded:" + ExcelFileUpload.PostedFile.FileName;
Button1.Visible = true;
catch (System.NullReferenceException ex)
Label1.Text = ex.Message;
Ok now you want to store data in to excel from gridview.Initially get the data in to a datatable and then passit as a parmeter to the following function.It will work.
public void ExportToExcel(DataTable DT)
if (DT != null)
//clear anything in buffer
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dc in DT.Columns)
Response.Write(tab + dc.ColumnName);
tab = "\t";
foreach (DataRow dr in DT.Rows)
tab = "";
for (i = 0; i < DT.Columns.Count; i++)
Response.Write(tab + dr[i].ToString());
tab = "\t";
Apr 21, 2009 06:23 PM|Prateek_Singh1982|LINK
thank you so much,
I have used 1st part of the code and its working fine. as for 2nd part i am suure its gonna work otherwise i will get back to you.
i appreciate your help, thanks again