若依+lodop+jasperreports+ireport 设计打印票据格式(三)

若依+lodop+jasperreports+ireport 设计打印票据格式(三)

实现效果1
若依+lodop+jasperreports+ireport 设计打印票据格式(三)_第1张图片
需要循环的数据放入ireport detai里面
java打印bean

package com.qlh.yhis.report.prints.bean;

import com.qlh.yhis.common.utils.DictUtils;
import com.qlh.yhis.common.utils.SecurityUtils;
import com.qlh.yhis.report.prints.IHandler;
import com.qlh.yhis.report.prints.PrintSetting;
import com.qlh.yhis.zy.domain.ZYj;
import com.qlh.yhis.zy.service.IZYjService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//打印预交明细
@Component
public class Zyyjmx  implements IHandler {

    protected Logger logger = LoggerFactory.getLogger(Zyyjksj.class);
    @Resource
    private IZYjService izYjService;

    @Override
    public void getPrintSetting(PrintSetting setting) {

    }

    @Override
    public void getParameters(Map<String, Object> request, Map<String, Object> response) {
        Map<String, Object> m = new HashMap<>();
        //病人住院号查询
        List<ZYj> zYjList = izYjService.zYjsByZBah((String) request.get("zBah"),SecurityUtils.getDeptId());
        Double sumYjje = new Double("0.000");
        for (int i = 0; i < zYjList.size() ; i++) {
            ZYj zYjPrint = zYjList.get(i);
            sumYjje+=zYjPrint.getzYjje().doubleValue();
        }
        response.put("deptName",SecurityUtils.getDept().getDeptName());
        String zdy = DictUtils.getDictLabel("SYS_USER",SecurityUtils.getUserId()+"");
        response.put("zdy",zdy);
        response.put("sumYjje",new BigDecimal(sumYjje));

    }

    @Override
    public void getFields(Map<String, Object> request, List<Map<String, Object>> records) {
        //病人住院号查询
        List<ZYj> zYjList = izYjService.zYjsByZBah((String) request.get("zBah"),SecurityUtils.getDeptId());
        for (int i = 0; i < zYjList.size() ; i++) {
            ZYj zYjPrint = zYjList.get(i);
            Map<String, Object> map = new HashMap<>();
            map.put("zBah",zYjPrint.getzBah());
            map.put("brxm",zYjPrint.getBrxm());
            map.put("yjje",zYjPrint.getzYjje());
            map.put("czy",zYjPrint.getCzy());
            map.put("zYjrq",zYjPrint.getzYjrq());
            map.put("hh",(i+1)+"");
            records.add(map);
        }
    }
}

对应打印jxrml文件


