码迷,mamicode.com
首页 > Windows程序 > 详细

C# 导出Excel

时间:2014-12-02 13:17:49      阅读:330      评论:0      收藏:0      [点我收藏+]

标签: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

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/xiaoqi742709106/p/4137058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!