要导出页的前台: <asp:Button runat="server" ID="btnExport" Text="导出" CssClass="button_sm7" onclick="btnExport_Click" /> 后台: protected void btnSearch_Click(object sender, EventArgs e) { Response.Redirect("Export.aspx?uname=" + txtUNameS.Text + "&dep=" + this.ddlDepS.SelectedValue + "&class=" + this.ddlClassS.SelectedValue + "&iyear=" + this.ddlYearS.SelectedValue); }
Export.aspx (显示数据): <title>哎哎哎导出</title> <meta http-equiv="content-type" content="application/ms-excel; charset=UTF-8" /> </head> <body> <form id="form1"> <% string fileName = "哎哎哎.xls"; //导出后的文件名 Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName)); %> <table border="1" cellpadding="0" cellspacing="0"> <tr align="center" style="font-weight: bold;"> <td style="width: 60px" rowspan="2"> 序号 </td> <td rowspan="2"> 。。 </td> <td rowspan="2"> 。。 </td> <td rowspan="2"> 。。 </td> <td rowspan="2"> 。。 </td> <td rowspan="2"> 。。 </td> <td rowspan="2"> 。。 </td> <td rowspan="2"> 。。 </td> <td colspan="4" style="text-align: center;"> 。。 </td> </tr> <tr> <td> 。。 </td> <td> 。。 </td> <td> 。。 </td> <td> 。。 </td> </tr> <asp:Repeater ID="rptStudentFee" runat="server"> <ItemTemplate> <tr> <td> <%#Eval("r_n") %> </td> <td> <%#Eval("Department") %> </td> <td> <%#Eval("CName") %> </td> <td> '<%#Eval("TTCard") %> </td> <td> <%#Eval("UName") %> </td> <td> <%#DigitalCampus.Web.Common.pfunction.getPBCName(DigitalCampus.Web.Common.clsDictionaryData.Gender.ToString(),Eval("Gender").ToString()) %> </td> <td> <%#DigitalCampus.Web.Common.pfunction.GetStrToShortDate(Eval("AdmissionDate").ToString())%> </td> <td> <%#Eval("UName") %> </td> <td> <%#Eval("SubYear")%> </td> <td> <%#Eval("ShouldPayment")%> </td> <td> <%#Eval("ActualPayment")%> </td> <td> <%#Decimal.Parse(Eval("ShouldPayment").ToString()) - Decimal.Parse(Eval("ActualPayment").ToString())%> </td> </tr> </ItemTemplate> </asp:Repeater> <asp:Repeater runat="server" ID="rptCount"> <ItemTemplate> <tr style="font-weight: bold;"> <td> 合计 </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> ¥ <%#Eval("ShouldPayment") %> </td> <td> ¥ <%#Eval("ActualPayment")%> </td> <td> ¥ <%#Decimal.Parse(Eval("ShouldPayment").ToString()) - Decimal.Parse(Eval("ActualPayment").ToString())%> </td> </tr> </ItemTemplate> </asp:Repeater> </table> </form> </body> </html> Export.aspx.cs(查数据): protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetFilesList(); } }
private void GetFilesList() { string sort = ""; string strWhere = " where 1=1 "; if (!string.IsNullOrEmpty(Request["uname"]) && Request["uname"] != "TT卡号/身份证/姓名") { strWhere += " and (vw.UName like '%" + Request["uname"] + "%' or vw.IdentityCard like '%" + Request["uname"] + "%' or vw.TTCard like '%" + Request["uname"] + "%')"; } if (Request["dep"] != "-1") { strWhere += " and vw.DepName='" + Request["dep"] + "'"; } if (Request["class"] != "-1") { strWhere += " and vw.ClassName='" + Request["class"] + "'"; } if (Request["iyear"] != "-1") { strWhere += " and sp.SubYear='" + Request["iyear"] + "'"; }
string strSql = @"select row_number() over(order by sp.SubYear,vw.DepName,vw.ClassName,vw.UName) as r_n,sp.*,vw.* from (select StuID,SubYear,isnull(sum(ShouldPayment),0) ShouldPayment,isnull(sum(ActualPayment),0) ActualPayment from StudentShouldpay group by StuID,SubYear) sp inner join dbo.vw_StudentInfo vw on sp.StuID=vw.ID " + strWhere; DataTable dt = DbHelperSQL.Query(strSql).Tables[0]; if (dt.Rows.Count == 0) { Response.Write("<script>alert('数据不存在,导出失败!');history.back();</script>"); Response.End(); } rptStudentFee.DataSource = dt; rptStudentFee.DataBind(); if (dt.Rows.Count > 0) { DataTable dt2 = new DataTable(); dt2 = DbHelperSQL.Query(@"select isnull(sum(ShouldPayment),0) ShouldPayment,isnull(sum(ActualPayment),0) ActualPayment from (select StuID,SubYear,isnull(sum(ShouldPayment),0) ShouldPayment,isnull(sum(ActualPayment),0) ActualPayment from StudentShouldpay group by StuID,SubYear) sp inner join dbo.vw_StudentInfo vw on sp.StuID=vw.ID " + strWhere).Tables[0]; rptCount.DataSource = dt2; rptCount.DataBind(); }
}