1,首先得到一个DataTable

public DataTable GetTable(string sql)

{

SqlConnnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstringname"].ConnectionString);

con.Open();

SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
con.Close();
return ds.Tables[0];

}

2,Excel导出功能实现的实现方法

/// <summary>
/// 将网格数据导出到Excel,
/// </summary>
/// <param name="ctrl">网格名称(如GridView1)</param>
/// <param name="FileType">要导出的文件类型(Excel:application/ms-excel)</param>
/// <param name="FileName">要保存的文件名</param>

//System.Web.UI.Control ctrl相当于Control ctrl 我这么写是由于我的项目中有冲突
private void Export(System.Web.UI.Control ctrl, string FileType, string FileName)
{

HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctrl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}

//这个重写是必须的,可以不让他做事,但是必须存在

public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}

3,在导出按钮的事件中调用Excel的导出方法

protected void btnExport_Click(object sender, EventArgs e)
{
string classID = Request["ClassID"].ToString();
string dt1 = txtStart.Value;
string dt2 = txtEnd.Value;

string sql =
"select ClassCname,NewsTitle,a.CreatTime as createtime,Editor FROM jy_news a,jy_nc b,jy_news_class c where a.NewsID=b.NewsID AND b.ClassID=c.ClassID ";

string where = "";
if (!string.IsNullOrEmpty(classID))
{
where+=" and b.Classid='" +classID + "'";
}

if (!string.IsNullOrEmpty(dt1))
{
where += " and a.CreatTime>='" + dt1 + "'";
}

if (!string.IsNullOrEmpty(dt2))
{
where += " and a.CreatTime<='" + dt2 + "'";
}

sql = sql + where + " order by a.CreatTime desc";

DataTable dt = GetTable(sql);
this.rptdata.DataSource = dt;
this.rptdata.DataBind();

foreach (System.Web.UI.Control c in rptdata.Controls)
{
Label lbl1 = (Label)c.FindControl("Label1");
Label lbl2 = (Label)c.FindControl("Label2");
if (string.IsNullOrEmpty(lbl1.Text) && string.IsNullOrEmpty(lbl2.Text))
{
if (!string.IsNullOrEmpty(dt1) && !string.IsNullOrEmpty(dt2))
{
string dt3 = string.Format("{0:yyyy.MM.dd}", DateTime.Parse(dt1));
string dt4 = string.Format("{0:yyyy.MM.dd}", DateTime.Parse(dt2));

lbl1.Text = dt3;
lbl2.Text = dt4;
}
}
break;
}
string filename = "新闻列表";
if (!string.IsNullOrEmpty(dt1))
{
filename = "_" + dt1;
}

if (!string.IsNullOrEmpty(dt2))
{
filename = filename + "-" + dt2;
}

filename = filename + ".xls";

string filetype = "application/ms-excel";
System.Web.UI.Control ctrl = rptdata;
Export(ctrl, filetype, filename);
}

最后是前台页面,页面上有个日历控件My97DatePicker

<html>

<head runat="server">
<title>新闻列表导出</title>
<script src="/My97DatePicker/WdatePicker.js" type="text/javascript"></script>

</head>
<body>
<form id="form1" runat="server">
<div id="msg">
<p style="color: red">
</p>

</div>
<div>
<label id="lblstart">
开始时间:</label><input type="text" id="txtStart" onclick="WdatePicker()" runat="server"
onchange="txt();" />
<label id="lblend">
结束时间:</label><input type="text" id="txtEnd" onclick="WdatePicker()" runat="server" />
<asp:Button ID="btnExport" runat="server" Text="导出" OnClick="btnExport_Click" />
</div>
<div id="repeaterView">
<asp:Repeater ID="rptdata" runat="server">
<HeaderTemplate>
<table border="1" cellpadding="0" cellspacing="0" style="width: 1006px; border-collapse: collapse;
text-align: center;">
<tr>
<td colspan="5" style="text-align: center; font-size: 150%">
<strong>普陀区科协网站信息发布签发单</strong>
</td>
</tr>
<tr style="height: 30px">
<td colspan="3" style="border: 0px">
发布时间: <asp:Label ID="Label1" runat="server" Text=""></asp:Label>--
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</td>
<td colspan="2" style="border: 0px">
签发人:
</td>
</tr>
<tr style="height: 30px">
<td style="font-weight:bold; text-align: center">
<font size="3">序号</font>
</td>
<td style="font-weight: bold; text-align: center">
<font size="3">标题</font>
</td>
<td style="font-weight: bold; text-align: center">
<font size="3">所在栏目</font>
</td>
<td style=" font-weight: bold; text-align: center">
<font size="3">发布时间</font>
</td>
<td style="font-weight: bold; text-align: center">
<font size="3">发布人</font>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="height: 30px">
<td style="text-align: center">
<%# Container.ItemIndex + 1 %>
</td>
<td style="text-align: center">
<%# DataBinder.Eval(Container.DataItem, "NewsTitle")%>
</td>
<td style="text-align: center">
<%# DataBinder.Eval(Container.DataItem, "ClassCname")%>
</td>
<td style="text-align: center">
<%#string.Format("{0:yyyy年MM月dd日}", Eval("createtime"))%>
</td>
<td style="text-align: center">
<%# DataBinder.Eval(Container.DataItem, "Editor")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>

asp.net 将repeater上数据导出到excel的更多相关文章

  1. Asp.net网页中DataGridView数据导出到Excel

    经过上网找资料,终于找到一种可以直接将GridView中数据导出到Excel文件的方法,归纳方法如下: 1. 注:其中的字符集格式若改为“GB2312”,导出的部分数据可能为乱码: 导出之前需要关闭分 ...

  2. asp.net将数据导出到excel

    本次应用datatable导出,若用gridview(假设gridview设为了分页显示)会出现只导出当前页的情况. protected void btnPrn_Click(object sender ...

  3. JS 在页面上直接将json数据导出到excel,支持chrome,edge,IE10+,IE9,IE8,Safari,Firefox

    JS 在页面上直接将json数据导出到excel,支持chrome,edge,IE10+,IE9,IE8,Safari,Firefox <html> <head> </h ...

  4. 将datagrid中数据导出到excel中 -------<<工作日志2014-6-6>>

    前台datagrid数据绑定 #region 导出到excel中    /// <summary>    /// 2014-6-6    /// </summary>    / ...

  5. Qt中将QTableView中的数据导出为Excel文件

    如果你在做一个报表类的程序,可能将内容导出为Excel文件是一项必须的功能.之前使用MFC的时候我就写过一个类,用于将grid中的数据导出为Excel文件.在使用了QtSql模块后,我很容易的将这个类 ...

  6. C#大量数据导出到Excel(转)

    工作过程中经常会用到将数据导出到Excel中,一般情况下需要导出的数据都是几百几千条或者上万条,这都没有什么问题,但有时候会遇到特殊的需求,客户要求把几十万条甚至上百万条的数据导出到Excel中,这就 ...

  7. 使用PHPExcel将数据导出至Excel

    安装类库 从GitHub上下载PHPExcel类库 地址:https://github.com/PHPOffice/PHPExcel 解压后将Classes文件夹移动到ThinkPHP的extend目 ...

  8. Pl/sql 如何将oracle的表数据导出成excel文件?

    oracle将表数据导出成excel文件的方法 1)在SQL窗体上,查询需要导出的数据 --查询数据条件-- ; 结果视图 2)在查询结果的空白处,右键选择Copy to Excel 3) 查看导出e ...

  9. 大批量数据导出到Excel的实现

    在平时的项目中,将数据导出到Excel的需求是很常见的,在此对一些常见的方法做以总结,并提供一种大数据量导出的实现. OLEDB   使用OLEDB可以很方便导出Excel,思路很简单,处理时将Exc ...

随机推荐

  1. 简单的企业会议管理cms后台模板——后台

    链接:http://pan.baidu.com/s/1eRAVAka 密码:olr1

  2. 离线部署ELK+kafka日志管理系统【转】

    转自 离线部署ELK+kafka日志管理系统 - xiaoxiaozhou - 51CTO技术博客http://xiaoxiaozhou.blog.51cto.com/4681537/1854684 ...

  3. openjudge-NOI 2.6-1768 最大子矩阵

    题目链接:http://noi.openjudge.cn/ch0206/1768/ 题解: 如果用O(n4)的算法肯定会炸,需要压缩掉一维的空间,只需要简单加和就好啦 例如,我们要对样例中第2-4行D ...

  4. python类的继承和多态

    现在属于是老年人的脑子,东西写着写着就忘了,东西记着记着就不知道了.之前学C++的时候就把类.对象这块弄得乱七八糟,现在是因为很想玩python,所以就看看python的类和对象. 就像说的,类有三个 ...

  5. hbase学习(一)hbase简介

    1.hadoop生态系统 2.hbase简介 非关系型数据库知识面扩展 cassandra.hbase.mongodb.redis couchdb,文件存储数据库 Neo4j非关系型图数据库 3.hb ...

  6. OPENSSL问题,使用fsockopen()函数提示错误

    环境配置 系统环境 CentOS7.2WDCP v3.2.2 lanmp PHP 多版本 指定使用5.6 OpenSSL 1.0.2h  3 May 2016 php.ini相关设置allow_url ...

  7. (转载)使用curl 和 libjson 完成联网和数据解析

    转载地址:http://my.oschina.net/cocosgame/blog/71181 libjson 编译和使用 - 3. libjson的C接口 API http://blog.csdn. ...

  8. c++ primer 4 数组和指针

    类比的思想学习数组和指针,c++提供类似于vector和迭代器的低级复合类型——数组和指针.与vector相似,数组可以保存某一种类型的一组对象:而他们的区别在于,数组的长度固定,数组一经创建就不允许 ...

  9. poj2956 Repeatless Numbers(枚举|BFS)

    题目链接 http://poj.org/problem?id=2956 题意 如果一个数中的每一位都是不同的,那么这个数叫做无重复数,如11是有重复数,12是无重复数.输入正整数n(1<=n&l ...

  10. Hibernate (开源对象关系映射框架)

    一.基本介绍1.它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm(对象关系映射)框架,hibernate可以自动生成SQL语句,自动执行: Hibern ...