private byte[] generaXls(List lista) throws JRException { Map<String, Object> params = new HashMap<>(); JRXlsExporter exporter = new JRXlsExporter(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); JasperDesign jd = JRXmlLoader.load( this.getClass() .getResourceAsStream("/mx/edu/um/mateo/inventario/reportes/almacenes.jrxml")); JasperReport jasperReport = JasperCompileManager.compileReport(jd); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JRBeanCollectionDataSource(lista)); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byteArrayOutputStream); exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporter.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporter.exportReport(); byte[] archivo = byteArrayOutputStream.toByteArray(); return archivo; }
private static JRExporter getJREXporter(final String extension) { if ("pdf".equalsIgnoreCase(extension)) { JRPdfExporter exporter = new JRPdfExporter(); // exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, "this.print();"); return exporter; } else if ("html".equalsIgnoreCase(extension)) { return new JRHtmlExporter(); } else if ("xls".equalsIgnoreCase(extension)) { JRXlsExporter exporterXLS = new JRXlsExporter(); exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporterXLS.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); return exporterXLS; } else if ("txt".equalsIgnoreCase(extension)) { return new JRTextExporter(); } else if ("csv".equalsIgnoreCase(extension)) { return new JRCsvExporter(); } else if ("docx".equalsIgnoreCase(extension)) { return new JRDocxExporter(); } return null; }
/** 导出excel */ public static void exportExcel( JasperPrint jasperPrint, String defaultFilename, HttpServletRequest request, HttpServletResponse response) throws IOException, JRException { logger.debug("执行导出excel The method======= exportExcel() start......................."); /* * 设置头信息 */ response.setContentType("application/vnd.ms-excel"); String defaultname = null; if (defaultFilename.trim() != null && defaultFilename != null) { defaultname = defaultFilename + ".xls"; } else { defaultname = "export.xls"; } String fileName = new String(defaultname.getBytes("gbk"), "utf-8"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); ServletOutputStream ouputStream = response.getOutputStream(); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporter.exportReport(); ouputStream.flush(); ouputStream.close(); }
public static void exportToExcel(String jrprintFileName, String excelFileName) throws Exception { long start = System.currentTimeMillis(); File sourceFile = new File(URL + jrprintFileName); JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile); File destFile = new File(URL + excelFileName); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporter.exportReport(); System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); System.out.println("成功创建了一个Excel文档"); }
public void xls() throws JRException { long start = System.currentTimeMillis(); File sourceFile = new File("build/reports/DataSourceReport.jrprint"); JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls"); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporter.exportReport(); System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); }
public static void generateXLSReport(JasperPrint jp, String path) throws Exception { // JasperExportManager.exp exportReportToPdfFile(jp, path); if (jp != null) { JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jp); OutputStream output = new FileOutputStream(new File(path)); exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, output); System.out.print(exporter.getParameters().toString()); exporter.exportReport(); output.close(); } else { System.out.print("problme XLS"); } }
public static void exportReportXls(JasperPrint jp, String path) throws JRException, FileNotFoundException { JRXlsExporter exporter = new JRXlsExporter(); File outputFile = new File(path); File parentFile = outputFile.getParentFile(); if (parentFile != null) { parentFile.mkdirs(); } FileOutputStream fos = new FileOutputStream(outputFile); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos); exporter.setParameter(JRXlsAbstractExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporter.setParameter(JRXlsAbstractExporterParameter.IS_IGNORE_GRAPHICS, Boolean.FALSE); exporter.exportReport(); logger.debug("XLS Report exported: " + path); }
protected void generateXLSOutput( String reportName, JasperPrint jasperPrint, HttpServletResponse resp, HashMap<String, Object> params) throws IOException, JRException { String reportfilename = (reportName) + ".xls"; JRXlsExporter exporterXLS = new JRXlsExporter(); exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint); // exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.TRUE); exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporterXLS.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.FALSE); exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, resp.getOutputStream()); for (Iterator<?> iterator = params.keySet().iterator(); iterator.hasNext(); ) { String key = (String) iterator.next(); try { PdfReportParam.class.getDeclaredField(key); Field field = JRXlsExporterParameter.class.getDeclaredField(key); field.setAccessible(true); exporterXLS.setParameter( (JRExporterParameter) field.get(JRPdfExporterParameter.class), params.get(key)); } catch (Exception e) { // Xls Param will only valid in report param } } resp.setHeader("Content-Disposition", "inline;filename=" + reportfilename); resp.setContentType("application/vnd.ms-excel"); exporterXLS.exportReport(); resp.getOutputStream().close(); // clear(); }
public void xls() throws JRException { long start = System.currentTimeMillis(); File sourceFile = new File("build/reports/FirstJasper.jrprint"); JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls"); Map dateFormats = new HashMap(); dateFormats.put("EEE, MMM d, yyyy", "ddd, mmm d, yyyy"); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.FORMAT_PATTERNS_MAP, dateFormats); exporter.exportReport(); System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); }
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) { try { String portletId = ParamUtil.getString(resourceRequest, "portletId"); LOGGER.debug("portlet id--" + portletId); SimpleDateFormat timeStampFormat = new SimpleDateFormat(TIMESTAMP_FORMAT_FOR_EXCEL); PortletSession session = resourceRequest.getPortletSession(); String timeStamp = timeStampFormat.format(Calendar.getInstance().getTime()); String fileName = "Location_Tracker" + timeStamp + ".xls"; List<LocationDetailsVO> locationDetailList = LocationTrackerReportsUtil.getLocationDetailsVoList(resourceRequest); JRBeanCollectionDataSource reportList = new JRBeanCollectionDataSource(locationDetailList); JasperReport report = JasperCompileManager.compileReport( session.getPortletContext().getRealPath("/reports/location_tracker_report.jrxml")); JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String, Object>(), reportList); OutputStream os = resourceResponse.getPortletOutputStream(); JRXlsExporter exporterXLS = new JRXlsExporter(); exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print); exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, os); exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporterXLS.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporterXLS.exportReport(); resourceResponse.setContentType("application/x-excel"); resourceResponse.setProperty("Content-Disposition", "attachment; filename=" + fileName); } catch (JRException jre) { LOGGER.error("Error in exporting the resluts:" + jre.getMessage(), jre); } catch (Exception e) { LOGGER.error("Error in exporting the resluts:" + e.getMessage(), e); } }
/** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub try { System.out.println("Genetate Report"); // Get requested student id from user request // String studentId = request.getParameter("sid"); // Get requested report type from user request String reportType = request.getParameter("reportType"); // Setting Default report Type if (reportType == null || reportType.trim().length() == 0) { reportType = "pdf"; } // Get servlet Out put stream to displat report at client side ServletOutputStream sos = response.getOutputStream(); // Construct path for your .jrxml file ServletContext application = getServletContext(); String prefix = application.getRealPath("\\"); // String file = getInitParameter("reportTemplet"); String file = "AveTechEventReport.jrxml"; String path = prefix + file; System.out.println("Path : " + path); // Load that .jrxml file in Jasper Design JasperDesign jasperDesign = JRXmlLoader.load(path); // Compile that .jrxml file into .jasper file JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); /* Populate data from your database It must contain all the fields used in designing a .jrxml file Here we have used Java Bean Data Source in generating report. So populate ArrayList which contains your collection of your beans. */ JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(AdminDAO.getAllEventData(), false); // Filling the reports using your .jasper file and generated Java Bean Datasource JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, ds); // Response for generating PDF report if (reportType != null && reportType.equals("pdf")) { System.out.println("PDF"); response.setContentType("application/pdf"); JRPdfExporter expoterPDF = new JRPdfExporter(); expoterPDF.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); expoterPDF.setParameter(JRExporterParameter.OUTPUT_STREAM, sos); expoterPDF.exportReport(); } // Response for generating EXCEL report if (reportType != null && reportType.equals("excel")) { response.setContentType("application/vnd.ms-excel"); JRXlsExporter exporterXLS = new JRXlsExporter(); exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint); exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, sos); exporterXLS.exportReport(); } // Response for generating WORD report if (reportType != null && reportType.equals("word")) { response.setContentType("application/msword"); JRRtfExporter exporterRTF = new JRRtfExporter(); exporterRTF.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporterRTF.setParameter(JRExporterParameter.OUTPUT_STREAM, sos); exporterRTF.exportReport(); } // Closing all opened streams sos.flush(); sos.close(); } catch (Exception ex) { ex.printStackTrace(); } }
public static File generate(HrReport report, String outputFormat, String jrxmlUrl) throws IOException { String url = Context.getRuntimeProperties().getProperty("connection.url", null); Connection conn; try { conn = connect(url); } catch (SQLException e) { log.error("Error connecting to DB.", e); return null; } Map<String, Object> map = new HashMap<String, Object>(); for (HrReportParameter reportParameter : report.getParameters()) { if (reportParameter != null) if (reportParameter.getName() != null) map.put(reportParameter.getName(), reportParameter.getValue()); } log.debug("Report parameter map: " + map); String exportPath = OpenmrsUtil.getApplicationDataDirectory(); URL resourceUrl = new URL(jrxmlUrl); InputStream is = resourceUrl.openStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); FileWriter appDirJrxml = new FileWriter(new File(exportPath + report.getFileName())); BufferedWriter bw = new BufferedWriter(appDirJrxml); String line; while ((line = br.readLine()) != null) { bw.write(line); bw.write("\n"); } bw.flush(); bw.close(); JasperPrint jasperPrint = null; try { JasperDesign jasperDesign = JRXmlLoader.load(exportPath + report.getFileName()); JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); jasperPrint = JasperFillManager.fillReport(jasperReport, map, conn); if (outputFormat.equals("PDF")) { File pdfFile = new File( exportPath + report.getName() + new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date()) + ".pdf"); JasperExportManager.exportReportToPdfFile(jasperPrint, pdfFile.getAbsolutePath()); return pdfFile; } else if (outputFormat.equals("Excel")) { ByteArrayOutputStream output = new ByteArrayOutputStream(); File outputfile = new File( exportPath + report.getName() + new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date()) + ".xls"); OutputStream outputfileStream = new FileOutputStream(outputfile); JRXlsExporter exporterXLS = new JRXlsExporter(); exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint); exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, output); exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporterXLS.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporterXLS.exportReport(); outputfileStream.write(output.toByteArray()); outputfileStream.flush(); outputfileStream.close(); return outputfile; } } catch (JRException e) { log.error("Error generating report", e); } finally { try { if (!conn.isClosed()) { conn.close(); } } catch (SQLException e) { log.error("Exception closing report connection.", e); } } return null; }