注:每次替换新的.jrxml文件请删除target缓存
有用没用的都加上了
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.21.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-fonts</artifactId>
<version>6.9.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-pdfa</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class JasperUtil {
public static void exportToExcel(File sourceFile, OutputStream outputStream, Map<String, Object> params, JRBeanCollectionDataSource dataSource ){
try {
//获取源文件输入流
JasperReport jasperReport = JasperCompileManager.compileReport(sourceFile.getAbsolutePath());
// 填充报表
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
// 创建XLSX导出器
JRXlsxExporter xlsExporter = new JRXlsxExporter();
// 设置导出器输入和输出
xlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
// 设置XLSX报表配置
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
// 根据需要设置其他配置,比如是否自动调整行高、是否检测单元格类型等
configuration.setDetectCellType(true);
configuration.setCollapseRowSpan(false);
xlsExporter.setConfiguration(configuration);
// 导出报表
xlsExporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
}
public static void exportToPdf(File sourceFile, OutputStream outputStream, Map<String, Object> params, JRBeanCollectionDataSource dataSource ) throws FileNotFoundException {
try {
//获取源文件输入流
JasperReport jasperReport = JasperCompileManager.compileReport(sourceFile.getAbsolutePath());
// 填充报表
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
} catch (JRException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException {
ClassLoader classLoader = JasperUtil.class.getClassLoader();
String path = classLoader.getResource("aaaaa.jrxml").getPath();
// File outputFile = new File("D:\\aaaaa.xlsx");
File outputFile = new File("D:\\output.pdf");
FileOutputStream outputStream = new FileOutputStream(outputFile);
File sourceFile = new File(path);
Map<String, Object> params = new HashMap<>();
params.put("type","统计");
params.put("time","2012-02-17");
ArrayList<Map<String,Object>> arrayList = new ArrayList<>();
for (int i=0;i<20;i++){
Map<String,Object> tableData = new HashMap<>();
tableData.put("name","小明");
tableData.put("age","10");
tableData.put("sex","男");
tableData.put("hobby","工作");
tableData.put("weight","200");
arrayList.add(tableData);
}
// 添加需要的报表参数
params.put(JRParameter.IS_IGNORE_PAGINATION, Boolean.TRUE);
// 这里使用一个bean集合作为数据源
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(arrayList);
params.put("tableData",dataSource);
// exportToExcel(sourceFile, outputStream,params,dataSource );
exportToPdf(sourceFile, outputStream,params,dataSource );
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.20.6.final using JasperReports Library version 6.20.6-5c96b6aa8a39ac1dc6b6bea4b81168e16dd39231 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="aaaaa" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6ddcaf5e-3cb7-4c0e-a331-17402fa3272b">
<style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table 1_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table 1_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table 1_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="Empty Dataset1" uuid="0474d908-f206-41e3-9c3a-53c9a87edab9">
<field name="name" class="java.lang.String"/>
<field name="age" class="java.lang.String"/>
<field name="sex" class="java.lang.String"/>
<field name="hobby" class="java.lang.String"/>
<field name="weight" class="java.lang.String"/>
</subDataset>
<parameter name="type" class="java.lang.String"/>
<parameter name="time" class="java.lang.String"/>
<parameter name="tableData" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="88" splitType="Stretch">
<staticText>
<reportElement x="120" y="10" width="340" height="60" uuid="dc9538ed-5f82-48f6-8a40-9b5ecb4f118a"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑" size="37" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[这是一个标题]]></text>
</staticText>
<image>
<reportElement x="0" y="0" width="110" height="60" uuid="568b6969-c1c5-49c6-8978-b832617bb5a8"/>
<imageExpression><![CDATA["https://img-home.csdnimg.cn/images/20201124032511.png"]]></imageExpression>
</image>
</band>
</title>
<pageHeader>
<band height="92" splitType="Stretch">
<staticText>
<reportElement x="10" y="0" width="110" height="31" uuid="7148e1c0-9262-4fad-a61a-e582e3c171d9"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineColor="#000000"/>
</box>
<textElement>
<font fontName="微软雅黑" size="22" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[统计类型:]]></text>
</staticText>
<staticText>
<reportElement x="10" y="31" width="110" height="31" uuid="66aaf97c-1be9-4ec7-925b-ee3375c1237e"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineColor="#000000"/>
</box>
<textElement>
<font fontName="微软雅黑" size="22" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[统计时间:]]></text>
</staticText>
<textField>
<reportElement x="121" y="0" width="260" height="31" uuid="350b01fe-74d5-4c4b-9051-06195876895b"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{type}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="121" y="31" width="260" height="31" uuid="d8e182cb-d72d-483a-9f39-663726bf448d"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{time}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="128" splitType="Stretch">
<componentElement>
<reportElement x="0" y="10" width="560" height="100" uuid="d5e6bf70-589a-4405-8e7b-bc03d7644a55">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table 1_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table 1_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table 1_TD"/>
<property name="com.jaspersoft.studio.components.autoresize.next" value="true"/>
<property name="com.jaspersoft.studio.components.autoresize.proportional" value="true"/>
</reportElement>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="Empty Dataset1" uuid="2a3adb96-796c-48af-9da8-09350d117027">
<dataSourceExpression><![CDATA[$P{tableData}]]></dataSourceExpression>
</datasetRun>
<jr:column width="112" uuid="359963b1-fc01-4caf-9066-ca4ba669143b">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:columnHeader style="Table 1_CH" height="30" rowSpan="1">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
<staticText>
<reportElement x="0" y="0" width="112" height="30" uuid="7a7a1f0e-689b-4b22-ad32-42e786cdc485"/>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[名字]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table 1_TD" height="30">
<textField>
<reportElement x="0" y="0" width="112" height="30" uuid="f87017c9-1d91-4622-9fc9-080d0b0b8969"/>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="112" uuid="412c8396-9f0a-41ac-8029-f0d0733f2f4c">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
<jr:columnHeader style="Table 1_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="112" height="30" uuid="aafb1ccb-bd5a-4c41-b66e-f9fb45578a60"/>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[年龄]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table 1_TD" height="30">
<textField>
<reportElement x="0" y="0" width="112" height="30" uuid="f985b807-7aeb-4344-a2f7-98489a47ee1d"/>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="112" uuid="76ed064c-6b9e-4f06-9dcf-7b1be24874c1">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
<jr:columnHeader style="Table 1_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="112" height="30" uuid="75615abe-6f1c-41f4-a702-62c02de4fa73"/>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[性别]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table 1_TD" height="30">
<textField>
<reportElement x="0" y="0" width="112" height="30" uuid="c28a5922-317a-4332-bfca-25608994c65a"/>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{sex}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="112" uuid="8f6813ef-4b51-43b0-ac9d-dd3981e507ad">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column4"/>
<jr:columnHeader style="Table 1_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="112" height="30" uuid="1f88e37a-4603-48b4-92ad-62cdca8b9dba"/>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[爱好]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table 1_TD" height="30">
<textField>
<reportElement x="0" y="0" width="112" height="30" uuid="c06f5c75-02fb-4c2e-8ee8-202fe13d8c2f"/>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{hobby}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="112" uuid="fd4cb02d-8049-48ad-ba41-ddc244523a62">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column5"/>
<jr:columnHeader style="Table 1_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="112" height="30" uuid="88bbe300-a905-4223-82c9-83b6db4f0181"/>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[体重]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table 1_TD" height="30">
<textField>
<reportElement x="0" y="0" width="112" height="30" uuid="c4d60aa9-959a-4136-b135-2a93cc83254c"/>
<textElement>
<font fontName="微软雅黑" size="19" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{weight}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<pageFooter>
<band height="54" splitType="Stretch">
<staticText>
<reportElement x="10" y="12" width="100" height="31" uuid="9a7a2015-b612-46f6-84c1-62563dc4e486"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineColor="#000000"/>
</box>
<textElement>
<font fontName="微软雅黑" size="22" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[页数:]]></text>
</staticText>
<textField>
<reportElement x="110" y="12" width="260" height="31" uuid="26aa4b83-a0f2-464e-bc00-d3bbdc88fcb1"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font fontName="微软雅黑" size="19"/>
</textElement>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
看着没啥问题
Exception in thread "main" net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font:?
pdfFontName: Helvetica
pdfEncoding: Identity-H
isPdfEmbedded : true
网上下载微软雅黑字体,涉及侵权不能发,自己找吧
配置xml,记得改名,normal为字体的绝对路径
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="微软雅黑">
<normal>fonts/wryh.TTF</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.html">'微软雅黑', Arial, Helvetica, sans-serif</export>
<export key="net.sf.jasperreports.xhtml">'微软雅黑', Arial, Helvetica, sans-serif</export>
</exportFonts>
</fontFamily>
</fontFamilies>
配置配置文件 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.lobstertwo=fonts/fonts.xml
配置完成,开始导出
配置下编码,所有参数都要配置,否则显示不出来