repeater数据导出excel
我的repeater有分页,当我导出数据的时候为什么只导出了第一页的数据
导出方法
- C# code
protected void ibtn_Excel_Click(object sender, ImageClickEventArgs e) { Response.Clear(); Response.Buffer = true; Response.Charset = "utf-8"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("名称", System.Text.Encoding.UTF8) + ".xls"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //设置输出流为简体中文 Response.ContentType = "application/ms-excel"; //设置输出文件类型为excel文件。 this.EnableViewState = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true); System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); // repeater控件的ID this.rpt_CEO.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End(); }
后台类中的代码
- C# code
#region CEO报表查询 /// <summary> /// CEO报表查询 /// </summary> /// <param name="timeBegin"></param> /// <param name="timeEnd"></param> /// <param name="page"></param> public void GetCEO(string timeBegin, string timeEnd, Page page) { try { Repeater rpt_CEO = (Repeater)page.FindControl("rpt_CEO"); DataSet ds; //= ViewState["DtDatat"] as DataTable; int ThisPage = PublicClass.thispages(); if (ThisPage < 1) ThisPage = 1; int pages = 0; int recordcount = 0; string WhereStr = "order_CreTime between '" + timeBegin + "'" + " and '" + timeEnd + "' and order_Status = 10";//接收参数语句 string text = Request.GetUrlPamars("text"); string WhereUrl = "Financial_Reports.aspx?text=" + text; List<string[]> OutList = new List<string[]>(); object[] Params = new object[11]; Params[0] = "Hotel_Order";//表名 Params[1] = "order_Id";//按该列来进行分页 Params[2] = "order_Id";//按该列来进行排序 Params[3] = 0;//列的类型 Params[4] = 0;//分页列排序,0-顺序,1-倒序 ; Params[5] = 0;//排序列排序,0-顺序,1-倒序 Params[6] = "*";//要查询出的字段列 Params[7] = 1;//每页记录数 Params[8] = ThisPage;//指定页 Params[9] = WhereStr;//条件 Params[10] = "Pagination";//分页存储过程名 //0:@tb varchar(100), --表名 ; 1:@col varchar(50), --按该列来进行分页; 2:@col1 varchar(50), --按该列来进行排序 ; 3:@coltype int, --@col列的类型,0-数字类型,1-字符类型,2-日期时间类型 ; 4:@orderby bit, --分页列排序,0-顺序,1-倒序 ; 5:@orderby1 bit, --排序列排序,0-顺序,1-倒序 ; 6:@collist varchar(800),--要查询出的字段列表,*表示全部字段 ; 7:@pagesize int, --每页记录数 ; 8:@page int, --指定页 ; 9:@condition varchar(800),--查询条件 ; 10:存储过程名 ds = Provider.DataSetList(true, out OutList, Params); rpt_CEO.DataSource = ds.Tables[0]; rpt_CEO.DataBind(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { Literal l1 = (Literal)rpt_CEO.Items[i].FindControl("l1"); //序号 Literal l2 = (Literal)rpt_CEO.Items[i].FindControl("l2"); //酒店名称 Literal l3 = (Literal)rpt_CEO.Items[i].FindControl("l3"); //房型 Literal l4 = (Literal)rpt_CEO.Items[i].FindControl("l4"); //入住人姓名 Literal l5 = (Literal)rpt_CEO.Items[i].FindControl("l5"); //入住时间 Literal l6 = (Literal)rpt_CEO.Items[i].FindControl("l6"); //离店时间 Literal l7 = (Literal)rpt_CEO.Items[i].FindControl("l7"); //间数 Literal l8 = (Literal)rpt_CEO.Items[i].FindControl("l8"); //几晚 Literal l9 = (Literal)rpt_CEO.Items[i].FindControl("l9"); //应收金额 Literal l10 = (Literal)rpt_CEO.Items[i].FindControl("l10"); //实际入住时间 Literal l11 = (Literal)rpt_CEO.Items[i].FindControl("l11"); //实际离店时间 Literal l12 = (Literal)rpt_CEO.Items[i].FindControl("l12"); //实收金额 Literal l13 = (Literal)rpt_CEO.Items[i].FindControl("l13"); //联系方式 Literal l14 = (Literal)rpt_CEO.Items[i].FindControl("l14"); //联系人姓名 l1.Text = "" + (i + 1); l2.Text = ds.Tables[0].Rows[i]["hotel_Name"].ToString(); l3.Text = ds.Tables[0].Rows[i]["roomType_Name"].ToString(); l4.Text = ds.Tables[0].Rows[i]["checkInName"].ToString(); l5.Text = Convert.ToDateTime(ds.Tables[0].Rows[i]["checkInTime"]).ToString("yyyy-MM-dd"); l6.Text = Convert.ToDateTime(ds.Tables[0].Rows[i]["cheOutTime"]).ToString("yyyy-MM-dd"); l7.Text = ds.Tables[0].Rows[i]["resRoomQuantity"].ToString(); l8.Text = ds.Tables[0].Rows[i]["resFate"].ToString(); l9.Text = Convert.ToInt32(ds.Tables[0].Rows[i]["totalPaid"]).ToString(); l10.Text = Convert.ToDateTime(ds.Tables[0].Rows[i]["OKcheckInTime"]).ToString("yyyy-MM-dd"); l11.Text = Convert.ToDateTime(ds.Tables[0].Rows[i]["OKcheOutTime"]).ToString("yyyy-MM-dd"); l12.Text = Convert.ToInt32(ds.Tables[0].Rows[i]["realityPrice"]).ToString(); l13.Text = ds.Tables[0].Rows[i]["resPhone"].ToString(); l14.Text = ds.Tables[0].Rows[i]["resName"].ToString(); } for (int i = 0; i < OutList.Count; i++) { if (OutList[i][0] == "@pages") { pages = int.Parse(OutList[i][1]); } if (OutList[i][0] == "@recordcounts") { recordcount = int.Parse(OutList[i][1]); } } Literal pageli = (Literal)page.FindControl("pageli"); pageli.Text = PublicClass.PageNumber(pages, recordcount, ThisPage, 5, WhereUrl); } catch (Exception ex) { ExceptionHander.FxtxException(0, ex.Message, "SysBulletinMange", ex); } } #endregion
这个框架是别人写好的7层 分为前台和后台两个ui层 高人指点迷津 高分送上
[解决办法]
通过调用方法 将从数据库获取的ds集合导出到excel
- C# code
//dataset导出excel public static void CreateExcel(DataSet ds, string FileName) { //resp = Page.Response; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls"); HttpContext.Current.Response.ContentType = "application/ms-excel"; string colHeaders = "", ls_item = ""; int i = 0; //定义表对象与行对像,同时用DataSet对其值进行初始化 System.Data.DataTable dt = ds.Tables[0]; DataRow[] myRow = dt.Select(""); //取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符 for (i = 0; i < dt.Columns.Count - 1; i++) colHeaders += dt.Columns[i].Caption.ToString() + "\t"; colHeaders += dt.Columns[i].Caption.ToString() + "\n"; //向HTTP输出流中写入取得的数据信息 HttpContext.Current.Response.Write(colHeaders); //逐行处理数据 foreach (DataRow row in myRow) { //在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n for (i = 0; i < dt.Columns.Count - 1; i++) ls_item += row[i].ToString() + "\t"; ls_item += row[i].ToString() + "\n"; //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据 HttpContext.Current.Response.Write(ls_item); ls_item = ""; } //写缓冲区中的数据到HTTP头文件中 HttpContext.Current.Response.End(); }
[解决办法]
一般遍历repeater或gridview 获取的都是第一页的值
[解决办法]
像LZ这种分页sql查询出来 导出的肯定只有一页呢 那只能写个查询方法查询全部了
[解决办法]
方法我试了 貌似可以直接导出excel 数据也是完整的 导出到excel的是从数据库返回的集合
[解决办法]
你使用 this.rpt_CEO.RenderControl(oHtmlTextWriter);
这个方法当然只会导出一页.
这个方法是把当前的rpt_CEO控件中的HTML元素以文本方式写出(说白点就是把HTML源码输出为文本)
因为Repeater只呈现了一页的资料,只有这一页的资料生成了HTML元素.
至于方法,一楼已经给了你解了.
"所有字段都查了出来连Guid都有 而且标题全是数据库的字段"
这些都是你自己可以在代码中筛选或者设置的.
[解决办法]
一个比较笨拙的方法,点打印的时候弹出另一个新页面,显示的是所有你搜索出来的数据,再打印。
[解决办法]
能判断一下又多少页,然后改变一次pageindex绑定一次数据,然后添加到一个table里,然后依次导出么