標籤:style blog http io ar color os sp for
設定一個按鈕:
<input type="submit" name="button" id="exprotExcel" value="匯出Excel" />
給按鈕綁定事件:
$("#exprotExcel").click(function () {
window.open("../../Manage/Student/BeadRollListExportExcel.aspx?studentType=" + $("#selStudentType").val() + "&schoolCode=" + $("#selSchool").val() + "&district=" + $("#selDistrict").val());
});
BeadRollListExportExcel.aspx 頁面就是匯出的頁面了,頁面代碼:
<asp:GridView ID="GridView1" BorderColor="Black" runat="server" AutoGenerateColumns="False" Font-Size="12px" Width="530px" AllowSorting="True" OnRowCreated="GridView1_RowCreated"> <Columns> <asp:TemplateField HeaderText="序號" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <%#(((GridViewRow)Container).DataItemIndex + 1) %> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="PrimarySchoolName" HeaderText="學校" /> <asp:BoundField DataField="TypeName" HeaderText="類別" /> <asp:BoundField DataField="Name" HeaderText="姓名" /> <asp:BoundField DataField="SexCode" HeaderText="性別" /> <asp:BoundField DataField="" HeaderText="備忘" /> </Columns> <HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" /> <RowStyle HorizontalAlign="Center" /> <PagerStyle HorizontalAlign="Center" /> </asp:GridView>
後台代碼:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetList(); Export("application/ms-excel", string.Format("{0}2015屆朝陽區小學畢業年級在校生名冊.xls", DateTime.Now.ToString("yyyy-MM-dd"))); } } public void GetList() { string studentType = Request.QueryString["studentType"].ToString(); string schoolCode = Request.QueryString["schoolCode"].ToString(); string district = Request.QueryString["district"].ToString(); string strWhere = " 1=1"; if (!string.IsNullOrEmpty(studentType)) { strWhere += " and TypeCode=‘" + studentType + "‘"; } if (!string.IsNullOrEmpty(district)) { strWhere += " and DistrictCode=‘" + district + "‘"; } if (!string.IsNullOrEmpty(schoolCode)) { strWhere += " and PrimarySchoolCode =‘" + schoolCode + "‘"; } DataTable dt = new BLL.ObjMethod().GetList("View_ExportExcelStudent", strWhere); GridView1.DataSource = dt; GridView1.DataBind(); } /// <summary> /// 定義匯出Excel的函數 /// </summary> /// <param name="FileType"></param> /// <param name="FileName"></param> private void Export(string FileType, string FileName) { for (int i = 0; i < GridView1.Rows.Count; i++) { GridView1.Rows[i].Cells[4].Attributes.Add("style", "vnd.ms-excel.numberformat:@"); } Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); GridView1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } /// <summary> /// 此方法必重寫,否則會出錯 /// </summary> /// <param name="control"></param> public override void VerifyRenderingInServerForm(Control control) { } protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);//表頭行 TableHeaderCell cell = new TableHeaderCell(); cell.Text = "2015屆朝陽區小學畢業年級在校生名冊"; cell.ColumnSpan = 23; rowHeader.Cells.Add(cell); ((GridView)sender).Controls[0].Controls.AddAt(0, rowHeader); } }
C# 匯出Excel