源码如下:
<%@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();
}
}
%>