将数据集导出到Excel [英] Exporting Dataset to excel

查看:90
本文介绍了将数据集导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据集导出到excel.我试过了,但是没有生成任何文件..

I want to export dataset to excel. I tried but it is not generating any file..

public void ExportDataSetToExcel(DataSet ds, string filename)
        {
            HttpResponse response = HttpContext.Current.Response;

            // first let's clean up the response.object   
            response.Clear();
            response.Charset = "";

            // set the response mime type for excel   
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

            // create a string writer   
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    // instantiate a datagrid   
                    DataGrid dg = new DataGrid();
                    dg.DataSource = ds.Tables[0];

                    dg.DataBind();
                    dg.RenderControl(htw);
                    response.Write(sw.ToString());
                    response.End();
                }
            }

        }

推荐答案

尝试在您的代码后面添加以下代码:

Try adding below code in your code behind :

public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}



另请参考以下链接:
http://toniyojackson.blogspot.in/2010/10/gridview-export-to- excel.html [ ^ ]



Also refer below link:
http://toniyojackson.blogspot.in/2010/10/gridview-export-to-excel.html[^]


嗨 您在哪种情况下调用此函数?
实际上,应该在页面加载事件之前调用它.
否则它将无法正常工作.
hi in which event you are calling this function?
Actually before the page load event it should be called.
Otherwise it will not work.


这篇关于将数据集导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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