JasperReport学习时的demo

package com.action;

import java.io.OutputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JExcelApiExporter;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRTextExporter;
import net.sf.jasperreports.engine.export.JRTextExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    @Autowired
    private JdbcTemplate jdbcTemplate = null;
    
    /**
     * 导出xls
     * @return
     * @throws Exception
     */
    public String exportToXls(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
        
        OutputStream output = null;
        output=response.getOutputStream();
        JRAbstractExporter exporter = new JExcelApiExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
        exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);
        // 不显示边框
        exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.TRUE);
        // 删除记录最下面的空行
        exporter.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
        exporter.exportReport();
        output.flush();
        return null;
    }
   
   /**
    * 导出pdf
    * @return
    * @throws Exception
    */
   public String exportToPdf(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
        
        OutputStream output = null;
        output=response.getOutputStream();
        JRAbstractExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
        exporter.exportReport();
        output.flush();
        return null;
    }
   
   /**
    * 导出html
    * @return
    * @throws Exception
    */
   public String exportToHtml(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
        
        
        OutputStream output = null;
        output=response.getOutputStream();
        JRAbstractExporter exporter = new JRHtmlExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,false);
        exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");
        exporter.exportReport();
        output.flush();
        return null;
    }
   
   /**
    * 导出word
    * @return
    * @throws Exception
    */
   public String exportToWord(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
        
        
        JRAbstractExporter exporter = new JRRtfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
        OutputStream output = null;
        output=response.getOutputStream();
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
        exporter.exportReport();
        output.flush();
        return null;
    }
   
   /**
    * 导出csv
    * @return
    * @throws Exception
    */
   public String exportToCsv(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
        
        
        JRAbstractExporter exporter = new JRCsvExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
        OutputStream output = null;
        output=response.getOutputStream();
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
    //    exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER,",");
    //    exporter.setParameter(JRCsvExporterParameter.RECORD_DELIMITER,"\n");
        exporter.exportReport();
        output.flush();
        return null;
    }
   /**
    * 导出txt
    * @return
    * @throws Exception
    */
   public String exportToTxt(JasperPrint jasperPrint,HttpServletResponse response) throws Exception {
        
        
        JRAbstractExporter exporter = new JRTextExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
        OutputStream output = null;
        output=response.getOutputStream();
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
        exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH,new Float(5));
        exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT,new Float(35));
        exporter.exportReport();
        output.flush();
        return null;
    }
    public String exportReport()throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        String id= request.getParameter("id");
        String name= request.getParameter("name");
        String type= request.getParameter("type");
        Connection con = jdbcTemplate.getDataSource().getConnection();
        String realPath = request.getRealPath("/report/report3.jasper");
        StringBuilder sb =  new StringBuilder("select * from student where 1=1");
        if (StringUtils.hasLength(id)) {
            
            sb.append(" and id="+id);
        }
       if (!StringUtils.hasLength(name)) {
            
            name = new String(("学生基本信息统计表."+type).getBytes("utf-8"),"ISO8859-1");
        }
       response.setContentType("application/octet-stream;charset=gbk");
       response.setHeader("Content-disposition","attachment;filename=\""+name+"\"");
       Map<String,String> parameters = new HashMap<String,String>();
       parameters.put("strSql",sb.toString());
       
       JasperReport jasperReport = (JasperReport)JRLoader.loadObject(realPath);
       
       JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, con);
       
       if ("xls".equals(type)) {
           
           this.exportToXls(jasperPrint, response);
       }else if("pdf".equals(type)){
           
           this.exportToPdf(jasperPrint, response);
       }else if("html".equals(type)){
           
           this.exportToHtml(jasperPrint, response);
       }else if("doc".equals(type)){
           
           this.exportToWord(jasperPrint, response);
       }else if("csv".equals(type)){
           
           this.exportToCsv(jasperPrint, response);
       }else if("txt".equals(type)){
           
           this.exportToTxt(jasperPrint, response);
       }
         return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例,演示如何在 Quarkus 应用程序中使用 JasperReports 来生成 PDF 报表并将其打印。 首先,您需要在 `pom.xml` 文件中添加以下依赖项: ```xml <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>6.17.0</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext-pdfa</artifactId> <version>5.5.13</version> </dependency> ``` 接下来,您需要创建一个 JasperReports 模板文件,可以使用 JasperReports Studio 或手动编写。这里我们假设模板文件名为 `template.jrxml`。 然后,您需要在您的代码中加载模板文件,并将数据填充到模板中: ```java // 加载模板文件 InputStream templateFile = getClass().getResourceAsStream("/template.jrxml"); JasperReport jasperReport = JasperCompileManager.compileReport(templateFile); // 填充数据 Map<String, Object> parameters = new HashMap<>(); parameters.put("name", "John Doe"); parameters.put("age", 30); JRDataSource dataSource = new JREmptyDataSource(); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource); ``` 接下来,您可以使用 `JasperExportManager` 将 JasperPrint 对象导出为 PDF 文件: ```java // 导出 PDF 文件 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream); byte[] pdfBytes = outputStream.toByteArray(); ``` 最后,您可以使用 Quarkus 提供的 `@Produces(MediaType.APPLICATION_PDF)` 注解将 PDF 文件作为响应返回: ```java @GET @Path("/print") @Produces(MediaType.APPLICATION_PDF) public Response printReport() throws JRException { // 加载模板文件 InputStream templateFile = getClass().getResourceAsStream("/template.jrxml"); JasperReport jasperReport = JasperCompileManager.compileReport(templateFile); // 填充数据 Map<String, Object> parameters = new HashMap<>(); parameters.put("name", "John Doe"); parameters.put("age", 30); JRDataSource dataSource = new JREmptyDataSource(); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource); // 导出 PDF 文件 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream); byte[] pdfBytes = outputStream.toByteArray(); // 返回 PDF 文件 return Response.ok(pdfBytes).build(); } ``` 这样,您就可以通过访问 `/print` 端点来生成并打印 PDF 报表了。请注意,此示例中使用的是 iText 2 和 JasperReports 6.17.0,您可以根据自己的需要使用其他版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值