2016-09-23 198 views
0

我正在开发用于生成求职者简历的jrxml模板。候选人在我的数据库中。Jasper Reports:将报告导出到多个文件

我需要生成1个记录Word文件(.DOCX)(由求职者),如下图:

Word file with individual candidate resume

我怎样才能让碧玉生成一个文件中的每个记录我的SQL查询?并将这些文件导出到Word?

我看到有一个参数叫做PAGE_INDEX exporter。但我没有找到如何使用它...

有人可以帮我吗?

注1:我的报告不是由JasperServer生成的。我开发了一个Java程序来生成它们并通过电子邮件发送报告。

注2:每个候选人的页数可能不同。

更新状态

我设法生成每个文件的一个记录。但我只能生成第一个记录的文件。 我需要为其余记录生成其他文件。 我仍然遇到另一个问题:当每个记录(候选实体)的页数可以改变时,如何分离成单独的文件? $ V {REPORT_COUNT},其中有记录数是在详细信息区域:

final JRDocxExporter exporter = new JRDocxExporter(); 
     exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); 
     exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(new java.io.File("/home/admin/resume candidate.docx"))); 
     SimpleDocxReportConfiguration configuration = new SimpleDocxReportConfiguration(); 
     configuration.setPageIndex(0); 
     exporter.setConfiguration(configuration); 
     exporter.exportReport(); 

+2

你应该考虑[接受](http://stackoverflow.com/help/accepted-answer)关于你的所有旧问题的一些答案,关于如何接受查看[tour],如果不接受问题仍然是开放的(没有有用的解决方案),未来的用户没有参考 –

回答

0

问题解决方法

我通过插入与表达每一页的页脚中的变量解决了这个问题: enter image description here

之后,Java程序会在JasperPrint对象的页面之间循环。 所以,我找到那个元素,告诉我哪个页面属于候选人。 基于此信息并存储候选索引数据及其页面(在HashMap> mapCandPage中),我可以确定每个候选项的起始页和结束页。这样我就可以为每个候选人记录导出一个文档。

public static void main(String args[]) throws Exception { 

     File relJasperArqFile = new File("Candidate Resume Template.jasper"); 
     Connection conn = ConnectionFactory.getNewConnectionSQLDRIVER(); 

     JasperReport jasperReport = (JasperReport) JRLoader.loadObject(relJasperArqFile); 

     JasperPrint jasperPrint 
       = JasperFillManager.fillReport(jasperReport, 
         null, 
         conn); 

     final JRDocxExporter exporter = new JRDocxExporter(); 
     exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); 

     List<JRPrintPage> listPrintPage = jasperPrint.getPages(); 

     int candIdx = 0; 
     int fileIdx = 0; 
     int lastCandIdx = 0; 
     HashMap<Integer, List<Integer>> mapCandPage = new HashMap<>(); 
     for (int pageIdx = 0; pageIdx < listPrintPage.size(); pageIdx++) { 
      JRPrintPage page = listPrintPage.get(pageIdx); 
      candIdx = getCandIdx(page); 

      if (!mapCandPage.containsKey(candIdx)) { 
       mapCandPage.put(candIdx, (new ArrayList<>())); 
      } 
      mapCandPage.get(candIdx).add(pageIdx); 

      if (pageIdx > 0 && candIdx != lastCandIdx) { 
       fileIdx++; 
       exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(new File(String.format("Candidate Resume %d.docx", fileIdx)))); 
       SimpleDocxReportConfiguration configuration = new SimpleDocxReportConfiguration(); 
       configuration.setStartPageIndex(mapCandPage.get(lastCandIdx).get(0)); 
       configuration.setEndPageIndex(mapCandPage.get(lastCandIdx).get(mapCandPage.get(lastCandIdx).size() - 1)); 
       exporter.setConfiguration(configuration); 
       exporter.exportReport(); 

      } 

      lastCandIdx = candIdx; 

     } 
     fileIdx++; 
     exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(new File(String.format("Candidate Resume %d.docx", fileIdx)))); 
     SimpleDocxReportConfiguration configuration = new SimpleDocxReportConfiguration(); 
     configuration.setStartPageIndex(mapCandPage.get(lastCandIdx).get(0)); 
     configuration.setEndPageIndex(mapCandPage.get(lastCandIdx).get(mapCandPage.get(lastCandIdx).size() - 1)); 
     exporter.setConfiguration(configuration); 
     exporter.exportReport(); 

    } 

    public static Integer getCandIdx(JRPrintPage page) { 
     JRPrintElement lastRowNumber = page.getElements().get(page.getElements().size() - 1); 
     return Integer.parseInt(((JRTemplatePrintText) lastRowNumber).getFullText()); 
    } 

这是一个测试,我的代码没有优化。如果有人有建议或更好的主意,请在这里发帖。谢谢。

相关问题