Click here to Skip to main content
12,457,770 members (55,672 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hello friends..
I want code to export data to excel..please help me..
Thanks in advance..
Posted 6-Jan-13 19:33pm
Comments
adriancs 8-Jan-13 8:30am
   
save it as a html table file and name it with a file extension of ".xls".
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
v2
Comments
AshishChaudha 7-Jan-13 23:14pm
   
my +5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

on aspx page:
<asp:linkbutton id="lnkExporttoExcel" runat="server" causesvalidation="false" cssclass="link" onclientclick="return confirm('Are you sure you want to export this report to excel?');" onclick="OnLnkExportToExcelClick" xmlns:asp="#unknown">Export to Excel</asp:linkbutton>
and on .cs page :
 protected void OnLnkExportToExcelClick(object sender, EventArgs e)
        {
       
 Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=CodeProjectExcelHelp_Report.xls");
                Response.Charset = "iso-8859-2";
                Response.ContentType = "application/ms-excel";
               
                using (StringWriter StringWriter = new System.IO.StringWriter())
                {
                    using (HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter))
                    {
                        grdUserTaskDetails.AllowPaging = false;//grdUserTaskDetails is my gridview name
                        grdUserTaskDetails.Attributes.AddAttributes(HtmlTextWriter);
                        GetUserTaskDetails();//get your data here
                        MergeRows(grdUserTaskDetails);//formate your repeated rows
                        grdUserTaskDetails.RenderControl(HtmlTextWriter);
                      }
                    string str = "User E-mail Id : " + User.Identity.Name + "<br />" + "Start Date : " + startDate.ToString("dd/MM/yyyy") + "<br /> End Date : " + endDate.ToString("dd/MM/yyyy") + "<br />";
                    Response.Write(str);
                    Response.Write(StringWriter.ToString());
}

public override void VerifyRenderingInServerForm(Control control)
       {
           /* Verifies that a Form control was rendered */
       }

thanks
JMD
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

hi Aysha Patel,

then please try this....i think its remove your error...
goto in Source code of your page and add EnableEventValidation="false" property in @ page derivative...

like :-
<!<%@ Page Language="C#" MasterPageFile="~/MasterHome.master" AutoEventWireup="true"  EnableEventValidation="false" CodeFile="Register.aspx.cs" Inherits="Register" Title="Untitled Page" %>
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

You can see this article, there are 8 solutions, solution five may help you a lot.
9 Solutions to Export Data to Excel for ASP.NET[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

hi friend!!
its very easy but after very hard work....just paste it with making minute changes..

string s = txtreportname.Text; //textbox for your excel name
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + s + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter StringWriter = new System.IO.StringWriter();
HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
GridView2.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();
after it just paste this method not matter that it is blank....

public override void VerifyRenderingInServerForm(Control control)
   {
 
   }

i hope it will help you...
  Permalink  
Comments
Aysha Patel 9-Jan-13 6:38am
   
Thanks to all....
I have written the code but i mgetting following error...I dont know what is it...please help ...


Error 35 'controls_ExcelImportData.VerifyRenderingInServerForm(System.Web.UI.Control)': no suitable method found to override

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 19 Feb 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100