关于 数据源 导出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();
}
{
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();
}
- 关于 数据源 导出excel (这是) 通过 画一个html 实现的、最简单、好理解、的代码、
- 这是一个gridview导出到Excel的代码方法
- 最简单的Excel导出
- 最简单不过的数据库导出Excel文件代码
- 最简单实用的GridView导出到Excel表格代码
- 通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
- 通过一个简单的C程序汇编代码理解计算机是怎么工作的
- 通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
- 通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
- Web应用导出Excel报表的简单实现(HTML)
- Web应用导出Excel报表的简单实现(HTML)
- 简单的实现excel导出
- 最简单的Asp导出Excel
- PHP导出Excel最简单的方法
- excel最简单的导入导出
- ASP 最简单导出excel的方法
- 通过反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
- 通过反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
- Java_内存分区堆(heap) 栈(stack)和方法区(method)
- WinCE系统下BootLoader的开发
- Oracle trunc()函数的用法
- Windows CE环境下无线网卡的自动安装
- 撰写毕业论文中word公式编辑器的学习使用(二)——公式编辑器
- 关于 数据源 导出excel (这是) 通过 画一个html 实现的、最简单、好理解、的代码、
- 基于Windows CE的SPI驱动程序设计
- abort(),exit()和assert()
- c++和java(c#)之间的pk
- js刷新页面方法大全 详细出处参考:http://www.jb51.net/article/14397.htm
- Windows CE下串行通信的实现
- struts2学习笔记---FilterDispatcher
- javascript frame 刷新
- Windows CE下流驱动的动态加载