cjl
2023-11-15 df67fe187fae9ce9f486ea2f14c95a14d843e4a1
fix:增加导出功能
6 files added
7 files modified
672 ■■■■■ changed files
screen-api/pom.xml 12 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/controller/AllocationController.java 98 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/pojo/ext/allocation/AllocationExcelExt.java 51 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/pojo/vo/allocation/AllocationListExcelVo.java 172 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/pojo/vo/file/FileAddressVo.java 32 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/AllocationService.java 4 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/FileTableService.java 3 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/AllocationServiceImpl.java 13 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/FileTableServiceImpl.java 19 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/utils/CustomImageModifyHandler.java 127 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/utils/MyStringImageConverterUtil.java 69 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/utils/MyUrlConverterUtil.java 70 ●●●●● patch | view | raw | blame | history
screen-api/src/main/resources/application-qa.yml 2 ●●● patch | view | raw | blame | history
screen-api/pom.xml
@@ -53,6 +53,18 @@
            <artifactId>fastjson</artifactId>
            <version>1.2.66</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.7</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
screen-api/src/main/java/com/moral/api/controller/AllocationController.java
@@ -1,49 +1,45 @@
package com.moral.api.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.AllocationLog;
import com.moral.api.pojo.enums.SysDictTypeEnum;
import com.moral.api.entity.ResponsibilityUnit;
import com.moral.api.pojo.ext.allocation.AllocationExcelExt;
import com.moral.api.pojo.ext.allocation.AllocationExt;
import com.moral.api.pojo.ext.allocation.AllocationPageExt;
import com.moral.api.pojo.query.allocation.*;
import com.moral.api.pojo.query.allocationextension.AllocationExtensionAddCond;
import com.moral.api.pojo.vo.allocation.AllocationListExcelVo;
import com.moral.api.pojo.vo.allocation.AllocationPageVo;
import com.moral.api.pojo.vo.allocation.AllocationVo;
import com.moral.constant.PageResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.moral.api.entity.ResponsibilityUnit;
import com.moral.api.pojo.vo.file.FileAddressVo;
import com.moral.api.service.AllocationService;
import com.moral.api.utils.EasyExcelUtils;
import com.moral.api.utils.NoModelWriteData;
import com.moral.api.utils.*;
import com.moral.constant.Constants;
import com.moral.constant.PageResult;
import com.moral.constant.ResultMessage;
import com.moral.util.DateUtils;
import com.moral.util.WebUtils;
import io.swagger.annotations.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
@Api(tags = {"立行立改"})
@@ -51,6 +47,8 @@
@RequestMapping("allocation")
public class AllocationController {
    @Value("${file.path}")
    private String basePath;
    @Autowired
    private AllocationService allocationService;
@@ -220,4 +218,48 @@
    }
    @GetMapping("listExcel")
    @ApiOperation("批量导出")
    public void listExcel(@RequestParam @ApiParam(value = "id",name = "主键id") List<Integer> id,HttpServletResponse response){
        List<AllocationExcelExt> extList = allocationService.listExcel(id);
        try {
            genImageExcel(extList,response);
        }catch (Exception e){
        }
    }
    private final String PARHTINAGE = "https://qx.7drlb.com/api/file/preview/";
    private void genImageExcel(List<AllocationExcelExt> list, HttpServletResponse response) throws IOException {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<AllocationListExcelVo> demoDataList = new ArrayList<>();
        for(int i = 0;i<list.size();i++){
            AllocationListExcelVo item = AllocationListExcelVo.convert(list.get(i));
            List<String> urls = new ArrayList<>();
            for(FileAddressVo z : list.get(i).getFileBaseList()){
                urls.add(handleFileRealPath(z.getFileAddress()));
            }
            item.setWriteCellDataFile(urls);
            demoDataList.add(item);
        }
        try {
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''"
                    + URLEncoder.encode("交办单导出", "utf-8") + ".xlsx");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        EasyExcel.write(response.getOutputStream(), AllocationListExcelVo.class)
                .registerWriteHandler(new CustomImageModifyHandler()).sheet("交办单").doWrite(demoDataList);
        }catch (Exception e){
        }
    }
    private String handleFileRealPath(String path) {
        return basePath.replaceAll(StringUtils.BACKSLASH.concat(StringUtils.BACKSLASH), StringUtils.SLASH).concat(StringUtils.SLASH).concat(path);
    }
}
screen-api/src/main/java/com/moral/api/pojo/ext/allocation/AllocationExcelExt.java
New file
@@ -0,0 +1,51 @@
package com.moral.api.pojo.ext.allocation;
import com.baomidou.mybatisplus.annotation.TableField;
import com.moral.api.entity.Allocation;
import com.moral.api.pojo.vo.approvetable.ApproveTableListVo;
import com.moral.api.pojo.vo.file.FileAddressVo;
import com.moral.api.pojo.vo.file.FileVo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.util.List;
/**
 * <p>
 * 交办单
 * </p>
 * deyt template generate
 * @author JI
 * @since 2023-09-25
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class AllocationExcelExt extends Allocation {
    @ApiModelProperty(value = "基础附件")
    private List<FileAddressVo> fileBaseList;
 /*   @ApiModelProperty(value = "整改附件")
    private List<FileVo> fileChangeList;
    @ApiModelProperty(value = "审批附件")
    private List<FileVo> fileApproveList;
    @ApiModelProperty(value = "流程状态")
    private List<ApproveTableListVo> approveList;*/
    //主体单位名字
    @TableField(exist = false)
    private String unitName;
    //上报单位名字
    @TableField(exist = false)
    private String escalationUnitName;
    @TableField(exist = false)
    //污染类型名字
    private String polluteTypeName;
}
screen-api/src/main/java/com/moral/api/pojo/vo/allocation/AllocationListExcelVo.java
New file
@@ -0,0 +1,172 @@
package com.moral.api.pojo.vo.allocation;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.moral.api.pojo.ext.allocation.AllocationExcelExt;
import com.moral.api.pojo.vo.approvetable.ApproveTableListVo;
import com.moral.api.pojo.vo.file.FileAddressVo;
import com.moral.api.pojo.vo.file.FileVo;
import com.moral.api.utils.BeanConverts;
import com.moral.api.utils.MyStringImageConverterUtil;
import com.moral.api.utils.MyUrlConverterUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.net.URL;
import java.util.Date;
import java.util.List;
/**
 * <p>
 * 交办单
 * </p>
 * deyt template generate
 * @author JI
 * @since 2023-09-25
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="AllocationListExcelVo - 详情VO对象", description="AllocationListExcelVo - 详情VO对象")
public class AllocationListExcelVo implements Serializable {
    @ApiModelProperty(value = "交办单号")
    @ExcelProperty(value = "交办单号")
    private String allocationNum;
    @ApiModelProperty(value = "上报时间")
    @JsonFormat(pattern="yyyy-MM-dd")
    @ExcelProperty(value = "上报时间")
    private Date escalationTime;
    @ApiModelProperty(value = "污染位置")
    @ExcelProperty(value = "污染位置")
    private String pollutePosition;
  /*  @ApiModelProperty(value = "基础附件")
    private List<FileAddressVo> fileBaseList;*/
    @ApiModelProperty(value = "基础附件")
    @ExcelProperty(value = "整改前图片",converter = MyStringImageConverterUtil.class)
    private List<String> writeCellDataFile;
    @ApiModelProperty(value = "上报人")
    @ExcelProperty(value = "上报人")
    private String escalationName;
   /* @ApiModelProperty(value = "责任单位id")
    private Integer unitId;
    @ApiModelProperty(value = "污染分类id")
    private Integer polluteType;
    @ApiModelProperty(value = "整改类型id")
    private Integer changeType;
    @ApiModelProperty(value = "期限天数")
    private Integer changeDay;
    @ApiModelProperty(value = "上报单位id")
    private Integer escalationUnitId;
    @ApiModelProperty(value = "上报人")
    private String escalationName;
    @ApiModelProperty(value = "排查方式id")
    private Integer investigationType;
    @ApiModelProperty(value = "问题描述")
    private String problemDescribe;
    @ApiModelProperty(value = "是否整改")
    private Integer isChange;
    @ApiModelProperty(value = "整改反馈")
    private String changeDescribe;
    @ApiModelProperty(value = "整改人性名")
    private String changeName;
    @ApiModelProperty(value = "整改时间")
    @JsonFormat(pattern="yyyy-MM-dd")
    private Date changeTime;
    @ApiModelProperty(value = "考核分值")
    private Integer checkScore;
    @ApiModelProperty(value = "考核理由")
    private String checkDescribe;
    @ApiModelProperty(value = "考核人")
    private String checkName;
    @ApiModelProperty(value = "考核时间")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm", timezone = "GMT+8")
    private Date checkTime;
    @ApiModelProperty(value = "状态")
    private Integer state;
    @ApiModelProperty(value = "是否作废")
    private Integer isInvalid;
    @ApiModelProperty(value = "作废理由")
    private String invalidReason;
    @ApiModelProperty(value = "创建人id")
    private Integer createId;
    @ApiModelProperty(value = "创建人姓名")
    private String createName;
    @ApiModelProperty(value = "创建时间")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm", timezone = "GMT+8")
    private Date createTime;
    @ApiModelProperty(value = "更新人id")
    private Integer updateId;
    @ApiModelProperty(value = "更新人姓名")
    private String updateName;
    @ApiModelProperty(value = "更新时间")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm", timezone = "GMT+8")
    private Date updateTime;
    @ApiModelProperty(value = "整改附件")
    private List<FileVo> fileChangeList;
    @ApiModelProperty(value = "审批附件")
    private List<FileVo> fileApproveList;
    @ApiModelProperty(value = "流程状态")
    private List<ApproveTableListVo> approveList;
    @TableField(exist = false)
    private String time;
    @TableField(exist = false)
    private String unitName;
    @TableField(exist = false)
    private String escalationUnitName;
    @TableField(exist = false)
    private String polluteTypeName;*/
    public static AllocationListExcelVo convert(AllocationExcelExt allocationExt) {
        AllocationListExcelVo allocationVo = BeanConverts.convert(allocationExt, AllocationListExcelVo.class);
        return allocationVo;
    }
}
screen-api/src/main/java/com/moral/api/pojo/vo/file/FileAddressVo.java
New file
@@ -0,0 +1,32 @@
package com.moral.api.pojo.vo.file;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
 * @ClassName FileVo
 * @Description TODO
 * @Author @cjl
 * @Date 2023-09-21 14:51
 * @Version 1.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(description="FileAddressVo - 文件VO对象")
