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();
}