Import and Export data from Excel

Posted by Sathya4260 under ASP.NET on 12/20/2010 | Points: 10 | Views : 4288 | Status : [Member] | Replies : 6
Hi Experts,

I am trying to read the datas from excel and then save the datas to database and then on selected the report it should be generated in gridview and when clicking the export to excel button this should be exported to excel .

I am just able to produce the grid view not able to import or export the data from excel. I tried a lot from other forums but none worked fr me.
Kindly provide me the full codes to do the same as soon as possible.

Thanks in advance,

Sathish Kumar S


Responses

Posted by: T.saravanan on: 12/20/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

To add Microsoft.Office.Interop.Excel Reference in your project.

For Export to Excel...http://support.microsoft.com/kb/306023

For Import from Excel..http://www.dotnetfunda.com/codes/code972-read-a-excel-file-with-sheet-wise-into-dataset-using-csharp.aspx


Cheers :)

Thanks,
T.Saravanan

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sathya4260 on: 12/20/2010 [Member] Starter | Points: 25

Up
0
Down
hi saravanan thanks fr ur timely response,

Wen i tried to use the below code to export the result of grid view to excel i got error as

RegisterForEventValidation can only be called during Render();

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving than
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}

Help me to solve this.....

Sathish Kumar S

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 12/20/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi Sathish,

Sorry for the delay....
Same error is discuss in this link...http://www.aspsnippets.com/Articles/RegisterForEventValidation-can-only-be-called-during-Render.aspx
and also look this link for export to excel...http://www.c-sharpcorner.com/uploadfile/dipalchoksi/exportxl_asp2_dc11032006003657am/exportxl_asp2_dc.aspx

Cheers :)

Thanks,
T.Saravanan

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sathya4260 on: 12/20/2010 [Member] Starter | Points: 25

Up
0
Down
I got the answer,

since i used firefox as the browser,

Just insert the below code

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Sathish Kumar S

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 12/20/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi Sathish,

Thanks for your answer sharing....

Thanks,
T.Saravanan

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Webmaster on: 12/20/2010 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hi Sathish,

Great to know that you got the answer, could you please mark appropriate post as Answer so that it will help others?

Thanks

Best regards,
Webmaster
http://www.dotnetfunda.com

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response