日期:2014-05-16 浏览次数:20971 次
最近项目经理安排任务学习jasperreport报表的设计与制作,根据提供的资料以及在网上的学习,自己实现了一个可以通过外部传入数据下载excel的报表,
因为还在初学阶段,数据源暂时是在程序中传入,还没通过数据库或者javabean传入,接下来的时间里慢慢深入。首先是在IReport软件里设计好自己的报表图,我设计了一个很简单的报表,其实报表设计最重要的是其核心的几个过程设计报表,数据导入,输出报表
第一个程序是service里的下载代码
@Service("reportService") @Path("/report") public class ReportService { @Autowired private ICommonDao dao; @Path("download") @Produces(MediaType.TEXT_PLAIN) @Transactional public String downloadReport(@Context HttpServletRequest req , @Context HttpServletResponse res) throws Exception{ String designFilePath = req.getSession().getServletContext().getRealPath("/jasper") + File.separator + "reportTest.jrxml"; File designFile = new File(designFilePath); if(designFile.exists()){ DataReportProcess reportProcess = new DataReportProcess(); reportProcess.process(req, res, designFile); } return "success"; } }
这段代码里涉及DataReportProcess类
public class DataReportProcess extends XLSReportProcess{ /** *从模板文件编译获得jasperReport对象 *@return JasperReport对象 jasperReport * @throws JRException */ private JasperReport getJasperReport(File designFile) throws Exception{ JasperReport jasperReport = null; JasperDesign design = JRXmlLoader.load(designFile); jasperReport = JasperCompileManager.compileReport(design); return jasperReport; } public void process(HttpServletRequest req , HttpServletResponse res, File designFile) throws Exception{ String outputFileName = "dataReport.xlsx"; Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("name", "张三"); Collection<Map<String, ?>> dataMapList = new ArrayList<Map<String,?>>(); dataMapList.add(dataMap); JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(dataMapList); JasperReport jasperReport = this.getJasperReport(designFile); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,dataSource); this.exportWebReport(ReportProcess.Type.xlsx, res, jasperPrint , outputFileName); } }这里面的数据源接口用的是JRMapCollectionDataSource,现在是写死的key value。这个类中主要是jasperrepor中涉及到的一些API