<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="zyyjmx" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true" uuid="bb5c8304-71b4-4739-b160-6704af9ba72f">
    <property name="ireport.zoom" value="1.331000000000001"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="zdy" class="java.lang.String"/>
    <parameter name="deptName" class="java.lang.String"/>
    <parameter name="sumYjje" class="java.math.BigDecimal"/>
    <field name="zBah" class="java.lang.String"/>
    <field name="brxm" class="java.lang.String"/>
    <field name="yjje" class="java.math.BigDecimal"/>
    <field name="czy" class="java.lang.String"/>
    <field name="zYjrq" class="java.util.Date"/>
    <field name="hh" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    background>
    <title>
        <band height="80" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="40" width="555" height="40" uuid="d39d1cc2-ca13-496d-9ddc-635866fd0358"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="30"/>
                textElement>
                <text>text>
            staticText>
            <textField>
                <reportElement x="0" y="0" width="555" height="40" uuid="e33c854f-60d3-4ac5-ab2a-7ecf5fc4947b"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="30"/>
                textElement>
                <textFieldExpression>textFieldExpression>
            textField>
        band>
    title>
    <columnHeader>
        <band height="50">
            <staticText>
                <reportElement x="0" y="0" width="76" height="30" uuid="77b65634-15e9-437e-850c-9d08cc9be0e4"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <text>text>
            staticText>
            <staticText>
                <reportElement x="207" y="0" width="73" height="30" uuid="3ee0e16f-e7f3-477c-a8a6-c3dbdc7939b1"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <text>text>
            staticText>
            <staticText>
                <reportElement x="535" y="0" width="19" height="30" uuid="4b79ed80-0479-4ffc-9269-2ec0f5ccd436"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <text>text>
            staticText>
            <textField evaluationTime="Report">
                <reportElement x="512" y="0" width="23" height="30" uuid="67ed400d-ac7e-4dfc-bfe2-b326bb047d43"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <textFieldExpression>textFieldExpression>
            textField>
            <staticText>
                <reportElement x="472" y="0" width="40" height="30" uuid="18c96254-21d0-4d4b-ab8c-56ca03d2070a"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <text>text>
            staticText>
            <textField>
                <reportElement x="455" y="0" width="17" height="30" uuid="b8471b2f-579d-4ea0-8912-1122b3bb86f0"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <textFieldExpression>textFieldExpression>
            textField>
            <staticText>
                <reportElement x="438" y="0" width="17" height="30" uuid="42a462fe-60da-4bfb-af74-f10860547360"/>
                <textElement textAlignment="Right" verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <text>text>
            staticText>
            <textField pattern="yyyy-MM-dd hh:mm:ss" isBlankWhenNull="true">
                <reportElement x="76" y="0" width="131" height="30" uuid="03e428ab-8455-4b27-9188-3847f3c9dc1a"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="宋体" size="12"/>
                textElement>
                <textFieldExpression>textFieldExpression>
            textField>
            <textField>
                <reportElement x="280" y="0" width="111" height="30" uuid="92056cae-3b2b-4828-a0d7-c2d30f9b4be3"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="宋体" size="12"/>
                textElement>
                <textFieldExpression>textFieldExpression>
            textField>
            <staticText>
                <reportElement x="0" y="30" width="61" height="20" uuid="bbd149a0-e989-437e-b5c7-54dc9a1ee452"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <text>text>
            staticText>
            <staticText>
                <reportElement x="61" y="30" width="110" height="20" uuid="fb0f371e-54a6-4a35-985f-2495f5e48fe8"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <text>text>
            staticText>
            <staticText>
                <reportElement x="171" y="30" width="70" height="20" uuid="90779022-ba56-4960-ab72-0a70316b9b2a"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <text>text>
            staticText>
            <staticText>
                <reportElement x="241" y="30" width="70" height="20" uuid="368f1422-71d5-4c7e-85e1-017fcf76a0a1"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <text>text>
            staticText>
            <staticText>
                <reportElement x="311" y="30" width="174" height="20" uuid="a683fd3f-3c82-477e-87f0-44022b650c03"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <text>text>
            staticText>
            <staticText>
                <reportElement x="485" y="30" width="70" height="20" uuid="a5c285c4-e0e6-4afd-950e-c0d2e814659a"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="14"/>
                textElement>
                <text>text>
            staticText>
        band>
    columnHeader>
    <detail>
        <band height="20" splitType="Immediate">
            <textField>
                <reportElement x="0" y="0" width="61" height="20" uuid="9ff99265-92b1-4575-b4e8-07ddb26cdbfd"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression>textFieldExpression>
            textField>
            <textField>
                <reportElement x="61" y="0" width="110" height="20" uuid="8429c402-8c97-41a8-a1a7-c93d1f09b0f5"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression>textFieldExpression>
            textField>
            <textField>
                <reportElement x="171" y="0" width="70" height="20" uuid="62078c2f-4fdf-4850-a361-0cc1ab4ed9b8"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression>textFieldExpression>
            textField>
            <textField>
                <reportElement x="241" y="0" width="70" height="20" uuid="b5f8fb3b-1ead-4324-9290-c22751ffdb69"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression>textFieldExpression>
            textField>
            <textField pattern="yyyy-MM-dd HH:mm:ss">
                <reportElement x="311" y="0" width="174" height="20" uuid="fc50432e-79f6-4d6d-b1aa-3540359dcae6"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression>textFieldExpression>
            textField>
            <textField>
                <reportElement x="485" y="0" width="70" height="20" uuid="b9e3c141-09fd-49c7-b61c-5dabfe524ccb"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression>textFieldExpression>
            textField>
        band>
    detail>
    <columnFooter>
        <band height="21">
            <staticText>
                <reportElement x="61" y="0" width="110" height="20" uuid="a9acd837-54ea-4fc0-ad81-2dde84cd63ac"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="宋体" size="12"/>
                textElement>
                <text>text>
            staticText>
            <textField>
                <reportElement x="241" y="0" width="70" height="20" uuid="c0210dff-96da-4b04-b807-d14f0710558f"/>
                <textElement>
                    <font fontName="宋体" size="12"/>
                textElement>
                <textFieldExpression>textFieldExpression>
            textField>
        band>
    columnFooter>
