zoukankan      html  css  js  c++  java
  • easyPoi导入带图片的excel

    注意:导入的图片需为浮动图片

    官方API:http://easypoi.mydoc.io

    1.依赖

      <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>

    2.代码

    ImportExcel.java:
    public static List<GoodsHasImgModel> analysisExcelFileEasyPoi(MultipartFile file) throws Exception {
    ImportParams params = new ImportParams();
    params.setNeedSave(false);
       //解析后的图片字段存放的是图片的地址,需要在GoodsModel中配置图片存放路径
    List<GoodsModel> result = ExcelImportUtil.importExcel(file.getInputStream(), GoodsModel.class, params);
    for (GoodsModel goodsModel : result) {
    if (StringUtils.isNotEmpty(goodsModel.getGoodsPic())) {
            //将图片文件转为base64图片
    File file1 = new File(goodsModel.getGoodsPic());
    FileInputStream inputStream = new FileInputStream(file1);
    byte[] buffer = new byte[inputStream.available()];
    if (inputStream.read(buffer) == -1) {
    inputStream.close();
    }
    StringBuilder imageBase64 = new StringBuilder(Base64.getEncoder().encodeToString(buffer));
    goodsHasImgModel.setGoodsPic(new String(imageBase64));
    }
    }
    return result;
    }

    @ExcelTarget("goodsEntity")
    @Data
    @Component
    public class GoodsHasImgModel {

    @Excel(name = "商品名称")
    private String goodsName;
      
       //savePath配置的是解析excel后存放图片的路径
    @Excel(name = "商品照片", type = 2, width = 20, height = 10, imageType = 1, savePath = "/home/file/img")
    private String goodsPic;

       ...
    }

    删除临时图片
    String[] fileList = new File(savePath).list();
    for (String str : fileList) {
    File file1 = new File(savePath + "/" + str);
    if (file1.isFile()) {
    logger.info("清理图片缓存:" + str + (file1.delete() ? "成功" : "失败"));
    }
    }



  • 相关阅读:
    Mysql: 一个死锁场景的解决
    Mysql: 创建新的账号
    安全评测:BashDoor
    BeanFactory和FactoryBean的区别:
    linux 下将war解压到当前目录
    学习BoolanC++笔记_01(C++面向对象高级编程(上)第一周)
    学习BoolanC++笔记_00(预备)
    Linux (Centos) 上安装Nexus OSS 3 并搭建docker私有仓库
    linux(Centos7)搭建rpm仓库
    树莓派4b安装 manjaro linux
  • 原文地址:https://www.cnblogs.com/sanshao-ghf/p/14808634.html
Copyright © 2011-2022 走看看