本文介绍了如何在asp.net中将gridview数据导出为ex​​cel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友..

我希望代码将数据导出到excel ..请帮助我..

提前谢谢..

Hello friends..
I want code to export data to excel..please help me..
Thanks in advance..

推荐答案



<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>



和.cs页面:


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 */
       }





谢谢

JMD



thanks
JMD


这篇关于如何在asp.net中将gridview数据导出为ex​​cel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 05:40