jasperReport>

实现效果2
若依+lodop+jasperreports+ireport 设计打印票据格式(三)_第2张图片
需要使用子表,就是主表里面放一个list,list存放map类型list中对应数据是子表数据
主jrxml


<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="HospitalizationOrderCareCard" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="eec368bc-45e5-4753-af53-5d9da5680535">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
		<defaultValueExpression>defaultValueExpression>
	parameter>
	<field name="jcName" class="java.lang.String"/>
	<field name="no" class="java.lang.String"/>
	<field name="hdName" class="java.lang.String"/>
	<field name="pyName" class="java.lang.String"/>
	<field name="YZDATA" class="net.sf.jasperreports.engine.JRDataSource"/>
	<background>
		<band splitType="Stretch"/>
	background>
	<title>
		<band splitType="Stretch"/>
	title>
	<pageHeader>
		<band splitType="Stretch"/>
	pageHeader>
	<columnHeader>
		<band splitType="Stretch"/>
	columnHeader>
	<detail>
		<band height="245" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="31" width="100" height="29" uuid="581e0cb2-cf08-47ed-8c98-29250bfcc326"/>
				<textElement verticalAlignment="Bottom">
					<font fontName="宋体" size="16"/>
				textElement>
				<text>text>
			staticText>
			<staticText>
				<reportElement x="243" y="31" width="67" height="29" uuid="4b635098-4fda-4b6f-914a-a8e83209590c"/>
				<textElement verticalAlignment="Bottom">
					<font fontName="宋体" size="16"/>
				textElement>
				<text>text>
			staticText>
			<textField>
				<reportElement x="0" y="0" width="316" height="31" uuid="f0bac411-2ab4-4876-8abf-4da2dd90c3d8"/>
				<textElement verticalAlignment="Bottom">
					<font fontName="宋体" size="16"/>
				textElement>
				<textFieldExpression>textFieldExpression>
			textField>
			<staticText>
				<reportElement x="316" y="0" width="40" height="31" uuid="223a414a-3039-40da-9d08-497e281bf2ac"/>
				<textElement verticalAlignment="Bottom">
					<font fontName="宋体" size="16"/>
				textElement>
				<text>text>
			staticText>
			<textField>
				<reportElement x="356" y="0" width="199" height="31" uuid="51fb2f72-ad22-49db-994d-929b82c79794"/>
				<textElement verticalAlignment="Bottom">
					<font fontName="宋体" size="16"/>
				textElement>
				<textFieldExpression>textFieldExpression>
			textField>
			<textField>
				<reportElement x="100" y="31" width="143" height="29" uuid="1fafb55a-e991-494b-9b67-7d94811d160e"/>
				<textElement verticalAlignment="Bottom">
					<font fontName="宋体" size="16"/>
				textElement>
				<textFieldExpression>textFieldExpression>
			textField>
			<textField>
				<reportElement x="310" y="31" width="245" height="29" uuid="69bdd0a4-0da8-48cd-bf13-aeee79e3ed91"/>
				<textElement verticalAlignment="Bottom">
					<font fontName="宋体" size="16"/>
				textElement>
				<textFieldExpression>textFieldExpression>
			textField>
			<textField>
				<reportElement x="167" y="60" width="76" height="44" uuid="653c9283-ebcb-4a44-a275-249efd15cd80"/>
				<textElement verticalAlignment="Middle">
					<font fontName="宋体" size="16"/>
				textElement>
				<textFieldExpression>textFieldExpression>
			textField>
			<textField>
				<reportElement x="167" y="104" width="76" height="47" uuid="5aafcdd3-1c8e-4353-a4f8-3bb41b107a1f"/>
				<textElement verticalAlignment="Middle">
					<font fontName="宋体" size="16"/>
				textElement>
				<textFieldExpression>textFieldExpression>
			textField>
			<textField>
				<reportElement x="167" y="151" width="76" height="48" uuid="6c296cea-ef33-4dab-b1a5-4af7b8b94b49"/>
				<textElement verticalAlignment="Middle">
					<font fontName="宋体" size="16"/>
				textElement>
				<textFieldExpression>textFieldExpression>
			textField>
			<textField>
				<reportElement x="243" y="60" width="199" height="44" uuid="f8564e93-4b9f-464f-97a1-1ad78655f379"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font fontName="宋体" size="16" isBold="true"/>
				textElement>
				<textFieldExpression>textFieldExpression>
			textField>
			<textField>
				<reportElement x="243" y="104" width="199" height="47" uuid="5cc277d0-0754-4235-9c3c-970a17909a6c"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font fontName="宋体" size="16"/>
				textElement>
				<textFieldExpression>textFieldExpression>
			textField>
			<subreport>
				<reportElement x="0" y="199" width="555" height="46" uuid="ce7e3297-2d9c-41b7-bede-ee35e2ec3803"/>
				<dataSourceExpression>dataSourceExpression>
				<subreportExpression>subreportExpression>
			subreport>
		band>
	detail>
	<columnFooter>
		<band splitType="Stretch"/>
	columnFooter>
	<pageFooter>
		<band splitType="Stretch"/>
	pageFooter>
	<summary>
		<band splitType="Stretch"/>
	summary>
