zoukankan      html  css  js  c++  java
  • gridview 导出Excel

     protected void btnImportData_Click(object sender, EventArgs e)
        {
            BTN_Query_Click(sender, e);
            BindStoreUserListImport(ViewState["sWhere"].ToString());
            setHiddenFiled(GridView1, false);
            setFiledFormat(GridView1);
            Export("application/ms-excel", "我需要的数据.xls");
            setHiddenFiled(GridView1, true);
        }

        private void Export(string FileType, string FileName)
        {
            Response.Clear();
            Response.Buffer = true;

            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            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);
            this.GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
            this.BindStoreUserList(Convert.ToInt32(ViewState["PageNo"]), ViewState["sWhere"].ToString());
        }

       

      //要导出的数据

        protected void BindStoreUserListImport(string swhere)
        {
            int index=0;
            DataSet ds = new DataSet();

            string strSQL = "select * from   table1 where " + swhere + " order by  SignDate desc";

            string strCount = "select count(Id) as total from table1 where " + swhere + "";
            SqlDataReader sdr = help.ExecuteReader(strSQL);
            object countObj = help.GetSingle(strCount);
            if (countObj != null)
            {
                index = Convert.ToInt32(countObj);
            }
            GridView1.DataSource = sdr;

            GridView1.DataBind();

        

            this.WebPager1.RecordCount = index; //分页控件
            this.WebPager1.PageSize = 20;
            this.WebPager1.CurrentPageIndex = 1;

        }

        必要的方法 不然会出错

        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        隐藏不需要导出的字段
        protected  void setHiddenFiled(GridView gv,bool flag)
        {
            if (gv.Rows.Count > 0)
            {
                int cellLength = gv.Rows[0].Cells.Count;
                for (var i = 0; i < gv.Rows.Count; i++)
                {
                    gv.Rows[i].Cells[cellLength - 2].Visible = flag;
                }
                gv.HeaderRow.Cells[cellLength - 2].Visible = flag;
            }

        }

        导出 格式化身份证号码或银行卡号
        protected void setFiledFormat(GridView gv)
        {
            for (int i =0; i <gv.Rows.Count; i++)
            {
                gv.Rows[i].Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                gv.Rows[i].Cells[4].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
            }
        }
  • 相关阅读:
    二叉排序树的查找和插入操作
    二叉排序树(二叉查找树)- 数据结构和算法73
    线性索引查找
    斐波那契查找(黄金分割法查找)- 数据结构和算法71
    插值查找(按比例查找)- 数据结构和算法70
    序列!序列!- 零基础入门学习Python016
    字符串:格式化
    字符串:各种奇葩的内置方法
    为duilib的MenuDemo增加消息响应,优化代码和显示效果
    为duilib的MenuDemo增加消息响应,优化代码和显示效果
  • 原文地址:https://www.cnblogs.com/smallfa/p/1588663.html
Copyright © 2011-2022 走看看