asp.net 怎么把repeater查询出来的数据导入到excel中,具体代码怎么写
2个回答
展开全部
DataTable dt = (new Admin()).GetAdmin_print();
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();//这里属于添加datatable ,每个人不同
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
Repeater1.RenderControl(hw);
Response.Clear();
Response.ContentType = "application/vnd.ms-word";//这里设置是选择导出excel,还是word
Response.Charset = "";
Repeater1.Page.EnableViewState = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=Advertising.xls");//这里是文件名称设置
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\"><table><tr><td>");
Response.Write(sw.ToString());
Response.Write("</td></tr></table>");
Response.End();
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();//这里属于添加datatable ,每个人不同
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
Repeater1.RenderControl(hw);
Response.Clear();
Response.ContentType = "application/vnd.ms-word";//这里设置是选择导出excel,还是word
Response.Charset = "";
Repeater1.Page.EnableViewState = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=Advertising.xls");//这里是文件名称设置
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\"><table><tr><td>");
Response.Write(sw.ToString());
Response.Write("</td></tr></table>");
Response.End();
追问
我是新手,data table 怎么获得repeater里面的数据啊
(new Admin()).GetAdmin_print(); 这个是什么方法?
追答
我这里是 调用我自己的类里面的方法去读取数据库 返回一个表单,也就是datatable,
datatable 就是数据源,repeater需要绑定datatable才能显示数据,你直接打印datatable里面的数据就可以了,不需要别的操作
2012-08-08
展开全部
//重写此事件,防止从gridview导出到excel时报错。
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
public void Export(string FileType, string FileName)
{
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Repeater1.RenderControl(hw);
string str = hw.InnerWriter.ToString();
Response.Write(sw.ToString());
Response.End();
}
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "repeater.xls");
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
public void Export(string FileType, string FileName)
{
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Repeater1.RenderControl(hw);
string str = hw.InnerWriter.ToString();
Response.Write(sw.ToString());
Response.End();
}
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "repeater.xls");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询