问题:在一个页面中按“确定”按钮,如何只打印GridView中的内容?

解决方案 »

  1.   

    要打印的内容放在div中,然后用下面的代码进行打印。<html>
    <head>
    <script language="javascript">
    function printdiv(printpage)
    {
    var headstr = "<html><head><title></title></head><body>";
    var footstr = "</body>";
    var newstr = document.all.item(printpage).innerHTML;
    var oldstr = document.body.innerHTML;
    document.body.innerHTML = headstr+newstr+footstr;
    window.print(); 
    document.body.innerHTML = oldstr;
    return false;
    }
    </script>
    <title>div print</title>
    </head><body>
    //HTML Page
    //Other content you wouldn't like to print
    <input name="b_print" type="button" class="ipt"   onClick="printdiv('div_print');" value=" Print "><div id="div_print"><h1 style="Color:Red">The Div content which you want to print</h1></div>
    //Other content you wouldn't like to print
    //Other content you wouldn't like to print
    </body></html>
      

  2.   

    gvShowInfo.AllowPaging = false; //清除分页        
            gvShowInfo.AllowSorting = false; //清除排序              
            this.gvShowInfo.DataBind();  //你绑定gridview1数据源的那个函数         Response.Clear(); 
            Response.Buffer = true; 
            Response.Charset = "GB2312";         Response.AppendHeader("Content-Disposition", "attachment;filename=MonitorResult.xls"); 
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");         //设置输出文件类型为excel文件。 
            Response.ContentType = "application/ms-excel"; 
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); 
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);         this.gvShowInfo.RenderControl(oHtmlTextWriter); 
            Response.Output.Write(oStringWriter.ToString());         Response.Flush(); 
            Response.End(); 
            //this.ApplicationInstance.CompleteRequest(); //停止页的执行         gvShowInfo.AllowSorting = true; //恢复分页          
            gvShowInfo.AllowPaging = true;  //恢复排序        
            this.gvShowInfo.DataBind(); //再次绑定