码迷,mamicode.com
首页 > Web开发 > 详细

ExtAspNet从DataTable里导出Excel

时间:2017-06-27 23:36:45      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:file   for   tostring   tab   send   net   osi   csharp   app   

 protected void btn_ToExcel_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
        Response.ContentType = "application/excel";
        Response.Write(GetDataTableHtml((DataTable)ViewState["tt"]));
        Response.End();
    }


private string GetDataTableHtml(DataTable dt)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">");
        sb.Append("<tr>");
        foreach (DataColumn column in dt.Columns)
        {
            sb.AppendFormat("<td>{0}</td>", column.Caption);
        }
        sb.Append("</tr>");

        foreach (DataRow row in dt.Rows)
        {
            sb.Append("<tr>");
            for (int i = 0; i < dt.Columns.Count;i++ )
            {
                string html = row[i].ToString();
                sb.AppendFormat("<td>{0}</td>", html);
            }
            sb.Append("</tr>");
        }
        sb.Append("</table>");
        return sb.ToString();
    }

ExtAspNet从DataTable里导出Excel

标签:file   for   tostring   tab   send   net   osi   csharp   app   

原文地址:http://www.cnblogs.com/tlnshuju/p/7087333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!