@Override public ReportDesign getReportDesign() { ReportDesign design = new ReportDesign(); design.setName("MOH 361A Register Design"); design.setReportDefinition(this.getReportDefinition()); design.setRendererType(ExcelTemplateRenderer.class); Properties props = new Properties(); props.put("repeatingSections", "sheet:2,row:4,dataset:allPatients"); design.setProperties(props); ReportDesignResource resource = new ReportDesignResource(); resource.setName("template.xls"); InputStream is = OpenmrsClassLoader.getInstance() .getResourceAsStream("templates/MOH361AReportTemplate_0_2.xls"); if (is == null) throw new APIException("Could not find report template."); try { resource.setContents(IOUtils.toByteArray(is)); } catch (IOException ex) { throw new APIException("Could not create report design for MOH 361A Register.", ex); } IOUtils.closeQuietly(is); design.addResource(resource); return design; }
@RequestMapping(method = RequestMethod.POST) public void process( final @RequestParam(required = false, value = "cohort") Integer cohortId, final @RequestParam(required = true, value = "template") MultipartFile template, final HttpServletResponse response) throws IOException, EvaluationException { EvaluationContext context = new EvaluationContext(); context.addParameterValue("currentDate", new Date()); if (cohortId != null) { context.setBaseCohort(Context.getCohortService().getCohort(cohortId)); } PatientDataSetDefinition definition = new PatientDataSetDefinition(); definition.addColumn( "id", new PatientIdDataDefinition(), StringUtils.EMPTY, new ObjectFormatter()); ListConverter listConverter = new ListConverter(); listConverter.setMaxNumberOfItems(1); PatientIdentifierDataDefinition preferredIdentifier = new PatientIdentifierDataDefinition(); preferredIdentifier.addType(Context.getPatientService().getPatientIdentifierType(1)); definition.addColumn("identifier", preferredIdentifier, StringUtils.EMPTY, listConverter); definition.addColumn( "name", new PreferredNameDataDefinition(), StringUtils.EMPTY, new ObjectFormatter("{familyName}, {givenName}")); AgeDataDefinition ageOnDate = new AgeDataDefinition(); ageOnDate.addParameter(new Parameter("effectiveDate", "effective date", Date.class)); definition.addColumn("age", ageOnDate, "effectiveDate=${currentDate}", new AgeConverter()); definition.addColumn( "birthdate", new BirthdateDataDefinition(), StringUtils.EMPTY, new BirthdateConverter("dd-MMM-yyyy")); definition.addColumn( "gender", new GenderDataDefinition(), StringUtils.EMPTY, new ObjectFormatter()); ReportDefinition reportDefinition = new ReportDefinition(); reportDefinition.setName("Test Report"); reportDefinition.addDataSetDefinition("PatientDataSetDefinition", definition, null); final ReportDesign design = new ReportDesign(); design.setName("Test Report Design With Excel Template Renderer"); design.setReportDefinition(reportDefinition); design.setRendererType(ExcelTemplateRenderer.class); Properties properties = new Properties(); properties.put("repeatingSections", "sheet:1,dataset:PatientDataSetDefinition"); design.setProperties(properties); ReportDesignResource resource = new ReportDesignResource(); resource.setName("excel-template.xls"); InputStream inputStream = template.getInputStream(); resource.setContents(IOUtils.toByteArray(inputStream)); IOUtils.closeQuietly(inputStream); design.addResource(resource); ExcelTemplateRenderer renderer = new ExcelTemplateRenderer() { public ReportDesign getDesign(String argument) { return design; } }; ReportDefinitionService rs = Context.getService(ReportDefinitionService.class); ReportData data = rs.evaluate(reportDefinition, context); CsvReportRenderer csvReportRenderer = new CsvReportRenderer(); csvReportRenderer.render(data, "output:csv", System.out); File file = File.createTempFile("excel", "summary"); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); renderer.render(data, "output:xls", bufferedOutputStream); bufferedOutputStream.close(); response.setHeader("Content-Disposition", "attachment; filename=patient-summary.xls"); response.setContentType("application/vnd.ms-excel"); response.setContentLength((int) file.length()); FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream()); if (file.delete()) log.info("Temporary file deleted!"); }