ireport報表導出 pdf xls doc

/**
     * 生成PDF
     * 
     * @param srcPath 源報表模版文件
     * @param destPath pdf報表文件
     * @throws JRException
     */
    private void pdf(final String srcPath, final String destPath) throws JRException
    {
        long start = System.currentTimeMillis();

        File sourceFile = new File(srcPath);
        JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
        String destPdf = destPath + jasperPrint.getName() + ".pdf";

        JasperExportManager.exportReportToPdfFile(jasperPrint, destPdf);

        logger.debug("PDF creation time : {}", (System.currentTimeMillis() - start));
    }

    /**
     * 生成xls
     * 
     * @param srcPath 源報表模版文件
     * @param destPath xls報表文件
     * @throws JRException
     */
    private void xls(final String srcPath, final String destPath) throws JRException
    {
        long start = System.currentTimeMillis();
        File sourceFile = new File(srcPath);

        JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);

        File destFile = new File(destPath, 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();

        logger.debug("XLS creation time : {}", (System.currentTimeMillis() - start));
    }

    /**
     * 生成docx
     * 
     * @param srcPath 源報表模版文件
     * @param destPath docx報表文件
     * @throws JRException
     */
    private void docx(final String srcPath, final String destPath) throws JRException
    {
        long start = System.currentTimeMillis();
        File sourceFile = new File(srcPath);

        JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);

        File destFile = new File(destPath, jasperPrint.getName() + ".docx");

        JRDocxExporter exporter = new JRDocxExporter();

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());

        exporter.exportReport();

        logger.debug("DOCX creation time : {}", (System.currentTimeMillis() - start));
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章