快速简洁将Gridview导出到EXCEL中

来源:互联网 发布:nsurlsessiond mac 编辑:程序博客网 时间:2024/06/06 12:54

将GridView中的数据导出到Excel中 

 1.需要重写VerifyRenderingInServerForm(Control control)方法

/// <summary>    /// 导出Excel采用此方法 需要重写    /// </summary>    /// <param name="control"></param>    public override void VerifyRenderingInServerForm(Control control)    {    }


2.导出到Excel中的方法

 public  void ExcelAndManualSign(System.Web.UI.Control ctl, string strFileName)    {        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");        HttpContext.Current.Response.Charset = "gb2312";        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");        //System.Text.Encoding.Default;              HttpContext.Current.Response.ContentType = "application/ms-excel";     //设置输出流为简体中文        ctl.Page.EnableViewState = false;        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);        System.IO.StringWriter tw = new System.IO.StringWriter();        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);        ctl.RenderControl(hw);        HttpContext.Current.Response.Write(tw.ToString());        HttpContext.Current.Response.End();        hw.Flush();        hw.Close();        tw.Flush();        tw.Close();    }

3.调用导入Excel的方法

 protected void lbBtnToExcel_Click(object sender, EventArgs e)    {        if (GVContentList.Rows.Count == 0)        {            ScriptManager.RegisterStartupScript(GVContentList, this.GetType(), "", "alert('没有需要导出的数据');", true);            return;        }        GVContentList.ShowHeader = true;        GVContentList.Columns[5].Visible = false;        GVContentList.GridLines = GridLines.Both;        GVContentList.AllowPaging = false;   //取消分页 导出所有数据 需要重新加载        if (cbISAll.Checked == true)        {            LoadGvlistbyMins();        }        else        {            loadGvList();        }        string fileName = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();        ExcelAndManualSign(GVContentList, fileName);    }     public void LoadGvlistbyMins()     {        int count = 0;        if(CheckForms.IsUnsignedInteger(this.txtNoTimeMin.Value)==false)        {            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('输入的迟到分钟不符合!')", true);            return;        }        string laterMin = this.txtNoTimeMin.Value;        if (laterMin == "" || !cbISAll.Checked)            laterMin = "0";        System.IFormatProvider format = new System.Globalization.CultureInfo("zh-CN", true);        ScheduleInfoManage siMan = new ScheduleInfoManage();        DataTable dt = siMan.NoTimeExamCount(txtSTime.Value.Replace("-", ""), txtEtime.Value.Replace("-", ""), Convert.ToInt32(ddlStyle.SelectedValue), Convert.ToInt32(ddlJF.SelectedValue), ddlKSBQ.SelectedValue, laterMin);        DataTable newdt = dt.Copy();        newdt.Clear();        this.GVContentList.DataSource = newdt;        this.GVContentList.DataBind();     }



 

 

0 0
原创粉丝点击