解析spring boot与ireport 整合问题

pom 文件依赖

  
            net.sf.jasperreports
            jasperreports
            6.10.0
        
        
            org.codehaus.groovy
            groovy-all
            2.4.11
        
        
        
        
            cn.lesper
            iTextAsian
            3.0
        

controller

 @GetMapping("/preview")
    public void print(HttpServletResponse response) throws Exception {
        List> data = getMaps();
        JRDataSource dataSource = new JRBeanCollectionDataSource(data);
        File file = ResourceUtils.getFile("classpath:jaspertemplate/abc.jrxml");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/pdf");
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("test", "UTF-8") + ".pdf");

        try (OutputStream outputStream = response.getOutputStream()) {
            //编译jrxml
            JasperReport jasperReport = JasperCompileManager.compileReport(file.getPath());
            //渲染加载数据
            HashMap params = new HashMap<>();
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
            JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
        } catch (IOException | JRException e) {
            e.printStackTrace();
        }
    }
  private List> getMaps() {
        List> data = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map m = new HashMap<>(3);
            m.put("id", "id" + i);
            m.put("name", "测试" + i);
            m.put("price", "price" + i);
            m.put("volTitle", "测试" + i);
            data.add(m);
        }
        return data;
    }


abc.jrxml 文件

path:src/main/resources/jaspertemplate



	
	
	
	
		
	
	
		
	
	
		
	
	
	
	
		
	
	
		
	
	
		
	
	
	
		
	
	
		<band height="75" splitType="Stretch">
			<staticText>
				<reportElement x="202" y="23" width="381" height="43" uuid="6dd7ca27-e8c4-4b52-964d-69045be9bf66"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="24" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[目 录]]></text>
			</staticText>
		</band>
	
	
		
			
				
				
					
					
					
					
					
				
				
					
				
				
			
			
				
				
					
					
					
					
					
				
				
					
				
				
			
			
				
				
					
					
					
					
					
				
				
					
				
				
			
			
				
				
					
					
					
					
					
				
				
					
				
				
			
			
				
				
					
					
					
					
				
				
					
				
				
			
			
				
				
					
					
				
				
					
				
				
			
			
				
				
					
					
					
					
				
				
					
				
				
			
		
	
	
		
			
				
				
					
					
					
					
					
				
				
					
				
				
			
			
				
				
					
					
					
					
					
				
				
					
				
				
			
			
				
				
					
					
					
					
				
				
					
				
				
			
			
				
				
					
				
				
					
				
				
			
			
				
				
					
					
				
				
					
				
				
			
			
				
				
					
					
					
					
					
				
				
					
				
				
			
			
				
				
					
					
				
				
					
				
				
			
			
				
				
					
					
				
				
					
				
				
			
			
				
				
					
					
					
				
				
					
				
				
			
			
				
				
					
					
				
				
					
				
				
			
		
	
	
		
	


其他

本文不涉及有关ireport报表如何设计问题,有需要请自行查询 Y(_)Y

到此这篇关于spring boot 与 ireport 整合的文章就介绍到这了,更多相关spring boot ireport 整合内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(解析spring boot与ireport 整合问题)