public class FileAddressVo {
    @ApiModelProperty(value = "ID")
    private Integer fileId;
    @ApiModelProperty(value = "文件名称")
    private String fileName;
    @ApiModelProperty(value = "文件路径")
    private String fileAddress;
    @ApiModelProperty(value = "类型1图片")
    private Integer fileType;
}
screen-api/src/main/java/com/moral/api/service/AllocationService.java
@@ -9,6 +9,7 @@
import com.moral.api.entity.Allocation;
import com.moral.api.entity.AllocationLog;
import com.moral.api.entity.ResponsibilityUnit;
import com.moral.api.pojo.ext.allocation.AllocationExcelExt;
import com.moral.api.pojo.ext.allocation.AllocationExt;
import com.moral.api.pojo.ext.allocation.AllocationListExt;
import com.moral.api.pojo.ext.allocation.AllocationPageExt;
@@ -160,4 +161,7 @@
     * @return
     */
    List<Integer> getUnitAuthority(String code);
    List<AllocationExcelExt> listExcel(List<Integer> id);
}
screen-api/src/main/java/com/moral/api/service/FileTableService.java
@@ -2,6 +2,7 @@
import com.moral.api.entity.FileTable;
import com.baomidou.mybatisplus.extension.service.IService;
import com.moral.api.pojo.vo.file.FileAddressVo;
import com.moral.api.pojo.vo.file.FileVo;
import org.springframework.web.multipart.MultipartFile;
@@ -30,4 +31,6 @@
    void upDateResult(List<FileVo> list,int relationId,int fileModule);
    List<FileVo> list(int relationId,int fileModule);
    List<FileAddressVo> list(int relationId,int fileModule,int fileType);
}
screen-api/src/main/java/com/moral/api/service/impl/AllocationServiceImpl.java
@@ -19,6 +19,7 @@
import com.moral.api.pojo.bean.BaseInvalidEntity;
import com.moral.api.pojo.dto.allocation.AllocationUnitViewDto;
import com.moral.api.pojo.enums.*;
import com.moral.api.pojo.ext.allocation.AllocationExcelExt;
import com.moral.api.pojo.ext.allocation.AllocationExt;
import com.moral.api.pojo.ext.allocation.AllocationListExt;
import com.moral.api.pojo.ext.allocation.AllocationPageExt;
@@ -31,6 +32,7 @@
import com.moral.api.pojo.vo.app.AppAuthority;
import com.moral.api.pojo.vo.user.QxUser;
import com.moral.api.service.*;
import com.moral.api.utils.BeanConverts;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.util.DateUtils;
@@ -994,4 +996,15 @@
          return   responsibilityUnit;
    }
    @Override
    public List<AllocationExcelExt> listExcel(List<Integer> id) {
        List<AllocationExcelExt> list = new ArrayList<>();
        for(Integer i : id){
            AllocationExt allocationExt = oneAllocation(i);
            AllocationExcelExt allocationExcelExt = BeanConverts.convert(allocationExt,AllocationExcelExt.class);
            allocationExcelExt.setFileBaseList(fileTableService.list(i,FileTableEnum.ALLOCATION_FOUNDATION.value,FileType.PICTURE.getValue()));
            list.add(allocationExcelExt);
        }
        return list;
    }
}
screen-api/src/main/java/com/moral/api/service/impl/FileTableServiceImpl.java
@@ -6,6 +6,7 @@
import com.moral.api.mapper.FileTableMapper;
import com.moral.api.pojo.enums.FileType;
import com.moral.api.pojo.enums.YesOrNo;
import com.moral.api.pojo.vo.file.FileAddressVo;
import com.moral.api.pojo.vo.file.FileVo;
import com.moral.api.pojo.vo.user.QxUser;
import com.moral.api.service.FileTableService;
@@ -236,4 +237,22 @@
        });
        return list;
    }
    @Override
    public List<FileAddressVo> list(int relationId, int fileModule, int fileType) {
        List<FileAddressVo> list = new ArrayList<>();
        List<FileTable> existsList = this.lambdaQuery().eq(FileTable::getRelationId, relationId)
                .eq(FileTable::getFileModule, fileModule)
                .eq(Objects.nonNull(fileType),FileTable::getFileType,fileType)
                .eq(FileTable::getIsDel, YesOrNo.NO.value).orderByAsc(FileTable::getCreateTime).list();
        existsList.forEach(it->{
            FileAddressVo fileVo = new FileAddressVo();
            fileVo.setFileId(it.getFileId());
            fileVo.setFileName(it.getFileName());
            fileVo.setFileType(it.getFileType());
            fileVo.setFileAddress(it.getFileAddress());
            list.add(fileVo);
        });
        return list;
    }
}
screen-api/src/main/java/com/moral/api/utils/CustomImageModifyHandler.java
New file
@@ -0,0 +1,127 @@
package com.moral.api.utils;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.Units;
import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName CustomImageModifyHandler
 * @Description TODO
 * @Author @cjl
 * @Date 2023-11-15 10:56
 * @Version 1.0
 */
