分享

Asp.net 导出Excel

 昵称10504424 2013-07-24
    1. protected void btn_out_Click(object sender, EventArgs e)  
    2.    {  
    3.        Response.ClearContent();  
    4.        Response.AddHeader("content-disposition""attachment; filename=DATASHOW.xls");  
    5.        Response.ContentType = "application/excel";  
    6.        Response.Write(GetGridTableHtml(gv_show));  
    7.        Response.End();  
    8.    }  
    9.    private string GetGridTableHtml(Grid grid)  
    10.    {  
    11.        StringBuilder sb = new StringBuilder();  
    12.   
    13.        sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">");  
    14.   
    15.        sb.Append("<tr>");  
    16.        foreach (GridColumn column in grid.Columns)  
    17.        {  
    18.            sb.AppendFormat("<td>{0}</td>", column.HeaderText);  
    19.        }  
    20.        sb.Append("</tr>");  
    21.   
    22.   
    23.        foreach (GridRow row in grid.Rows)  
    24.        {  
    25.            sb.Append("<tr>");  
    26.            foreach (object value in row.Values)  
    27.            {  
    28.                string html = value.ToString();  
    29.                // 处理CheckBox   
    30.                if (html.Contains("box-grid-static-checkbox"))  
    31.                {  
    32.                    if (html.Contains("box-grid-static-checkbox-uncheck"))  
    33.                    {  
    34.     html = "×";  
    35.                    }  
    36.                    else  
    37.                    {  
    38.     html = "√";  
    39.                    }  
    40.                }  
    41.   
    42.                // 处理图片   
    43.                if (html.Contains("<img"))  
    44.                {  
    45.                    string prefix = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, "");  
    46.                    html = html.Replace("src=\"""src=\"" + prefix);  
    47.                }  
    48.   
    49.                sb.AppendFormat("<td>{0}</td>", html);  
    50.            }  
    51.            sb.Append("</tr>");  
    52.        }  
    53.   
    54.        sb.Append("</table>");  
    55.   
    56.        return sb.ToString();  
    57.    }  
  1. ~

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多