天天看點

jasperreports中圖檔的填充方式

1、首先是用ireport設計報表,個人設計的如下:

jasperreports中圖檔的填充方式

設計代碼如下:

<?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

jasperreports中圖檔的填充方式

預設是選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;
	}
           

測試結果如下:

jasperreports中圖檔的填充方式