从asp c中的datatable生成excel [英] generate excel from datatable in asp c#

查看:68
本文介绍了从asp c中的datatable生成excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在excel中下载数据表的数据。在VS 2005中

i尝试了很多方法,但现在我没有用,我正在使用以下方法。



i want to download data of datatable in excel. in VS 2005
i tried many method but its no use now i am using following approach.

public void ExportToExcel(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                string filename = "Breakup wise Stock Summary.xls";
                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                DataGrid dgGrid = new DataGrid();
                dgGrid.DataSource = dt;
                dgGrid.DataBind();

                
                dgGrid.RenderControl(hw);
                //Response.ContentType = application/vnd.ms-excel;
                Response.ContentType = "application/vnd.ms-excel";
                //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
                this.EnableViewState = false;
                Response.Write(tw.ToString());
                Response.End();
            }
        }



但是收到错误。

sys.webforms.PageRequestManagerParsererrorException:从服务器收到的邮件可以不被解析。此错误的常见原因是通过调用Response.Write(),响应过滤器,HttpModules或服务器跟踪来修改响应时。

详细信息:解析

附近的错误

推荐答案

请在以下链接中找到解决的示例



在ASP.NET中将Gridview数据导出到Excel [ ^ ]



谢谢
Please find solved example in following link

Export Gridview Data to Excel in ASP.NET[^]

Thanks


试试这个,



Try This,

public void ExportToExcel()
{
this.EnableViewState = false;
Response.Charset = string.Empty;
BindDataGrid(false); //Function for Binding Grid,false indicates allopaging is false
   Response.AddHeader("content-disposition",   "attachment;filename=CategoryList_" + System.DateTime.Now.ToShortDateString() + ".xls");
            Response.ContentType = "application/ms-excel";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            System.Web.UI.HtmlControls.HtmlForm htmlform = new System.Web.UI.HtmlControls.HtmlForm();
            htmlform.Attributes["runat"] = "Server";
            dgvCategory.Parent.Controls.Add(htmlform);
            htmlform.Controls.Add(dgvCategory);
            htmlform.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            //gvEmployeePrint.Visible = false;
            Response.End();
}


这篇关于从asp c中的datatable生成excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