java EasyPoi导入/导出

Posted guxiaohai_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java EasyPoi导入/导出相关的知识,希望对你有一定的参考价值。

一:简介

  easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模
板导出,通过简单的注解和模板语言(熟悉的表达式语法),完成以前复杂的写法
详细介绍参考:
http://easypoi.mydoc.io/
https://www.cnblogs.com/vcmq/p/12149673.html

二:集成EasyPoi

  1. 引入pom.xml文件包
 <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>
  1. 编写entity实体类标题及属性
@Data
public class EasypoiEntity {

    @Excel(name = "主键",isColumnHidden = true)
    private int id;

    @Excel(name = "名称*")
    @NotBlank(message = "[名称]不能为空")
    private String name;

    @Excel(name = "性别*", replace = {"男_0", "女_1"})
    @Pattern(regexp = "[01]", message = "性别错误")
    private String genderStr;
}
  1. 实现导入excel功能
@PostMapping("/upload")
public void upload(@RequestParam("file") MultipartFile multipartFile) throws Exception {
    ImportParams params = new ImportParams();
    List<EasypoiEntity> result = ExcelImportUtil.importExcel(multipartFile.getInputStream(),
            EasypoiEntity.class, params);
    System.out.println(JSONUtil.toJsonStr(result));
}
  1. 实现导出excel功能
@PostMapping("/export")
public void export(HttpServletResponse response) {
     List<EasypoiEntity> list = new ArrayList<>();	//数据信息
     FileUtil.exportList(response,"名称.xls",EasypoiEntity.class,list);
 }
public class FileUtil {

    /**
     * @Author: ....
     * @Description:    将集合数据导出
     * @param fileName 导出的文件名称
     * @param beanType 导出时集合中的item的Class类型
     * @param list     导出用的集合数据
     * @Date: 2021-06-16 14:14
     */
    public static <T> void exportList(HttpServletResponse response, String fileName, Class<T> beanType, List<T> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        if (StringUtils.isBlank(fileName)) {
            fileName = DateTime.now().toString("yyyyMMddHHmmss") + "导出文件.xls";
        } else {
            if (!fileName.endsWith(".xls")) {
                fileName = fileName.trim() + ".xls";
            }
        }
        exportExcel(list, beanType, fileName, response);
    }

    private static void exportExcel(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response) {
        ExportParams exportParams = new ExportParams();
        exportParams.setCreateHeadRows(true);
        defaultExport(list, pojoClass, fileName, response, exportParams);
    }

    private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
            ExportParams exportParams) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        if (workbook != null) {
            ;
        }
        downLoadExcel(fileName, response, workbook);
    }

    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new ExcelImportException(e.getMessage());
        }
    }
}

以上是关于java EasyPoi导入/导出的主要内容,如果未能解决你的问题,请参考以下文章

【springboot+easypoi】一行代码搞定excel导入导出

java EasyPoi导入/导出

spring boot + easypoi两行代码excel导入导出

easypoi一行代码搞定excel导入导出

easypoi一行代码搞定excel导入导出

Easypoi excel导入时日期校验