控件导出到excel 样式自己设置

 ReportToExcel(控件id); 

public void ReportToExcel(Repeater dd)
    {
        string sname = Convert.ToString("导出到excel.xls");
        HttpContext.Current.Response.Charset = "utf-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(sname, System.Text.Encoding.UTF8));
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        dd.RenderControl(htw);
        HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\"><title>Copyright by SDU</title></head><body><center><table style='border:solid .5pt Black;'>");
        StringBuilder strAppend = new StringBuilder();
        strAppend.Append("<tr align='center'><th rowspan='2' style='width: 6%;border:solid .5pt Black;'>姓名</th>");
        strAppend.Append(" <th rowspan='2' style='width: 6%;border:solid .5pt Black;'>学号</th>");
        strAppend.Append(" <th rowspan='2' style='width: 8%;border:solid .5pt Black;'>院系</th>");
        strAppend.Append("<th rowspan='2' style='width: 8%;border:solid .5pt Black;'>班级</th>");
       
        HttpContext.Current.Response.Write(strAppend.ToString());
        HttpContext.Current.Response.Write(sw.ToString());
        StringBuilder strResult = new StringBuilder();
        strResult.Append("<tr>");
        strResult.Append("<td colspan='11' class='style1' style='border:solid .5pt Black;'>");
        strResult.Append("</td>");
        strResult.Append("<td class='style1' style='border:solid .5pt Black;'>");
        strResult.Append("<asp:Label runat='server' ID='xftotal'>"+xfresult+"</asp:Label>");
        strResult.Append("</td>");
        strResult.Append("<td class='style1' style='border:solid .5pt Black;'>");
        strResult.Append("<asp:Label runat='server' ID='zsftotal'>"+zsfresult+"</asp:Label>");
        strResult.Append("</td>");
        strResult.Append("<td class='style1' style='border:solid .5pt Black;'>");
        strResult.Append("<asp:Label runat='server' ID='qianfeitotal'>"+result+"</asp:Label>");
        strResult.Append("</td>");
        strResult.Append("</tr>");
        HttpContext.Current.Response.Write(strResult);
        HttpContext.Current.Response.Write("</table></center></body></html>");
        HttpContext.Current.Response.End();
    }

posted @ 2014-01-14 14:02  牡丹  阅读(136)  评论(0)    收藏  举报