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

ExtAspNet依据Grid导出Excel

时间:2017-06-14 20:07:56      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:str   tostring   turn   class   string   end   return   values   click   

 protected void Button1_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
        Response.ContentType = "application/excel";
        Response.Write(GetGridTableHtml(Grid1));
        Response.End();
    }
    private string GetGridTableHtml(Grid grid)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">");
        sb.Append("<tr>");
        foreach (GridColumn column in grid.Columns)
        {
            sb.AppendFormat("<td>{0}</td>", column.HeaderText);
        }
        sb.Append("</tr>");

        foreach (GridRow row in grid.Rows)
        {
            sb.Append("<tr>");
            foreach (object value in row.Values)
            {
                string html = value.ToString();
                sb.AppendFormat("<td>{0}</td>", html);
            }
            sb.Append("</tr>");
        }
        sb.Append("</table>");
        return sb.ToString();
    }

ExtAspNet依据Grid导出Excel

标签:str   tostring   turn   class   string   end   return   values   click   

原文地址:http://www.cnblogs.com/yfceshi/p/7010639.html

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