public class CustomImageModifyHandler implements CellWriteHandler {
    private List<String> repeats = new ArrayList<>();
    // 单元格的图片最大张数(每列的单元格图片张数不确定,单元格宽度需按照张数最多的长度来设置)
    private Integer maxDataSize = 0;
    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
    }
    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    }
    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        //  在 数据转换成功后 不是头就把类型设置成空
        if (isHead) {
            return;
        }
        //将要插入图片的单元格的type设置为空,下面再填充图片
        if(cellData.getImageValue()!=null||cellData.getData() instanceof ArrayList){
            cellData.setType(CellDataTypeEnum.EMPTY);
        }
    }
    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        //  在 单元格写入完毕后 ,自己填充图片
        if (isHead || CollectionUtils.isEmpty(cellDataList)) {
            return;
        }
        Boolean listFlag = false;
        ArrayList data = null;
        Sheet sheet = cell.getSheet();
        // 此处为ListUrlConverterUtil的返回值
        if (cellDataList.get(0).getData() instanceof ArrayList){
            data = (ArrayList) cellDataList.get(0).getData();
            if (CollectionUtils.isEmpty(data)) {
                return;
            }
            if (data.get(0) instanceof CellData){
                CellData cellData = (CellData) data.get(0);
                if (cellData.getImageValue() == null){
                    return;
                }else {
                    listFlag = true;
                }
            }
        }
        if (!listFlag && cellDataList.get(0).getImageValue() == null){
            return;
        }
        String key = cell.getRowIndex() + "_" + cell.getColumnIndex();
        if (repeats.contains(key)){
            return;
        }
        repeats.add(key);
        if (data.size() > maxDataSize) {
            maxDataSize = data.size();
        }
        //60px的行高大约是900,60px列宽大概是248*8,根据需要调整
        sheet.getRow(cell.getRowIndex()).setHeight((short)900);
        sheet.setColumnWidth(cell.getColumnIndex(), (int) (listFlag?21.8*256*maxDataSize:22.8*256));
        if (listFlag){
            for (int i = 0; i < data.size(); i++) {
                CellData cellData= (CellData) data.get(i);
                if(cellData.getImageValue()==null){
                    continue;
                }
                this.insertImage(sheet,cell,cellData.getImageValue(),i);
            }
        }else {
            // cellDataList 是list的原因是 填充的情况下 可能会多个写到一个单元格 但是如果普通写入 一定只有一个
            this.insertImage(sheet,cell,cellDataList.get(0).getImageValue(),0);
        }
    }
    private void insertImage(Sheet sheet,Cell cell,byte[] pictureData,int i){
        int picWidth = Units.pixelToEMU(175);
        int index = sheet.getWorkbook().addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG);
        Drawing drawing = sheet.getDrawingPatriarch();
        if (drawing == null) {
            drawing = sheet.createDrawingPatriarch();
        }
        CreationHelper helper = sheet.getWorkbook().getCreationHelper();
        ClientAnchor anchor = helper.createClientAnchor();
        // 设置图片坐标
        anchor.setDx1(picWidth*i);
        anchor.setDx2(picWidth+picWidth*i);
        anchor.setDy1(0);
        anchor.setDy2(0);
        //设置图片位置
        anchor.setCol1(cell.getColumnIndex());
        anchor.setCol2(cell.getColumnIndex());
        anchor.setRow1(cell.getRowIndex());
        anchor.setRow2(cell.getRowIndex() + 1);
        // 设置图片可以随着单元格移动
        anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
        drawing.createPicture(anchor, index);
    }
}
screen-api/src/main/java/com/moral/api/utils/MyStringImageConverterUtil.java
New file
@@ -0,0 +1,69 @@
package com.moral.api.utils;
import cn.hutool.core.convert.Convert;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.IoUtils;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName MyStringImageConverterUtil
 * @Description TODO
 * @Author @cjl
 * @Date 2023-11-15 14:08
 * @Version 1.0
 */
