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

java:easypoi导出Excel

程序员文章站 2022-06-24 23:49:43
...

使用

  • 1.easypoi 父包
  • 2.easypoi-annotation 基础注解包,作用与实体对象上,拆分后方便maven多工程的依赖管理
  • 3.easypoi-base 导入导出的工具包,可以完成Excel导出,导入,Word的导出,Excel的导出功能
  • 4.easypoi-web 耦合了spring-mvc 基于AbstractView,极大的简化spring-mvc下的导出功能

如果不使用spring mvc的便捷福利,直接引入easypoi-base jar包就可以了,easypoi-annotation jar包

如果使用maven,请使用如下坐标

        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>

实现 

package com.xzc.model;

import cn.afterturn.easypoi.excel.annotation.Excel;

import java.util.Date;

public class Person {

    @Excel(name = "姓名", orderNum = "0")
    private String name;

    @Excel(name = "性别", replace = {"男_1", "女_2"}, orderNum = "1")
    private String sex;

    public Person(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}
/**
 * 注解导出
 *
 * @param response
 */
@RequestMapping("/export")
public void export(HttpServletResponse response) {
    //模拟从数据库获取需要导出的数据
    List<Person> personList = new ArrayList<>();
    Person person1 = new Person("路飞", "1");
    Person person2 = new Person("娜美", "2");
    personList.add(person1);
    personList.add(person2);
    //导出操作
    FileUtil.exportExcel(personList, "花名册", "草帽一伙", Person.class, "海贼王.xls", response);
}

/**
 * map导出
 *
 * @param response
 */
@RequestMapping("/exportm")
public void exportm(HttpServletResponse response) {
    //标题
    List<ExcelExportEntity> entityList = new ArrayList<>();
    //内容
    List<Map<String, Object>> dataResult = new ArrayList<>();
    entityList.add(new ExcelExportEntity("表头苹果", "apple", 15));
    entityList.add(new ExcelExportEntity("表头香蕉", "banana", 25));
    for (int i = 0; i < 10; i++) {
        Map<String, Object> map = new HashMap<>();
        map.put("apple", "苹果" + i);
        map.put("banana", "香蕉" + i);
        dataResult.add(map);
    }
    Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("测试", "测试"), entityList,
            dataResult);
    FileUtil.downLoadExcel("shuiguo.xls", response, workbook);

}

/**
 * 模板导出
 *
 * @param response
 */
@RequestMapping("/printTest")
public void printTest(HttpServletResponse response) {
    TemplateExportParams params = new TemplateExportParams("excelTemp/成绩单模板.xls");
    Map<String, Object> map = new HashMap<>();
    map.put("putinTime", "2014-12-25 14:30:00");
    map.put("paperName", "java");
    map.put("userId", "java");
    map.put("score", 55);
    map.put("note", "xioaxing");
    List<Map<String, String>> listMap = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        Map<String, String> lm = new HashMap<>();
        lm.put("index", i + 1 + "");
        lm.put("questionName", "fasfafaffafafafa");
        lm.put("answerA", "A001");
        lm.put("answerB", "A001");
        lm.put("answerC", "A001");
        lm.put("answerD", "A001");
        lm.put("myAnswer", "A");
        lm.put("sign", "V");
        listMap.add(lm);
    }
    map.put("maplist", listMap);
    Workbook workbook = ExcelExportUtil.exportExcel(params, map);
    FileUtil.downLoadExcel("成绩单.xls", response, workbook);
}

 演示

java:easypoi导出Excel

java:easypoi导出Excel


参考链接:

码云源码

官方文档