jasperReport>

子jrxml


<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="HospitalizationOrderCareCardChildren" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2ea6d6d2-9b86-4cbb-8d1f-9a69eca61dab">
	<property name="ireport.zoom" value="1.331000000000001"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<field name="YZDATA" class="java.lang.String"/>
	<field name="ypName" class="java.lang.String"/>
	<field name="sl" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	background>
	<title>
		<band splitType="Stretch"/>
	title>
	<pageHeader>
		<band splitType="Stretch"/>
	pageHeader>
	<columnHeader>
		<band splitType="Stretch"/>
	columnHeader>
	<detail>
		<band height="19" splitType="Stretch">
			<textField>
				<reportElement x="0" y="0" width="243" height="19" uuid="9d47cbf9-36ae-433b-b378-9e55cd267456"/>
				<textElement verticalAlignment="Middle">
					<font fontName="宋体" size="16"/>
				textElement>
				<textFieldExpression>textFieldExpression>
			textField>
			<textField>
				<reportElement x="243" y="0" width="312" height="19" uuid="b96c60cd-da68-4a44-a0e0-441d1ad65d68"/>
				<textElement verticalAlignment="Middle">
					<font fontName="宋体" size="16"/>
				textElement>
				<textFieldExpression>textFieldExpression>
			textField>
		band>
	detail>
	<columnFooter>
		<band splitType="Stretch"/>
	columnFooter>
	<pageFooter>
		<band splitType="Stretch"/>
	pageFooter>
	<summary>
		<band splitType="Stretch"/>
	summary>