public class MyStringImageConverterUtil implements Converter<List<String>> {
    @Override
    public Class supportJavaTypeKey() {
        return List.class;
    }
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.IMAGE;
    }
    @Override
    public List<String> convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String stringValue = cellData.getStringValue();
        //用json转换工具将excel单元格中数据转换为java List<String>对象
        List<String> list = Convert.toList(String.class,stringValue);
        return list;
    }
    @Override
    public CellData convertToExcelData(List<String> stringUrl, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 这里进行对数据实体类URL集合处理
        List<CellData> data = new ArrayList<>();
        // for 循环一次读取
        for (String url : stringUrl) {
            FileInputStream inputStream = null;
            try {
                inputStream = new FileInputStream(url);
                byte[] bytes = IoUtils.toByteArray(inputStream);
                data.add(new CellData(bytes));
            } catch (Exception e) {
                //图片异常展示的图片
                //data.add(new CellData(IoUtils.toByteArray(new FileInputStream("D:\\easyexcel\\err.png"))));
                continue;
            } finally {
                if (inputStream != null){
                    inputStream.close();
                }
            }
        }
        // 这种方式并不能返回一个List,所以只好通过CellData cellData = new CellData(data);将这个list对象塞到返回值CellData对象的data属性中;
        CellData cellData = new CellData(data);
        cellData.setType(CellDataTypeEnum.IMAGE);
        return cellData;
    }
}
screen-api/src/main/java/com/moral/api/utils/MyUrlConverterUtil.java
New file
@@ -0,0 +1,70 @@
package com.moral.api.utils;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.IoUtils;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName MyUrlConverterUtil
 * @Description TODO
 * @Author @cjl
 * @Date 2023-11-15 11:02
 * @Version 1.0
 */
