日期:2014-05-16  浏览次数:20924 次

jasperreport 通过javabean datasoource实现chart的报表

       继上次report的demo后,还在继续做着report方面的research,今天主要是实现了通过javabean datasource填充chart图表,通过webservice下载pdf格式的报表。

       开始这一段快乐之旅吧,首先我在包下定义了两个类分别是Student和Mark,Student类下有三个属性,他们分别是

private String name;
private String number;
private List<Mark> mark;
Mark类下定义了两个属性

private String name;
private double mark;
另外写了一个函数去获取student的列表List,这将是填充JavaBean的数据源

public List<Student> getBeanData(){
		List<Student> listStudent = new ArrayList<Student>();
		Student student = new Student();
		student.setName("cheng");
		student.setNumber("01");
		
		List<Mark> listMark = new ArrayList<Mark>();
		listMark.add(new Mark("销售", 100));
		listMark.add(new Mark("IT", 139));
		listMark.add(new Mark("s", 110.6));
		listMark.add(new Mark("d", 120.4));
		listMark.add(new Mark("e", 120));
		
		student.setMark(listMark);
		listStudent.add(student);
		return listStudent;
	}
设计report chart报表


最后的任务是写webservice,打印出报表,贴主要的代码实现,有一些函数在上一篇文章中已经实现

public List<Student> getBeanData(){
		List<Student> listStudent = new ArrayList<Student>();
		Student student = new Student();
		student.setName("cheng");
		student.setNumber("01");
		
		List<Mark> listMark = new ArrayList<Mark>();
		listMark.add(new Mark("销售", 100));
		listMark.add(new Mark("IT", 139));
		listMark.add(new Mark("s", 110.6));
		listMark.add(new Mark("d", 120.4));
		listMark.add(new Mark("e", 120));
		
		student.setMark(listMark);
		listStudent.add(student);
		return listStudent;
	}
	
	//javabean
	@Path("download/chart")
	@Produces(MediaType.TEXT_PLAIN)
	@Transactional
	public String downloadReportByJavaBean(@Context HttpServletRequest req , @Context HttpServletResponse res) throws Exception{
		String designFilePath = req.getSession().getServletContext().getRealPath("/jasper") + 
				File.separator + "chartreport.jrxml";
		File designFile = new File(designFilePath);
		
		if(designFile.exists()){
			JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(getBeanData());
			JasperReport jasperReport = null;
			JasperDesign design = JRXmlLoader.load(designFile);
			jasperReport = JasperCompileManager.compileReport(design);
			
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,dataSource);
			XLSReportProcess process = new XLSReportProcess();
			process.exportWebReport(ReportProcess.Type.pdf, res, jasperPrint , "chart.pdf");
			
		}
		return "success";
	}

最后实现的效果是通过webservice自动下载pdf文档,内容显示