jasperReport导出excel(springMvc)

代码

@RequestMapping(value="/exexcel")
    public void exportExcel(HttpServletRequest request,HttpServletResponse response) throws JRException, IOException{  

        //打印程序当前路径
        System.out.println(System.getProperty("user.dir"));  
        WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();  
         ServletContext servletContext = webApplicationContext.getServletContext(); 
         String jasperPath = servletContext.getRealPath(  
                    "/WEB-INF/jasper/report13.jasper");  
         Map<String,Object> params  = new HashMap<String,Object>();  

           //UserJs.getUser()是List类型数据源
            JRDataSource dataSource = new JRBeanCollectionDataSource(UserJs.getUser(), true);  
            //print文件  
            JasperPrint print =  JasperFillManager.fillReport(jasperPath, params, dataSource);  

           //以附件形式保存文件
            String generateFileName = "My文件.xlsx";  
            response.setContentType("application/vnd.ms-excel");  
                        response.setHeader("Content-disposition", "attachment; filename="  
                                + URLEncoder.encode(generateFileName,"utf8"));  

            //设置导出时参数  
            SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration();  
            conf.setWhitePageBackground(false);  
            conf.setDetectCellType(true);  

            JRXlsxExporter exporter = new JRXlsxExporter();  
            exporter.setConfiguration(conf);  


            //设置输入项  
            ExporterInput exporterInput = new SimpleExporterInput(print);  
            exporter.setExporterInput(exporterInput);  

            //设置输出项  
            OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(response.getOutputStream());  
            exporter.setExporterOutput(exporterOutput);  


            exporter.exportReport();   
    }

UserJs.java

package com.xie.vo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用于测试jasperReport
 * @author xie
 *
 */
public class UserJs {

    private int id;
    private String name;
    private int age;
    private String title;

    public UserJs() {
        super();
    }

    public UserJs(int id, String name, int age, String title) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.title=title;
    }


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }


    public static List<UserJs> getUser(){

        List<UserJs> listUserJs=new ArrayList<>();
        listUserJs.add(new UserJs(0,"xx",20,"user表"));
        listUserJs.add(new UserJs(1,"写好",23,"user表"));
        listUserJs.add(new UserJs(2, "pp", 32,"user表"));

        return listUserJs;
    }

    public static Map<String,Object> getUserMap(){

        Map<String,Object> map=new HashMap<>();
        map.put("id",0);
        map.put("name","好啊");
        map.put("age", 23);
        map.put("title", "表格");

        return map;
    }

    public String gettitle() {
        return title;
    }

    public void settitle(String title) {
        this.title = title;
    }

}

说明

参考(复制)了网上好多人的,比如:
http://blog.csdn.net/wangjun5159/article/details/51152403
http://blog.csdn.net/wangjun5159/article/details/51152403
http://blog.csdn.net/zhuhuiby/article/details/8569516

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JasperReport是一个强大的报表生成工具,支持生成多种格式的报表,包括PDF、Excel、Word等。下面是使用JasperReport生成Excel报表的简单步骤: 1. 创建报表模板:使用JasperStudio创建报表模板,选择Excel作为输出格式,并设计报表布局和样式。 2. 定义数据源:定义报表所需的数据源,可以是数据库、XML文件等。 3. 编写Java代码:使用Java代码调用JasperReport API,读取数据源并将数据填充到报表模板中,最终生成Excel报表。 下面是一个简单的示例代码: ```java // 加载报表模板 JasperReport jasperReport = JasperCompileManager.compileReport("report.jrxml"); // 定义数据源 JRDataSource dataSource = new JREmptyDataSource(); // 填充数据并生成Excel报表 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource); JRXlsxExporter exporter = new JRXlsxExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("report.xlsx")); SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration(); configuration.setOnePagePerSheet(true); exporter.setConfiguration(configuration); exporter.exportReport(); ``` 在这个示例中,我们首先加载报表模板,然后定义一个空的数据源。接下来,使用JasperFillManager将数据源填充到报表模板中,并生成一个JasperPrint对象。最后,我们使用JRXlsxExporter将JasperPrint对象导出Excel文件。在导出Excel文件时,我们可以通过SimpleXlsxReportConfiguration设置导出选项,例如每页一个工作表等。 需要注意的是,JasperReport生成Excel报表的效果可能不如专业的Excel工具,例如Microsoft Excel或LibreOffice Calc。如果需要生成复杂的Excel报表,建议使用专业的Excel工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值