在gridview第一列中放一个模板列 
在模板列中放一个checkbox,表示一个字段的状态 
该字段表示是否导出该行数据 
如果选中某些行的checkbox,单击button按钮就要把选中的数据导出到excel(要弹出下载页面)
另:gridview中并没有完全显示数据库表中各字段的数据,但导出的时候就要按数据库表中所有的字段导出。请详解,谢谢!!!

解决方案 »

  1.   

    建议新建一个dataset 存放数据库表中各字段的数据,导出该dataset 即可。
      

  2.   

    思路:
    放置checkbox,用js来处理勾选问题。
    你的button应该是专门来处理是否导出的button吧?那就将选中列的key全部取出来,然后到数据库中重新get数据,然后就可以处理导出了。
      

  3.   

             function CheckSelected()
               {
        var Tab = document.getElementById("MainTable");
        var chks = Tab .getElementsByTagName("input");
    var a =new Array();
    var j;
        for(i=0;i<chks.length;i++)
        {
            if(chks[i].checked&&(chks[i].type=="checkbox"||chks[i].type=="radio"))
            {
              for(j=i;j<i+1;j++)
               {
                a[j]=i;
                }
            }
        }
        alert(a);
        if(a!="")
        {
           location.href='Research.aspx?Edit='+a;
         }
         else
         {
           alert('请先选择选项后再提交!');
           return false;
         }
        }
    a就是选中的checkbox集合
      

  4.   

    有了a 
    你可以在你的页面循环读出来
    再循环导出到excel
      

  5.   

    http://topic.csdn.net/t/20050923/21/4290545.htmlhttp://wangweixznu.cnblogs.com/archive/2006/04/25/384715.html
      

  6.   

    下面方法导出,为什么说str转换成Int时出错?还有下面的导出方法是否恰当?
        protected void btnexcel_Click(object sender, EventArgs e)
        {
            SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
            strcon.Open();
            string str = "";
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("chk");
                if (cbox.Checked == true)
                {
                    str += Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";
                }
            }
            string sql = "select Code_ID as 编码,Sort1 as 分类,Content as 内容 from Fault where ID in ('" + str + "')";
            SqlDataAdapter sda = new SqlDataAdapter(sql, strcon);
            DataSet ds = new DataSet();        //填充数据集
            sda.Fill(ds);
            DataTable dt = ds.Tables[0];
            //清除客户端当前显示
            Response.Clear();
            Response.Buffer = true;
            //输出类型为Word
            Response.ContentType = "application/vnd.ms-word";
            //输出类型为Excel
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            //设置显示的字和内容要存的形式
            Response.Charset = "Word文档";
            this.EnableViewState = false;
            StringWriter oStringWriter = new StringWriter();
            HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
            GridView gd = new GridView();
            //返回DataTable
            gd.DataSource = dt;
            gd.DataBind();
            gd.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.End();    }
      

  7.   

    SqlConnection strcon = new SqlConnection("Data Source=.;Database=www;User ID=sa;Password=supcon");
                strcon.Open();
                string str = string.Empty;
                for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
                {
                    CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                    if (cbox.Checked == true)
                    {
                        str += Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";
                    }
                }
                string check = str.TrimEnd(new char[] { ',' });
                string sql = string.Empty;
                if (check.Equals(string.Empty))
                {
                    sql = "select * from TBLTest";
                }
                else
                {
                    sql = "select * from TBLTest where sid in (" + check + ")";
                }
                SqlDataAdapter sda = new SqlDataAdapter(sql, strcon);
                DataSet ds = new DataSet();
                //填充数据集
                sda.Fill(ds);
                DataTable dt = ds.Tables[0];
                //清除客户端当前显示
                Response.Clear();
                Response.Buffer = true;
                //输出类型为Word
                // Response.ContentType = "application/vnd.ms-word";
                //输出类型为Excel
                Response.ContentType = "application/vnd.ms-excel";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                //设置显示的字和内容要存的形式
                Response.Charset = "Word文档";
                this.EnableViewState = false;
                //foreach (GridViewRow dg in this.GridView1.Rows)
                //{
                //    dg.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
                //    dg.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
                //    dg.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
                //}
                StringWriter oStringWriter = new StringWriter();
                HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
                GridView gd = new GridView();
                //返回DataTable
                gd.DataSource = dt;
                gd.DataBind();
                gd.RenderControl(oHtmlTextWriter);
                Response.Write(oStringWriter.ToString());
                Response.End();