1、(转)ireport 直接打印报表,不预览2011 年 02 月 23 日 星期三 下午 02:06打印类代码如下:package com.sofn.struts.report.actions;import java.io.File;import java.io.ObjectOutputStream;import java.util.ArrayList;import java.util.List;import java.util.Map;import javax.naming.Context;import javax.naming.InitialContext;import javax.nam
2、ing.NameNotFoundException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.sql.DataSource;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JasperFillManager;import net.sf.
3、jasperreports.engine.JasperPrint;import net.sf.jasperreports.engine.JasperPrintManager;import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.
4、struts.action.ActionMapping;import com.lowagie.text.ExceptionConverter;import com.sofn.struts.dto.UserInfoDTO;import com.sofn.struts.op.UserInfo;import com.sofn.util.UserSessionOperate;public class ReportViewAction extends Action (org.apache.struts.action.ActionMapping, org.apache.struts.action.Acti
5、onForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception /已编译文件路径和格式String fp =this.getServlet().getServletContext().getRealPath(“
6、/“)+ “reportjasper“+ request.getAttribute(“rdoc“)+ “.jasper“;File reportFile = new File(fp);/ System.out.println(“报表路径!“+reportFile);Map parameters = (Map) request.getAttribute(“paras“);byte bytes = null;List list = new ArrayList();DataSource ds = null; JasperPrint jasperPrint=null;/参数传递UserSessionO
7、perate uso = new UserSessionOperate();String user = uso.getUser(request);UserInfo eidr = new UserInfo();UserInfoDTO eidrdto = null;String deptname = “物流部门“;String username = “陈云刚“;parameters.put(“marker“, username);parameters.put(“bumen“, deptname); /数据库连接try String source = “java:comp/env/jdbc/posu
8、se“;Context ic = null;if (ic = null)ic = new InitialContext();ds = (DataSource) ic.lookup(source);if (ds = null) System.out.println(“Reportview-22222!“); catch (NameNotFoundException nfe) nfe.toString(); catch (Exception ne) ne.printStackTrace();/使用数据源if (request.getAttribute(“datasource“) != null)
9、list = (List) request.getAttribute(“reportlist“);JRBeanCollectionDataSource jds =new JRBeanCollectionDataSource(list); /使用数据源/使用 SQL 查询try jasperPrint =JasperFillManager.fillReport(reportFile.getPath(),parameters,ds.getConnection();/填充报表数据生成JasperPrint 对象 JasperPrintManager.printReport(jasperPrint,
10、false);/2008-08-25 直接打印,不用预览 PDF 直接打印 true 为弹出打印机选择.false 为直接打印. catch (JRException jre) /System.out.println(“Reportview-JRException88888“);jre.printStackTrace(); catch (ExceptionConverter ec) ec.getLocalizedMessage();ec.printStackTrace(); catch (NullPointerException npe) npe.getLocalizedMessage();n
11、pe.getMessage();npe.toString();System.err); catch (Exception ee) ee.printStackTrace();/输出response.setContentType(“application/octet-stream“);ServletOutputStream ouputStream = response.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(ouputStream);oos.writeObject(jasperPrint);/将 JasperPrint 对象写入对象输出流中 oos.flush();oos.close(); ouputStream.flush();ouputStream.close();return mapping.findForward(“view“); /转向使用技术:Jsp+Struts+Jdk1.6.7+Tomcat5.5+SQLServer2000+iReport2.0.5 从中遇到过两个问题:一是弹出打印机选择;二是弹出自动下载框. 代码中已解决