java超级简单到爆的Excel导入导出(easypoi)

场景:

    在日常工作中,excel导入导出,是十分常见的,有两种主流的技术,一种是jxl,另一种是poi,而easypoi就是对poi进行了封装,使得导入导出变得更加的简单,阿里巴巴也有封装的工具名叫EasyExcel有兴趣的小伙伴可以研究一下,这里讲我比较喜欢的easypoi。

    easypoi不适用于那种花里胡哨的excel表格。

maven导包:

pom文件配置如下:


    
      cn.afterturn
      easypoi-base
      3.2.0
    
    
      cn.afterturn
      easypoi-web
      3.2.0
    
    
      cn.afterturn
      easypoi-annotation
      3.2.0
    

封装的easypoi工具类:

package com.meeno.trainsys.util.easypoi.utils;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;


/**
 * @description: easypoiUtils
 * @author: Wzq
 * @create: 2019-11-08 14:54
 */
public class ExcelUtiles {
    public static void exportExcel(List list, String title, String sheetName, Class pojoClass,
                                   String fileName, boolean isCreateHeader, HttpServletResponse response){
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(list, pojoClass, fileName, response, exportParams);
    }


    public static void exportExcel(List list, String title, String sheetName, Class pojoClass,String fileName,
                                   HttpServletResponse response){
        defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
    }


    public static void exportExcel(List> list, String fileName, HttpServletResponse response){
        defaultExport(list, fileName, response);
    }


    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 NormalException(e.getMessage());
        }
    }


    private static void defaultExport(List> list, String fileName, HttpServletResponse response) {
        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
        if (workbook != null);
        downLoadExcel(fileName, response, workbook);
    }


    public static  List importExcel(String filePath,Integer titleRows,Integer headerRows, Class pojoClass){
        if (StringUtils.isBlank(filePath)){
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List list = null;
        try {
            list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
        }catch (NoSuchElementException e){
            //throw new NormalException("模板不能为空");
        } catch (Exception e) {
            e.printStackTrace();
            //throw new NormalException(e.getMessage());
        } return list;
    }


    public static  List importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class pojoClass){
        if (file == null){ return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        }catch (NoSuchElementException e){
            // throw new NormalException("excel文件不能为空");
        } catch (Exception e) {
            //throw new NormalException(e.getMessage());
            System.out.println(e.getMessage());
        }
        return list;
    }
}


excel对应模板的java实体类:

package com.meeno.trainsys.util.easypoi.model;


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


/**
 * @description: 用户ExcelModel
 * @author: Wzq
 * @create: 2019-11-08 14:56
 */
@Data
public class UserExcelModel {


    @Excel(name = "序号",orderNum = "0")
    private String number;


    @Excel(name = "名称",orderNum = "1")
    private String name;


    @Excel(name = "电话",orderNum = "2")
    private String phone;


}


excel文件:

java超级简单到爆的Excel导入导出(easypoi)_第1张图片

导入excel:



List userExcelModels = ExcelUtiles.importExcel("E:\\test\\2.xlsx", 0, 1, UserExcelModel.class);


System.out.println(userExcelModels.toString());


内置导出工具方法,傻瓜式调用即可!

ExcelUtiles.exportExce(...);

技术支持:高岳峰,闫宇峰,施凯雷,杨珂

他的个人博客地址:http://120.78.93.197/

这是我的公众号 有最新的it咨询,和个人工作的记录:

这是我的个人微信遇到问题欢迎,提问:

最后加上高质量的淘宝店:如有质量问题随时滴滴我,童叟无欺!

java超级简单到爆的Excel导入导出(easypoi)_第2张图片

你可能感兴趣的:(java超级简单到爆的Excel导入导出(easypoi))