灵魂-放水

为学日益,为道日损。

BlogJava 首页 新随笔 联系 聚合 管理
  296 Posts :: 10 Stories :: 274 Comments :: 0 Trackbacks

源码如下:

<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@taglib uri="/WEB-INF/struts-nested.tld" prefix="nested"%>
<%@taglib uri="/WEB-INF/struts-template.tld" prefix="template"%>
<%@taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<%@page import="java.sql.*"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="net.sf.jasperreports.engine.JasperRunManager"%>
<%@page import="net.sf.jasperreports.engine.*"%>
<%@page import="net.sf.jasperreports.engine.util.*"%>
<%@page import="net.sf.jasperreports.engine.export.*"%>
<%@page import="net.sf.jasperreports.j2ee.servlets.*"%>
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page import="net.sf.jasperreports.engine.JasperPrintManager"%>
<%@page import="net.sf.jasperreports.view.*"%>
<%@page import="java.sql.*, javax.sql.DataSource, javax.naming.InitialContext"%>
<%@page import="java.lang.*"%>
<%
  InitialContext ctx = new InitialContext();
  DataSource ds = (DataSource) ctx.lookup("java:/MSSQLDS");
  Connection conn = ds.getConnection();
  //String accNo ="200672395001";
  //String projID ="200672395121";
  String accNo = request.getParameter("accNo");
  String projID = request.getParameter("proId");
  File reportFile1 = new File(application.getRealPath("/report/kuaibao1.jasper"));
  File reportFile2 = new File(application.getRealPath("/report/kuaibao2.jasper"));
  File reportFile3 = new File(application.getRealPath("/report/kuaibao3.jasper"));
  File reportFile4 = new File(application.getRealPath("/report/kuaibao4.jasper"));
  if (!reportFile1.exists() || !reportFile2.exists() || !reportFile3.exists() || !reportFile3.exists() || !reportFile4.exists())
    throw new JRRuntimeException("Fidsfsfd. The report design must be compiled first.");
  JasperReport jasperReport1 = (JasperReport) JRLoader.loadObject(reportFile1.getPath());
  JasperReport jasperReport2 = (JasperReport) JRLoader.loadObject(reportFile2.getPath());
  JasperReport jasperReport3 = (JasperReport) JRLoader.loadObject(reportFile3.getPath());
  JasperReport jasperReport4 = (JasperReport) JRLoader.loadObject(reportFile4.getPath());
  Map parameters = new HashMap();
  parameters.put("accNo", accNo);
  parameters.put("projID", projID);
  JasperPrint jasperPrint1 =
      JasperFillManager.fillReport(
          jasperReport1,
          parameters,
          conn
      );
  JasperPrint jasperPrint2 =
      JasperFillManager.fillReport(
          jasperReport2,
          parameters,
          conn
      );
  JasperPrint jasperPrint3 =
      JasperFillManager.fillReport(
          jasperReport3,
          parameters,
          conn
      );
  JasperPrint jasperPrint4 =
      JasperFillManager.fillReport(
          jasperReport4,
          parameters,
          conn
      );
  response.setContentType("application/ms_word");
  JRExporter exporter = null;
  exporter = new JRRtfExporter();
  List reportlist = (List)new java.util.ArrayList();
  reportlist.add(jasperPrint1);
  reportlist.add(jasperPrint2);
  reportlist.add(jasperPrint3);
  reportlist.add(jasperPrint4);
  // System.out.println(reportlist.size() + "list");
  byte[] bytes;
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, reportlist);
  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
  exporter.exportReport();
  // exporter.exportReportToStream();
  bytes = baos.toByteArray();
  // System.out.println(bytes+"bytes");
  if (bytes != null && bytes.length > 0) {
    try {
     // response.reset();
      response.setContentType("application/ms_word");
      response.setHeader("Content-disposition",
                         "attachment; filename=yourReport.rtf");
      response.setContentLength(bytes.length);
      ServletOutputStream ouputStream = response.getOutputStream();
      ouputStream.write(bytes, 0, bytes.length);
      //ouputStream.flush();
      ouputStream.close();
      response.reset();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    finally {

      conn.close();
    }
  }
%>

posted on 2006-12-17 10:44 放水老倌 阅读(2621) 评论(7)  编辑  收藏 所属分类: 打印报表

Feedback

# re: 一个Jasperreport多表打印代码实战 2008-04-16 15:43 衣顺利
哥们,运行这个的时候怎么出现这个异常,是怎么回事啊?
严重: Servlet.service() for servlet jsp threw exception
net.sf.jasperreports.engine.JRException: Invalid text height
at net.sf.jasperreports.engine.export.JRRtfExporter.exportText(JRRtfExporter.java:732)
at net.sf.jasperreports.engine.export.JRRtfExporter.exportElements(JRRtfExporter.java:1325)
at net.sf.jasperreports.engine.export.JRRtfExporter.exportPage(JRRtfExporter.java:480)
at net.sf.jasperreports.engine.export.JRRtfExporter.exportReportToStream(JRRtfExporter.java:299)
at net.sf.jasperreports.engine.export.JRRtfExporter.exportReport(JRRtfExporter.java:185)
at org.apache.jsp.bigcus.RFIDCard.RFIDCardList_jsp._jspService(RFIDCardList_jsp.java:213)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)  回复  更多评论
  

# re: 一个Jasperreport多表打印代码实战 2008-04-16 15:44 衣顺利
我的邮箱是yishunliy@163.com ,请回复到我得邮箱 ,谢谢啦  回复  更多评论
  

# re: 一个Jasperreport多表打印代码实战 2008-05-05 16:04 放水老倌
@衣顺利
已经给您回复邮件了,注意查收。  回复  更多评论
  

# re: 一个Jasperreport多表打印代码实战[未登录] 2009-01-20 17:19 phoenix
我也出现这个问题了,不知道怎么回事,在保存rtf时候
谢谢回复wangping127@tom.com  回复  更多评论
  

# re: 一个Jasperreport多表打印代码实战 2009-05-19 22:02 Alex.tong
tongjiallin313@126.com 同样的问题  回复  更多评论
  

# re: 一个Jasperreport多表打印代码实战[未登录] 2009-12-24 14:20 iii
我也出现这个问题了,不知道怎么回事,在保存rtf时候
谢谢回复wangping127@tom.com @衣顺利
  回复  更多评论
  

# re: 一个Jasperreport多表打印代码实战[未登录] 2009-12-24 14:21 voracle
我也出现这个问题了,不知道怎么回事,在保存rtf时候
谢谢回复hikarus7@gmail.com @衣顺利
  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: