关于 数据源 导出excel (这是) 通过 画一个html 实现的、最简单、好理解、的代码、

来源:互联网 发布:unity3d 2d精灵动画 编辑:程序博客网 时间:2024/05/27 14:15
  private void GetExcel()
    {

        UserLogin ul = (UserLogin)Session["UserInfo"];
         DataSet ds=new DataSet();
         if (InfoMainClass.Enabled)
         {
             ds = new ChildrenManager().GetChildrenList(KeyWords.Text, Convert.ToInt32(InfoMainClass.SelectedValue));
         }
         else {
             ds = new ChildrenManager().GetChildrenList(KeyWords.Text,ul.ChildSchoolID);
         }
       
        StringBuilder where = new StringBuilder();

      
        if (!Common.NullCheck.DataSetIsNull(ds))          //  判断是 ds 里面是否 为空、、
        {
            StringBuilder FinalHTML = new StringBuilder();

            FinalHTML.AppendLine("<html> <head> <meta http-equiv=\"Content-Language\" content=\"zh-cn\"> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> </head><body style=\"padding:0;margin:0;\">");
            FinalHTML.AppendLine("<style type=\"text/css\">tr {height:25px;}</style>");


            FinalHTML.Append(GetTableHTML(ds));



            FinalHTML.AppendLine("</body></html>");

            HttpResponse resp = HttpContext.Current.Response;
            StringBuilder sb = new StringBuilder();
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            resp.ContentType = "application/ms-excel";
            resp.AppendHeader("Content-Disposition", "attachment;filename=child.xls");

            resp.Write(FinalHTML.ToString());
            resp.End();
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "NoData", "alert(\"找不到符合条件的学生!\");window.close();", true);
        }
    }

    string[] columns = { "学校名称","姓名","户籍类型","证件号","性别","出生日期","民族","户籍地址","实际居住地","父亲姓名","父亲电话","母亲姓名","母亲电话","其他监护人姓名","其他监护人与幼儿关系","其他监护人电话"
                        };
    string[] colNames = { "学校名称","姓名","户籍类型","证件号","性别","出生日期","民族","户籍地址","实际居住地","父亲姓名","父亲电话","母亲姓名","母亲电话","其他监护人姓名","其他监护人与幼儿关系","其他监护人电话"
                         };
    private string GetTableHTML(DataSet ds)
    {


        StringBuilder FinalHTML = new StringBuilder();
        FinalHTML.AppendLine("<table border=1 style=\"font-size:14px;\">");
        //生成标题行
        FinalHTML.AppendLine("<tr>");
        for (int i = 0; i < columns.Length; i++)
        {
            FinalHTML.AppendFormat("<td>{0}</td>", colNames[i]);
        }
        FinalHTML.AppendLine("</tr>");
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            FinalHTML.AppendLine("<tr>");
            for (int i = 0; i < columns.Length; i++)
            {
                string colName = columns[i];
                object val = dr[colName];
                //处理特殊情况
            if (colName.Equals("出生日期") || colName.IndexOf("Date") >= 0)
                {
                    try
                    {
                        val = Convert.ToDateTime(val).ToString("yyyy-MM-dd");
                    }
                    catch { }
                }


                FinalHTML.AppendFormat("<td style=\"vnd.ms-excel.numberformat:@\">{0}</td>", val);
            }
            FinalHTML.AppendLine("</tr>");
        }

        FinalHTML.AppendLine("</table>");
        return FinalHTML.ToString();
    }


    protected void Button1_Click(object sender, ImageClickEventArgs e)
    {
        GetExcel();
    }