栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java使用easypoi快速导入导出的实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java使用easypoi快速导入导出的实现

简介

easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员就可以方便的写出Excel导入,导出,通过简单的注解和模板语言(熟悉的表达式语法),完成以前复杂的写法。

集成

pom 中引入依赖即可

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

整合工具类 EasyPoiUtil

package cn.common.util;

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 cn.common.exception.ZXException;
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;


public class EasyPoiUtil {
    
  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);
    ExcelExportUtil.closeExportBigExcel();
    if (workbook != null);
    downLoadExcel(fileName, response, workbook);
  }

  
  private static void defaultExport(List> list, String fileName, HttpServletResponse response) {
    Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
    ExcelExportUtil.closeExportBigExcel();
    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 ZXException(e.getMessage());
    }
  }

  
  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 ZXException("模板不能为空");
    } catch (Exception e) {
      e.printStackTrace();
      throw new ZXException(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 ZXException("excel文件不能为空");
    } catch (Exception e) {
      throw new ZXException(e.getMessage());
    }
    return list;
  }

}

使用示例

实体类

public class BlackListExport {
  @Excel(name = "客户姓名", width = 15, orderNum = "2")
  private String name;
  @Excel(name = "备注", width = 10, orderNum = "1")
  private String remark;
  @Excel(name = "手机号", width = 15, orderNum = "0")
  private String phone;

  public String getName() {
    return name;
  }

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

  public String getRemark() {
    return remark;
  }

  public void setRemark(String remark) {
    this.remark = remark;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public BlackListExport() {
  }

  public BlackListExport(String name, String remark, String phone) {
    this.name = name;
    this.remark = remark;
    this.phone = phone;
  }
}

接口

  @ApiOperation(value = "easyPoiUtil 导出测试")
  @GetMapping(value = "/poi/export1")
  public void export1(HttpServletResponse response){
    List list=new ArrayList<>();
    for(int i=0;i<10000;i++){
      list.add(new BlackListExport(i+"",i+"",i+""));
    }
    EasyPoiUtil.exportExcel(list,"zx","huangy",BlackListExport.class,"zx.xls",response);
  }

  
  @ApiOperation(value = "多sheet 导出测试")
  @GetMapping(value = "/poi/export2")
  public void export2(HttpServletResponse response){
    // 查询数据,此处省略
    List list = new ArrayList<>();
    list.add(new BlackListExport("姓名1","备注1","手机1")) ;
    list.add(new BlackListExport("姓名2","备注2","手机2")) ;
    list.add(new BlackListExport("姓名3","备注3","手机3")) ;
    List list2 = new ArrayList<>();
    list2.add(new BlackListExport("姓名-1","备注-1","手机-1")) ;
    list2.add(new BlackListExport("姓名-2","备注-2","手机-2")) ;
    list2.add(new BlackListExport("姓名-3","备注-3","手机-3")) ;
    List> sheetsList = new ArrayList<>();
    for(int i=1;i<=4;i++){
    // 设置导出配置
    // 创建参数对象(用来设定excel得sheet得内容等信息)
    ExportParams params = new ExportParams() ;
    // 设置sheet得名称
    params.setSheetName("表格"+i);

    //创建sheet使用的map
    Map dataMap = new HashMap<>();
    // title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName
    dataMap.put("title",params) ;
    // 模版导出对应得实体类型
    dataMap.put("entity",BlackListExport.class) ;
    // sheet中要填充得数据
    if(i%2==0){
      dataMap.put("data",list) ;
    }else {
      dataMap.put("data",list2) ;
    }

    sheetsList.add(dataMap);
    }
    EasyPoiUtil.exportExcel(sheetsList,"hy.xls",response);
  }

到此这篇关于Java使用easypoi快速导入导出的实现的文章就介绍到这了,更多相关Java easypoi导入导出内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/138705.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号