代码如下:不知哪有问题,请多多指教!! protected void Button2_Click(object sender, EventArgs e)
    {        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
            if (ck.Checked)
            {
               
                string id = GridView1.Rows[i].Cells[1].Text.Trim();
              
                string title = GridView1.Rows[i].Cells[2].Text.Trim(); Export("application/ms-excel", "excel.xls");}
}
}
    private void Export(string FileType, string FileName)
    {                Response.Charset = "UTF-8";
        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();
 
    }

解决方案 »

  1.   


     protected void Button2_Click(object sender, EventArgs e)
        {        for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
                if (ck.Checked)
                {
                   
                    string id = GridView1.Rows[i].Cells[1].Text.Trim();
                  
                    string title = GridView1.Rows[i].Cells[2].Text.Trim(); Export("application/ms-excel", "excel.xls",title);}
    }
    }
        private void Export(string FileType, string FileName,string title)
        {                Response.Charset = "UTF-8";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            Response.Write(title);
            Response.End();
     
        }
      没试! 你自己试试! 还有把你的情况说清楚点..........
        是只导出 选种的行吗?  如果是的话!那你直接把选种的行的数据传递到Export 以流的方式保存就好了.
      

  2.   

    对啊,只导出 选种的行成excel!
      

  3.   

    protected void Button2_Click(object sender, EventArgs e)
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine("<table style='width:800px' border=1>");
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
                if (ck.Checked)
                {
                    //GridView1.Columns
                    s.AppendLine("<tr>");
                    for (int j = 0; j < GridView1.Columns.Count; j++)
                    {
                        s.AppendFormat("<td>{0}</td>", GridView1.Rows[i].Cells[j].Text);
                    }
                    s.AppendLine("</tr>");            }
            }
            s.Append("</table>");
            Export("application/ms-excel", "excel.xls",s);    }    private void Export(string FileType, string FileName,string s)
        {        Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            Response.Write(s);
            Response.End();    }
      

  4.   

    Export”方法没有采用“3”个参数的重载
      

  5.   


    5楼的方法可行,还有一种可以通过js来实现导出:
    <SCRIPT LANGUAGE="JavaScript">
    function AutomateExcel() 
    {
    try { 
        var oXL = new ActiveXObject("Excel.Application"); 
        }catch(e){ 
    alert('您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”!');
    return '';
    }       
    var oWB = oXL.Workbooks.Add();
    var oSheet = oWB.ActiveSheet;
    var table = document.getElementById("Excel"); 
    var hang = table.rows.length;
    if(hang>0)
    {
        var lie = table.rows(0).cells.length; 
        for (i=0;i<hang;i++)
        {
            //这里判断checkbox是否选中,假如checkbox在第1列
            var cb = table.rows[i].cells[0].getElementsTagName("checkbox");
            if(cb!=null && cb.checked)
            {
               for (j=0;j<lie;j++)
               {
                   oSheet.Cells(i+1,j+1).NumberFormatLocal="@";
                   oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
               }
            }
        }
    }
    oXL.Visible = true;
    oXL.UserControl = true;
    }</SCRIPT>//页面调用js
    <input type="button" onclick="AutomateExcel()"  value="导出" style="width: 41px" class="button">   
      

  6.   

    <input type="button" onclick="AutomateExcel()"  value="导出" style="width: 41px" class="button">   
    <SCRIPT LANGUAGE="JavaScript">
    function AutomateExcel() 
    {
    try { 
        var oXL = new ActiveXObject("Excel.Application"); 
        }catch(e){ 
    alert('您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”!');
    return '';
    }       
    var oWB = oXL.Workbooks.Add();
    var oSheet = oWB.ActiveSheet;
    var table = document.getElementById("表格名称"); 
    var hang = table.rows.length;
    if(hang>0)
    {
        var lie = table.rows(0).cells.length; 
        for (i=0;i<hang;i++)
        {
            //假如你的checkbox在第1列
            var cb = table.rows[i].cells[0].getElementsTagName("checkbox");
            if(cb!=null && cb.checked)
            {
              for (j=0;j<lie;j++)
              {
                oSheet.Cells(i+1,j+1).NumberFormatLocal="@";
                oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
              }
            }
        }
    }
    oXL.Visible = true;
    oXL.UserControl = true;
    }</SCRIPT>
      

  7.   


    //修改下:<input type="button" onclick="AutomateExcel()"  value="导出" style="width: 41px" class="button">   
    <SCRIPT LANGUAGE="JavaScript">
    function AutomateExcel() 
    {
    try { 
        var oXL = new ActiveXObject("Excel.Application"); 
        }catch(e){ 
    alert('您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”!');
    return '';
    }       
    var oWB = oXL.Workbooks.Add();
    var oSheet = oWB.ActiveSheet;
    var table = document.getElementById("表格名称"); 
    var hang = table.rows.length;
    if(hang>0)
    {
        var lie = table.rows(0).cells.length; 
        for (i=0;i<hang;i++)
        {
            //假如你的checkbox在第1列
            var cb = table.rows[i].cells[0].getElementsTagName("input");
            if(cb!=null && cb.type="checkbox" && cb.checked)
            {
              for (j=0;j<lie;j++)
              {
                oSheet.Cells(i+1,j+1).NumberFormatLocal="@";
                oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
              }
            }
        }
    }
    oXL.Visible = true;
    oXL.UserControl = true;
    }
      

  8.   

    建立一个datatable,将选中行复制到datatable,然后导出excel的时候,数据源就是新建的datatable
      

  9.   

    五楼的方法提示:Export”方法没有采用“3”个参数的重载
      

  10.   


    Export("application/ms-excel", "excel.xls",s.ToString());
      

  11.   

    编译错误 
    说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误消息: CS1501: “Export”方法没有采用“3”个参数的重载源错误: 行 128:        }
    行 129:        s.Append("</table>");
    行 130:        Export("application/ms-excel", "excel.xls", s.ToString());
    行 131:
    行 132:    }