jasperReport>

java bean

package com.qlh.yhis.report.prints.bean;

import com.qlh.yhis.mz.vo.PrintYZDemo;
import com.qlh.yhis.report.prints.IHandler;
import com.qlh.yhis.report.prints.PrintSetting;
import com.qlh.yhis.report.utils.PrintUtil;
import com.qlh.yhis.zy.service.IZyBqyzService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 住院医嘱卡片打印
 */

@Component
public class HospitalizationOrderCareCard implements IHandler {

    protected Logger logger = LoggerFactory.getLogger(HospitalizationOrderCareCard.class);
    @Resource
    private IZyBqyzService iZyBqyzService;
    @Override
    public void getPrintSetting(PrintSetting setting) {

    }

    @Override
    public void getParameters(Map<String, Object> request, Map<String, Object> response) {
        // 获得项目的绝对路径 并将路径值传进SUBREPORT_DIR中,
        // 因子报表SUBREPORT_DIR参数值在不同服务器上路径不同,所以用动态路径传值
        response.put("SUBREPORT_DIR","D:\\ZQ\\yunhis\\yhis-report\\src\\main\\resources\\jrxml\\");
    }

    @Override
    public void getFields(Map<String, Object> request, List<Map<String, Object>> records) {
        /**
         * 查询出子表中的数据list 循环给子表添加数据
         * 把整个输出的对象放在detail里,后端把每个对象放在list。
         */
        String zBah = (String) request.get("zBah");
        List<PrintYZDemo> demos = iZyBqyzService.selectZyBqyzByzBah(zBah);
        Map<String, List<Map<String, Object>>> hzmap = new HashMap<>();
        for (PrintYZDemo obj : demos) {
            String zXm = obj.getZXm();
            if (hzmap.containsKey(zXm)) {
                List<Map<String, Object>> maps = hzmap.get(zXm);
                Map<String, Object> map = new HashMap<>();
                map.put("ypName",obj.getYZMC());
                map.put("sl",obj.getYCSL().toString());
                maps.add(map);
            } else {
                List<Map<String, Object>> xms = new ArrayList<>();
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("ypName",obj.getYZMC());
                hashMap.put("sl",obj.getYCSL().toString());
                xms.add(hashMap);
                hzmap.put(zXm,xms);
            }
        }
        List<String> list = demos.stream().map(PrintYZDemo::getZXm).distinct()
                .collect(Collectors.toList());
        for (int i = 0; i < 5; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("jcName","地佐辛注射液");
            map.put("no","123456");
            map.put("hdName","ces");
            map.put("pyName","测试");
            map.put("name","测试");
            map.put("age","45");
            map.put("jzh","10000012");
            map.put("cw","3床");
            map.put("date","2025-10-10");
            // 这里面放list
            List<Map<String, Object>> maps = hzmap.get("测试");
            // 这个类型有问题
            map.put("YZDATA", PrintUtil.ds(maps));
            records.add(map);
        }

    }
}

打印辅助类

package com.qlh.yhis.report.utils;

import cn.hutool.core.util.ObjectUtil;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.*;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.xml.JRXmlTemplateLoader;
import net.sf.jasperreports.export.*;
import net.sf.jasperreports.web.util.WebHtmlResourceHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.util.*;

public class PrintUtil {

    public final static int REPORT_TYPE_OF_PDF = 0;
    public final static int REPORT_TYPE_OF_HTML = 1;
    public final static int REPORT_TYPE_OF_WORD = 2;
    public final static int REPORT_TYPE_OF_EXCEL = 3;
    public final static int REPORT_TYPE_OF_XML = 4;