public class MyUrlConverterUtil implements Converter<List<URL>> {
    @Override
    public Class supportJavaTypeKey() {
        return List.class;
    }
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        /**
         *这里记得枚举类型为IMAGE
         */
        return CellDataTypeEnum.IMAGE;
    }
    @Override
    public List convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return null;
    }
    @Override
    public CellData convertToExcelData(List<URL> value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 这里进行对数据实体类URL集合处理
        List<CellData> data = new ArrayList<>();
        // for 循环一次读取
        for (URL url : value) {
            InputStream inputStream = null;
            try {
                inputStream = url.openStream();
                byte[] bytes = IoUtils.toByteArray(inputStream);
                data.add(new CellData(bytes));
            } catch (Exception e) {
                //图片异常展示的图片
                data.add(new CellData(IoUtils.toByteArray(new FileInputStream("D:\\easyexcel\\err.png"))));
                continue;
            } finally {
                if (inputStream != null){
                    inputStream.close();
                }
            }
        }
        // 这种方式并不能返回一个List,所以只好通过CellData cellData = new CellData(data);将这个list对象塞到返回值CellData对象的data属性中;
        CellData cellData = new CellData(data);
        cellData.setType(CellDataTypeEnum.IMAGE);
        return cellData;
    }
}
screen-api/src/main/resources/application-qa.yml
@@ -141,4 +141,4 @@
    foh3wi2ooghiCh5
file:
  path: /data/upload/
  path: E:/upload