ExtAspNet依据Grid导出Excel
阅读原文时间:2023年07月14日阅读:3

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("

");
sb.Append(""); foreach (GridColumn column in grid.Columns) { sb.AppendFormat("", column.HeaderText); } sb.Append("");

    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();  
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章

{0}