1、首先是用ireport设计报表,个人设计的如下:
设计代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<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="Image" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0342f951-157c-4709-b85e-daba5d610ba9">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="normalStyle" isDefault="true" forecolor="#000000" backcolor="#FFFFFF" hAlign="Left" vAlign="Middle" isBlankWhenNull="true" fontName="宋体" fontSize="12" isBold="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
<parameter name="image" class="java.io.InputStream"/>
<parameter name="image2" class="java.lang.Object"/>
<parameter name="image3" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<summary>
<band height="351" splitType="Stretch">
<image>
<reportElement x="0" y="0" width="113" height="75" uuid="6645d932-3fb1-42a3-aee8-d0c3ff82728b"/>
<imageExpression><![CDATA[$P{image3}]]></imageExpression>
</image>
<image>
<reportElement x="113" y="0" width="115" height="75" uuid="0fe88379-8aeb-4e37-883a-bc3418819793"/>
<imageExpression><![CDATA[new java.io.File("C:\\Users\\caoyunyun\\Desktop\\下载.jpg")]]></imageExpression>
</image>
<image>
<reportElement x="228" y="0" width="112" height="75" uuid="227fecec-989b-4ff5-a135-40141b097c88"/>
<imageExpression><![CDATA[new java.net.URL($P{image3})]]></imageExpression>
</image>
<image>
<reportElement x="340" y="0" width="113" height="75" uuid="63321ae6-538c-4265-bb64-f5a94edb7c98"/>
<imageExpression><![CDATA[$P{image}]]></imageExpression>
</image>
<image>
<reportElement x="0" y="75" width="113" height="76" uuid="7282ea0c-0467-435d-b7c5-26d6ef048011"/>
<imageExpression><![CDATA[$P{image2}]]></imageExpression>
</image>
</band>
</summary>
</jasperReport>
这里Image有一个比较重要的属性:on Error Type
默认是选Error,如果图片加载不到(比如图片不存、传参错误之类的),就会报错
Blank是如果加载不到图片,就显示空白,不会报错
Icon是如果加载不到图片,就会显示默认的图标
2、导出代码(用的springMVC注解)
@RequestMapping(value = "reportImageAsPdf")
public String reportImageAsPdf(HttpServletRequest request, HttpServletResponse response){
String path = request.getSession().getServletContext().getRealPath("/");
//报表文件的路径
String filepath = path + "WEB-INF/page/report/Image.jasper";
//项目下一张图片的路径
String imagePath = path+"WEB-INF/page/image/b.png";
filepath = filepath.replace("\\", "/");
imagePath = imagePath.replace("\\", "/");
try {
ServletOutputStream sosRef = response.getOutputStream();
HashMap map = new HashMap();
//读取项目下的一张图片
InputStream is = new FileInputStream(imagePath);
//读取本地的一张图片
Image img = ImageIO.read(new FileInputStream("f:/image/e.jpg"));
map.put("image", is);//流
map.put("image2", img);//Image
map.put("image3", "https://img0.bdstatic.com/static/searchdetail/img/logo-2X_b99594a.png");
//加载报表文件
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File(filepath));
//填充报表
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, new JREmptyDataSource());
List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
jasperPrintList.add(jasperPrint);
response.setContentType("application/pdf");
//报表导出
JRPdfExporter exporter = new JRPdfExporter();
SimpleExporterInput sei = SimpleExporterInput.getInstance(jasperPrintList);
SimpleOutputStreamExporterOutput soseo = new SimpleOutputStreamExporterOutput(sosRef);
exporter.setExporterInput(sei);
exporter.setExporterOutput(soseo);
exporter.exportReport();
sosRef.flush();
sosRef.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
} catch (JRException e) {
e.printStackTrace();
}
return null;
}
测试结果如下: