使用JavaBean作为数据源构造JasperReport子报表样式如下:
构造我的JavaBean如下(get,set方法省略):
public class Person {
private String name;
private String age;
private String sex;
private String telephone;
}
public class Depart {
private String subTitle;
private String depart_name;
private List<Person> personList = null;
}
构造我们的Service:
public class DepartService {
public List<Depart> getAllDepart() {
List<Depart> departList = new ArrayList<Depart>();
List<Person> personList = null;
Depart depart = new Depart("测试一", "开发部");
personList = new ArrayList<Person>();
personList.add(new Person("小博", "22", "男", "123456"));
personList.add(new Person("张三", "21", "男", "321456"));
personList.add(new Person("李四", "24", "女", "654321"));
personList.add(new Person("王五", "23", "男", "123456"));
depart.setPersonList(personList);
departList.add(depart);
depart = new Depart("测试二", "研发部");
personList = new ArrayList<Person>();
personList.add(new Person("小博", "22", "男", "123456"));
personList.add(new Person("李四", "24", "女", "654321"));
depart.setPersonList(personList);
departList.add(depart);
departList.add(depart);
return departList;
}
}
构造我们的Servlet:
public class JRPDFServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DepartService departMght = new DepartService();
List<Depart> departList = departMght.getAllDepart();
ServletContext servletContext = this.getServletConfig().getServletContext();
File departJasperFile = new File(servletContext.getRealPath("/jasper/Main_Department_List.jasper"));
// 设置子报表的路径
String root_path = servletContext.getRealPath("/");
root_path = root_path.replace("\\", "\\\\");
root_path = root_path + "jasper\\\\";
Map<String, String> parameters = new HashMap<String, String>();
JasperPrint jasperPrint = null;
try {
JasperReport departJasperReport = (JasperReport) JRLoader
.loadObject(departJasperFile.getPath());
parameters.put("SUBREPORT_DIR", root_path);
jasperPrint = JasperFillManager.fillReport(departJasperReport,
parameters, new JRBeanCollectionDataSource(departList));
} catch (JRException e) {
e.printStackTrace();
}
if (null != jasperPrint) {
FileBufferedOutputStream fbos = new FileBufferedOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);
exporter
.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
try {
exporter.exportReport();
fbos.close();
if (fbos.size() > 0) {
response.setContentType("application/pdf");
response.setContentLength(fbos.size());
ServletOutputStream ouputStream = response
.getOutputStream();
try {
fbos.writeData(ouputStream);
fbos.dispose();
ouputStream.flush();
} finally {
if (null != ouputStream) {
ouputStream.close();
}
}
}
} catch (JRException e1) {
e1.printStackTrace();
} finally {
if (null != fbos) {
fbos.close();
fbos.dispose();
}
}
}
}
}
在web.xml配置servlet如下:
<servlet> <servlet-name>JRPDFServlet</servlet-name> <servlet-class>com.mengya.servlet.JRPDFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>JRPDFServlet</servlet-name> <url-pattern>/JRSubPDFServlet</url-pattern> </servlet-mapping>
页面调用:
<h3 align="center"><a href="JRSubPDFServlet">子表报</a></h3>
主要是报表设计如下面附件图片。