asp.net导出excel (这是)通过画一个html 实现的

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

FinalHTML.Append(GetTableHTML(ds));

FinalHTML.AppendLine("");

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("");
        for (int i = 0; i < columns.Length; i++)
        {
            FinalHTML.AppendFormat("{0}", colNames[i]);
        }
        FinalHTML.AppendLine("");
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            FinalHTML.AppendLine("");
            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}", val);
            }
            FinalHTML.AppendLine("");
        }

FinalHTML.AppendLine("");
        return FinalHTML.ToString();
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值