将数据表直接导出到Excel [英] Export datatable to Excel directly

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

问题描述

我可以在不使用for循环的情况下将数据表导出到Excel吗?
我想直接将数据从数据表导出到Excel工作表.
我已经看到了他们用于循环并逐行发布值的所有示例.

有可能吗?

如果有人建议,....

can i Export datatable to Excel without using for loop.
i want to export the datas directly from datatable to excel sheet.
i have seen all the examples that they have used for loop and posting the values line by line.

Is it possible.

If anybody suggest please....

推荐答案

9将数据导出到Excel for ASP.NET的解决方案 [ ^ ]


尝试:

Try:

public static void ExportToExcel(DataTable dt, 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.ContentType = "application/vnd.xls";
    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 = dt;
            dg.DataBind();
            dg.RenderControl(htw);
            response.Write(sw.ToString());
            response.End();
        }
    }
}



来源: http://forums.asp.net/t/1648661.aspx/1 [ ^ ]



source: http://forums.asp.net/t/1648661.aspx/1[^]


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

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