    private final static String ContentTypeOfHtml = "text/html;charset=UTF-8";
    private final static String ContentTypeOfWord = "application/msword;charset=UTF-8";
    private final static String ContentTypeOfExcel = "application/vnd.ms-excel;charset=UTF-8";
    private final static String ContentTypeOfPdf = "application/pdf;charset=UTF-8";
    private final static String[] postfix = {".pdf", ".html", ".doc", ".xls", ".xml"};

    public static String LOAD = "phis.simpleLoad";
    public static String QUERY = "phis.simpleQuery";

    // get JRTemplate
    public static JRTemplate getJRTemplate(String fileName) throws JRException {
        return JRXmlTemplateLoader.load(fileName);
    }

    // get JasperDesign
    public static JasperDesign getJasperDesign(String fileName) throws JRException {
        return JRXmlLoader.load(fileName);
    }

    public static JasperDesign getJasperDesign(File file) throws JRException {
        return JRXmlLoader.load(file);
    }

    public static JasperDesign getJasperDesign(InputStream is) throws JRException {
        return JRXmlLoader.load(is);
    }

    // get JasperReport
    public static JasperReport getJasperReport(String fileName) throws JRException {
        return JasperCompileManager.compileReport(fileName);
    }

    public static JasperReport getJasperReport(InputStream is) throws JRException {
        return JasperCompileManager.compileReport(is);
    }

    public static JasperReport getJasperReport(JasperDesign jd) throws JRException {
        return JasperCompileManager.compileReport(jd);
    }

    // get JasperPrint
    public static JasperPrint getJasperPrint(String fileName, Map<String, Object> parameters) throws JRException {
        return getJasperPrint(getJasperReport(fileName), parameters);
    }

    public static JasperPrint getJasperPrint(String fileName, Map<String, Object> parameters, Connection con) throws JRException {
        return getJasperPrint(getJasperReport(fileName), parameters, con);
    }

    public static JasperPrint getJasperPrint(JasperReport jr, Map<String, Object> parameters) throws JRException {
        return JasperFillManager.fillReport(jr, parameters);
    }

    public static JasperPrint getJasperPrint(JasperReport jr, Map<String, Object> parameters, Connection con) throws JRException {
        return JasperFillManager.fillReport(jr, parameters, con);
    }

    public static JasperPrint getJasperPrint(JasperReport jr, Map<String, Object> parameters, JRDataSource dataSource) throws JRException {
        return JasperFillManager.fillReport(jr, parameters, dataSource);
    }

    // export to httpservletresponse
    public static void exportToHttpServletResponse(int type, JasperPrint jp, HttpServletRequest request, HttpServletResponse response, String title) throws IOException, JRException {
//        JRExporter exporter = getJRExporter(type, request, response, title);
//        response.setContentType(getContentType(exporter));
//        request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jp);
//        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
//        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
//        exporter.exportReport();
        JRAbstractExporter exporter = getJRExporter(type, request, response, title);
        response.setContentType(getContentType(exporter));
        request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", jp);
        exporter.setExporterInput(new SimpleExporterInput(jp));
        //exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getOutputStream()));
        exporter.exportReport();
    }

    public static void exportToHttpServletResponse(int type, List<JasperPrint> list, HttpServletRequest request, HttpServletResponse response, String title) throws IOException, JRException {
        //        JRExporter exporter = getJRExporter(type, request, response, title);
//        response.setContentType(getContentType(exporter));
//        request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_LIST_SESSION_ATTRIBUTE, list);
//        exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, list);
//        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
//        exporter.exportReport();
        JRAbstractExporter exporter = getJRExporter(type, request, response, title);
        response.setContentType(getContentType(exporter));
        request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print_list", list);
        List<ExporterInputItem> l_sei=new ArrayList<ExporterInputItem>();
        if(list!=null&&list.size()>0){
            for(JasperPrint j:list){
                l_sei.add(new SimpleExporterInputItem(j));
            }
        }
        exporter.setExporterInput(new SimpleExporterInput(l_sei));
        exporter.exportReport();
    }

