c#导出excel文档。 - 古月容博客-php个人技术博客-技术交流分享

2018
IT资讯信息分享

c#导出excel文档。

 protected void Button2_Click(object sender, EventArgs e){            
        string sel_idlist = Request.Form["testpaper_list"];
        string bjtype=Request.Form["ddl_classes"];     
        string contsql="select * from ks_user where user_type=0 and user_type_Id in ('" + bjtype + "')";
        DataTable swyDt = mySql.DsSql_ks(contsql).Tables[0];                      
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;//设置缓冲输出
        HttpContext.Current.Response.Charset = "UTF-8";//设置输出流的HTTP字符集

        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("学生成绩", System.Text.Encoding.UTF8) + ".xls\"");
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
        HttpContext.Current.Response.ContentType = "application/ms-";
        //_page.EnableViewState = false;//是否保持视图状态
       config.ajax_ResponseHead();
       HttpContext.Current.Response.Write(HTML(swyDt,sel_idlist));
        HttpContext.Current.Response.End();
        
       }
    
     private string HTML(DataTable dt,string sel_idlist){

    string[] arr_sel = sel_idlist.Split(',');
       
        StringBuilder strHtml = new StringBuilder();
        strHtml.Append("<table>");
        strHtml.Append("<tr >");
        
        strHtml.Append("<td style=\"border:1px solid #000000\">编号</td>");
        strHtml.Append("<td style=\"border:1px solid #000000\">姓名</td>");
        for (int i = 0; i < arr_sel.Length; i++)
        {
            strHtml.Append("<td style=\"border:1px solid #000000\">" + arr_sel[i] + "</td>");      
        }
        
        strHtml.Append("</tr>");
        int ii = 1;
       
 
        
          foreach (DataRow dr in dt.Rows){
            strHtml.Append("<tr>");
          
             strHtml.Append(" <td style=\"border:1px solid #000000\">" + ii + "</td>");
              strHtml.Append("<td style=\"border:1px solid #000000\">"+dr["user_realname"].ToString()+"</td>");
            for (int s = 0; s < arr_sel.Length; s++)
            {
                strHtml.Append(" <td style=\"border:1px solid #000000\">" + class_user.get_user_testpaper_num(Convert.ToInt32(arr_sel[s]),Convert.ToInt32(dr["id"])) + "</td>");
            }
            strHtml.Append("</tr>");
            ii++;
        }
       
        strHtml.Append("</table>");

        return strHtml.ToString();
    }

×

感谢您的支持,我们会一直保持!

扫码支持
请土豪扫码随意打赏

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

打赏作者
版权所有,转载注意明处:古月容个人博客 » c#导出excel文档。
标签: 导出excel

发表评论

表情

网友评论(0)