C# 匯出Excel

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.