欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jasper 使用,关于list的连续模板读取,读取不同模板

程序员文章站 2022-04-30 20:35:20
...

1.关于list的使用

定义一个dataset,定义list的字段

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{mcLableDetailList})

jasper 使用,关于list的连续模板读取,读取不同模板

 

这样就不用搞复杂的子父报表

2.连续读取不同的模板:

使用java代码实现:根据条件区分读取不同代码

// controller
    @GetMapping(value = { "printPalletLable" })
    public void printPalletLable(HttpServletResponse response) {
        try {
            List<LableVo> lableVoList;
            Map map = warpUsualParameters(super.getRequestParameters());

            lableVoList = relInvoiceService.printPalletLable(map);
            if (CollectionUtils.isEmpty(lableVoList)) {
                return;
            }
            List<LableVo> mcList = new ArrayList<>();
            List<LableVo> mxList = new ArrayList<>();
            List<JasperPrint> jasperPrintList = new ArrayList<>();
            Map<String, Object> params = new HashMap<>();
            String path = new ClassPathResource("reports/logs/anji.png").getPath();
            params.put("imageURL",path);
            lableVoList.stream().forEach(lableVo -> {
                if ("MC".equals(lableVo.getTarget())) {
                    mcList.add(lableVo);
                } else {
                    mxList.add(lableVo);
                }
            });
            if (CollectionUtils.isNotEmpty(mxList)) {
                InputStream inputStream = new ClassPathResource("reports/mxLable.jasper").getInputStream();
                JasperPrint print = JasperPrinter.fillReport(inputStream, params, mxList);
                jasperPrintList.add(print);
            }
            if (CollectionUtils.isNotEmpty(mcList)) {
                InputStream inputStream2 = new ClassPathResource("reports/mcLable.jasper").getInputStream();
                JasperPrint print2 = JasperPrinter.fillReport(inputStream2, params, mcList);
                jasperPrintList.add(print2);
            }
            JasperPrinter.exportPDFToWebList(jasperPrintList, null, response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
-----------------------------------------------------------------------------------------
//JasperPrinter 类
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @ClassName JasperPrinter
 * @Description: TODO
 * @Author dingkaiqiang
 * @Date 2019-11-28
 * @Version V1.0
 **/
public class JasperPrinter {
    public static void exportPDFToWeb(JasperPrint jasperPrint, String outputFileName, HttpServletResponse response)
            throws JRException, IOException {
        response.setContentType("application/pdf");
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
    }

    public static JasperPrint fillReport(InputStream inputStream, Map<String, Object> params, Collection<?> detailList)
            throws JRException {
        JasperPrint jasperPrint = null;
        JRBeanCollectionDataSource datasource = null;
        if (detailList != null && !detailList.isEmpty()) {
            datasource = new JRBeanCollectionDataSource(detailList);
            jasperPrint = JasperFillManager.fillReport(inputStream, params, datasource);
        } else {
            jasperPrint = JasperFillManager.fillReport(inputStream, params, new JREmptyDataSource());
        }
        return jasperPrint;
    }
    /**
     * 连续读取多个模板
     * @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();
        }


    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相关标签: 报表