我有个页面,数据据是从数据库里取出来的,前些天做了个数据的下载,现在又要加个找印。参考了网上的代码,可是打印的是整个网页,包括,页面标题,页码,下载按钮什么,怎么显示只打印我要的表啊,这是我的页面部份代码,我参考了网上的意见,只打印部份可是点打印后就只弹一个空页面:错误提示缺少对像这行:PrintWin.document.write('<OBJECT   classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2   height=0   id=WebBrowser   width=0> </OBJECT>' +   document.all( "PrintContent").innerHTML); 是什么原因啊??
以下是部份代码:
<script   language= "javascript"> 
function   PrintNote() 

  var   PrintWin=window.open('about:blank', 'Print'); 
  PrintWin.document.write('<OBJECT   classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2   height=0   id=WebBrowser   width=0> </OBJECT>' +   document.all( "PrintContent").innerHTML); 
  PrintWin.document.all.WebBrowser.ExecWB(6,1); 
  PrintWin.close(); 

</script> 
<div   id= "PrintContent "   align= "center ">
中间是打印内容,一张表
<table class="tablelistcontent" width="100%" border="1"
cellspacing="1">
<tr>
<th width="20" align="center">序号</th>
.......
<th width="100" align="center">运费</th>

</tr>
<tr bordercolor="#CCCCCC" bgcolor="#EEEEEE">
           <logic:present name="billList">
   <logic:notEmpty name="billList">
   <logic:iterate name="billList" id="awb" indexId="index">
      <tr>
</table>
</div>
<input   onclick= "return PrintNote()"   type= "button"  value= "打印 "> 

解决方案 »

  1.   

    jsp对打印的处理很不好。我的方法是用iText,把要打印的东西转化成pdf,这样打印就不成问题了。
      

  2.   

    你的WebBrowser是在打开页面中创建的PrintWin.document.write(' <OBJECT  classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2  height=0  id=WebBrowser  width=0> </OBJECT>',而你确在本页面使用PrintWin.document.all.WebBrowser.ExecWB(6,1);
      

  3.   

    iText是一个开放源码的Java类库,可以用来方便地生成PDF文件。大家通过访问http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948下载最新版本的类库,下载完成之后会得到一个.jar包,把这个包加入JDK的classpath即可使用。
      如果生成的PDF文件中需要出现中文、日文、韩文字符,则还需要通过访问http://itext.sourceforge.net/downloads/iTextAsian.jar下载iTextAsian.jar包。
      关于iText类库的使用,http://www.lowagie.com/iText/tutorial/index.html有比较详细的教程。该教程从入门开始,比较系统地介绍了在PDF文件中放入文字、图片、表格等的方法和技巧。<%@ page import="java.io.*,java.awt.Color,com.lowagie.text.*,com.lowagie.text.pdf.*"%><%
    response.setContentType
    ( "application/pdf" );
    Document document = new Document();
    ByteArrayOutputStream buffer
    = new ByteArrayOutputStream();
    PdfWriter writer=
    PdfWriter.getInstance( document, buffer );
    document.open();
    document.add(new Paragraph("Hello World"));
    document.close();
    DataOutput output =
    new DataOutputStream
    ( response.getOutputStream() );
    byte[] bytes = buffer.toByteArray();
    response.setContentLength(bytes.length);
    for( int i = 0;
    i < bytes.length;
    i++ )
    {
    output.writeByte( bytes[i] );
    }
    %>只需要在document.open();和document.close();两条语句中间加入自己希望放在PDF文件中的内容即可。