使用JasperReport工具,生成报表模版,及通过JavaBean传参,常见问题及建议

1.下载JasperReport工具

下载地址:社区版 - Jaspersoft 社区icon-default.png?t=N7T8https://community.jaspersoft.com/download-jaspersoft/community-edition/

邮箱:lorettepatri.ckoa5434@gmail.com

密码:Zx123456.

2.工具使用方法注意

1.一次参数需要在左下角Parameters中新建,直接拖转右上角的TextField不会自动新建参数,到头来还是要在Parameters中新建

2.循环参数需要在左下角Fields中新建List中对象对应参数字段

3.常用的属性调整

4.字体设置,不支持中文,需要手动指定

5.排版时如何排版List数据

以本格式为例,中间的需要循环暂时,上下只显示一次.

参考数据:

我这里不是表格形式,所以没有列头,没用到Column Header,具体需根据自己情况安装

3.项目代码使用

1.引入必要文件

fonts.xml内容如下,只需要更改路径即可

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
    <!--
        可以配置多个字体
        name 属性:指定字体名称,这里的字体名称在 JasperReport 模板文件中使用的要一致,才能够匹配上
    -->
    <fontFamily name="MSYaHei">
        <!-- 正常字体路径 -->
        <normal><![CDATA[font/STSONG.TTF]]></normal>  <!-- 这里面为字体路径 -->
        <!-- 加粗字体路径 -->
        <bold><![CDATA[font/STSONG.TTF]]></bold>       <!-- 这里面为字体路径 -->
        <!-- 斜体字体路径 -->
        <italic><![CDATA[font/STSONG.TTF]]></italic>    <!-- 这里面为字体路径 -->
        <!-- 加粗斜体字体路径 -->
        <bolditalic><![CDATA[fonts/STSONG.TTF]]></bolditalic>   <!-- 这里面为字体路径 -->
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
        <pdfEncoding>Identity-H</pdfEncoding>
        <exportFonts/>
    </fontFamily>
</fontFamilies>
STSONG.TTF为华文宋体文件,这里的STSONG是文件名不是字体名,所以不用改成中文"华文宋体".字体下载地址

C:\Windows\Fonts

 jasperreports_extension.properties内容如下

# ?????????
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
# 这里是字体路径
net.sf.jasperreports.extension.simple.font.families.default=font/fonts.xml  

2.引入POM,注意jasperreports引入了许多外部jar包,不是这有问题,就是那个包maven仓库没有,这是我找到的对于我自己项目没报错的版本

<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
		<dependency>
			<groupId>net.sf.jasperreports</groupId>
			<artifactId>jasperreports</artifactId>
			<version>6.20.0</version>
			<exclusions>
				<!--
                    排除自带的itext依赖,因为自带的itext版本是 2.1.7.js10
                    这个版本在中央仓库里面没有,无法下载
                -->
				<exclusion>
					<groupId>com.lowagie</groupId>
					<artifactId>itext</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- 引入itext依赖,因为JasperReports中使用了itext操作PDF -->
		<dependency>
			<groupId>com.lowagie</groupId>
			<artifactId>itext</artifactId>
			<version>2.1.7</version>
		</dependency>

3.建议把jrxml源文件拷贝到项目中,避免本地丢失 

4.常见错误

1.java.lang.ClassNotFoundException大概率都是版本问题,换版本试一试是否还是同样错误

java.lang.ClassNotFoundException:com.lowagie.text.Document