    // export type
    private static JRAbstractExporter getJRExporter(int type, HttpServletRequest request, HttpServletResponse response, String filename)  throws IOException{
        JRAbstractExporter exporter = null;
        if (ObjectUtil.isEmpty(filename)) {
            filename = String.valueOf(new Date().getTime());
        }
        try {
            filename = new String(filename.getBytes("GBK"), "iso-8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        switch (type) {
            case REPORT_TYPE_OF_HTML:
                String isLandscape = request.getParameter("landscape");
                boolean landscape = false;
                if (!ObjectUtil.isEmpty(isLandscape) && "1".equals(isLandscape)) {
                    landscape = true;
                }
                boolean silentPrint = false;
                String isSilentPrint = request.getParameter("silentPrint");
                if (!ObjectUtil.isEmpty(isSilentPrint) && "1".equals(isSilentPrint)) {
                    silentPrint = true;
                }
                exporter = new JRHtmlExporter();
                StringBuffer sb = new StringBuffer(" \n\n")
//			.append("\n")
                        .append("\n")
                        .append("\n\n");
// exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, sb.toString());
// exporter.setParameter(JRHtmlExporterParameter.ZOOM_RATIO, 1.25f);
// exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "print_image?image=");
// exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML,"
");
SimpleHtmlExporterConfiguration config=new SimpleHtmlExporterConfiguration(); config.setHtmlFooter(sb.toString()); config.setBetweenPagesHtml("
"
); exporter.setConfiguration(config); SimpleHtmlReportConfiguration r_config=new SimpleHtmlReportConfiguration(); r_config.setZoomRatio(1.25F); exporter.setConfiguration(r_config); SimpleHtmlExporterOutput htmlExport = new SimpleHtmlExporterOutput(response.getOutputStream()); htmlExport.setImageHandler( new WebHtmlResourceHandler("print_image?image=")); exporter.setExporterOutput(htmlExport); break; case REPORT_TYPE_OF_WORD: exporter = new JRRtfExporter(); response.setHeader("Content-Disposition", "inline; filename=\"" + filename + postfix[REPORT_TYPE_OF_WORD] + "\""); break; case REPORT_TYPE_OF_EXCEL: exporter =new JRXlsExporter(); // exporter = new JExcelApiExporter(); response.setHeader("Content-Disposition", "inline; filename=\""+filename+postfix[REPORT_TYPE_OF_EXCEL]+"\""); SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration(); conf.setWhitePageBackground(false); conf.setDetectCellType(true); exporter.setConfiguration(conf); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream())); //自动选择格式 // exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); break; case REPORT_TYPE_OF_XML: exporter = new JRXmlExporter(); response.setHeader("Content-Disposition", "inline; filename=\"" + filename + postfix[REPORT_TYPE_OF_XML] + "\""); break; default: exporter = new JRPdfExporter(); response.setHeader("Content-Disposition", "inline; filename=\"" + filename + postfix[REPORT_TYPE_OF_PDF] + "\""); break; } return exporter; } // response contenttype private static String getContentType(JRExporter exporter) { String responseContentType = ContentTypeOfHtml; if (exporter instanceof JRRtfExporter) { responseContentType = ContentTypeOfWord; } else if (exporter instanceof JRXlsExporter || exporter instanceof JExcelApiExporter) { responseContentType = ContentTypeOfExcel; } else if (exporter instanceof JRPdfExporter) { responseContentType = ContentTypeOfPdf; } return responseContentType; } public static JRDataSource ds(Collection col) { return new JRMapCollectionDataSource(col); } public static String getLOAD() { return LOAD; } public static void setLOAD(String lOAD) { LOAD = lOAD; } public static String getQUERY() { return QUERY; } public static void setQUERY(String qUERY) { QUERY = qUERY; } }

你可能感兴趣的:(elementui,java)