2.jasperreort (wrong name: 大概率编译后的文件出了问题,我开始用的是工具编译,后面直接把jrxml文件放到项目中用代码编译成jasper文件,这样也可以保留jrxml文件在项目上,不会本地丢失

java.lang.NoClassDefFoundError: Jrxxxxxxxx _1811b31b27d13da1f8bc9955038dc0646b5de2a76581526d5a0228d015ebfd49 (wrong name: Jrxxxxxxxx )

5.简要代码(包含集合打印及多页打印)

public void downloadInspectPDF(String hisPatientNum,String dateTime) {
        List<InspectPDFInfoVo> inspectPDFInfoList = inspectPDFMapper.getInspectPDFInfo(hisPatientNum, dateTime);
        //new一个装多页打印JasperPrint的集合
        List<JasperPrint> jasperPrintList = new ArrayList<>();
        //我这里查询出来是一个集合数据,需要打印多页
        for (InspectPDFInfoVo inspectPDFInfoVo : inspectPDFInfoList) {
            inspectPDFInfoVo.setHisPatientNum(hisPatientNum);
            //这里就是需要一页里面的集合,也是上述Detail中循环的数据
            List<MedicationPDFInfo> medicationPDFInfoList = inspectPDFMapper.getMedicationPDFInfo(inspectPDFInfoVo.getInspectId());
            List<Map> list=new ArrayList<>();
            for (MedicationPDFInfo medicationPDFInfo : medicationPDFInfoList) {
                Map<String, Object> medicationInfoMap = BeanUtil.beanToMap(medicationPDFInfo,false,true);
                list.add(medicationInfoMap);
            }
            Map<String, Object> inspectPDFInfoVoMap = BeanUtil.beanToMap(inspectPDFInfoVo,false,true);
            String fileName = "XXXXXXX";
            try {
                //jrxml路径
                String jrxmlPath = ClassLoader.getSystemResource("").getPath() + "inspect_info.jrxml";
                //编译后jasper的路径
                String jasperPath = ClassLoader.getSystemResource("").getPath() + "inspect_info.jasper";
                JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
                JasperPrint jasperPrint =null;
                //我这边发现如果list为空,打印出来是空白的,就加了一个判断
                if(CollectionUtils.isEmpty(list)){
                    jasperPrint = JasperFillManager.fillReport(jasperPath, inspectPDFInfoVoMap, new JREmptyDataSource());
                }else{
                    jasperPrint = JasperFillManager.fillReport(jasperPath, inspectPDFInfoVoMap, new JRBeanCollectionDataSource(list));
                }
                jasperPrintList.add(jasperPrint);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        try {
            JasperPrinter.exportPDFToWebList(jasperPrintList, null, response);
        } catch (JRException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
JasperPrinter.java
 /**
     * 连续读取多个模板
     * @param jasperPrintList
     * @param outputFileName
     * @param response
     * @throws JRException
     * @throws IOException
     */
    public static void exportPDFToWebList(List<JasperPrint> jasperPrintList, String outputFileName, HttpServletResponse response)
            throws JRException, IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            JRPdfExporter exporter = new JRPdfExporter();
            //这里是多页打印重点
            exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
            exporter.exportReport();
            byte[] bytes = baos.toByteArray();
            // 写出文件的类型
            response.setContentType("application/pdf;charset=UTF-8");
            baos.close();
            outputStream.write(bytes);
        } finally {
            outputStream.flush();
        }


    }

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JasperReport是一个强大的报表生成工具,支持生成多种格式的报表,包括PDF、Excel、Word等。下面是使用JasperReport生成Excel报表的简单步骤: 1. 创建报表模板:使用JasperStudio创建报表模板,选择Excel作为输出格式,并设计报表布局和样式。 2. 定义数据源:定义报表所需的数据源,可以是数据库、XML文件等。 3. 编写Java代码:使用Java代码调用JasperReport API,读取数据源并将数据填充到报表模板中,最终生成Excel报表。 下面是一个简单的示例代码: ```java // 加载报表模板 JasperReport jasperReport = JasperCompileManager.compileReport("report.jrxml"); // 定义数据源 JRDataSource dataSource = new JREmptyDataSource(); // 填充数据并生成Excel报表 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource); JRXlsxExporter exporter = new JRXlsxExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("report.xlsx")); SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration(); configuration.setOnePagePerSheet(true); exporter.setConfiguration(configuration); exporter.exportReport(); ``` 在这个示例中,我们首先加载报表模板,然后定义一个空的数据源。接下来,使用JasperFillManager将数据源填充到报表模板中,并生成一个JasperPrint对象。最后,我们使用JRXlsxExporter将JasperPrint对象导出为Excel文件。在导出Excel文件时,我们可以通过SimpleXlsxReportConfiguration设置导出选项,例如每页一个工作表等。 需要注意的是,JasperReport生成Excel报表的效果可能不如专业的Excel工具,例如Microsoft Excel或LibreOffice Calc。如果需要生成复杂的Excel报表建议使用专业的Excel工具

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值