easypoi导出excel时导出图片

    /**
     * 将本地图片读取为字节流
     *
     * @param imagePath
     * @return
     * @throws IOException
     */
    public static byte[] readImageAsBytes(String imagePath) throws IOException {
        File file = new File(imagePath);
        try (InputStream inputStream = new FileInputStream(file)) {
            byte[] buffer = new byte[(int) file.length()];
            inputStream.read(buffer);
            return buffer;
        }
    }
 @Override
    public void exportXls(HttpServletResponse response, SeracherParam searchParamer) throws IOException {
        //拼接产地、等级、年份
        String leafInfo = searchParamer.getLeafInfo();
        if (StringUtils.isNotBlank(leafInfo)) {
            SerchConditionUtils.getSearch(leafInfo, searchParamer);
        }
        List<String> idList = new ArrayList<>();
        if (null != searchParamer.getSelections()) {
            idList = Arrays.asList(searchParamer.getSelections().split(","));
        }
        List<ExportVo> exportList = this.baseMapper.exportList(searchParamer, idList);
        exportList = exportList.stream()
                .collect(Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(ExportVo::getCheckNo))), ArrayList::new));
        for (ExportVo exportVo : exportList) {
            List<ExportTackRecordVo> exportInfo = itCheckRecordService.getExportInfo(exportVo.getId());
            for (ExportTackRecordVo exportTackRecordVo : exportInfo) {
                if (null != exportTackRecordVo.getPicture() || StringUtils.isNotEmpty(exportTackRecordVo.getPicture())) {
                    byte[] bytes = readImageAsBytes(upLoadPath + "/" + exportTackRecordVo.getPicture());
                    exportTackRecordVo.setImageBytes(bytes);
                }
            }
            exportVo.setExportTackRecordVoList(exportInfo);
        }
        ExportParamerUtils.createExportParamer(response, "抽检记录", ExportVo.class, exportList);
    }
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecg.common.aspect.annotation.Dict;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.poi.excel.annotation.ExcelCollection;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.List;

@Data
public class ExportVo {
    /**
     * 主键id
     */
    @TableId(type = IdType.ASSIGN_ID)
    @ApiModelProperty(value = "主键id")
    private java.lang.String id;
    private java.lang.String trcId;
    /**
     * 抽检编号
     */
    @Excel(name = "抽检编号", width = 15,needMerge = true)
    @ApiModelProperty(value = "抽检编号")
    private java.lang.String checkNo;
    /**
     * 抽检时间
     */
    @Excel(name = "抽检时间", width = 15,needMerge = true, format = "yyyy-MM-dd")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "抽检时间")
    private java.util.Date checkTime;
    /**
     * 存货编码
     */
    @Excel(name = "存货编码", width = 15,needMerge = true)
    @ApiModelProperty(value = "存货编码")
    private java.lang.String stockNo;
    /**
     * 产地
     */
    @Excel(name = "产地", width = 15,needMerge = true)
    @ApiModelProperty(value = "产地")
    private java.lang.String producer;

    /**
     * 年份
     */
    @Excel(name = "年份", width = 15,needMerge = true)
    @ApiModelProperty(value = "年份")
    private java.lang.String year;

    /**
     * 等级
     */
    @Excel(name = "等级", width = 15,needMerge = true)
    @ApiModelProperty(value = "等级")
    private java.lang.String grade;

    /**
     * 抽检类型 1入库 2 在库 3出库
     */
    @Dict(dicCode = "samplingType")
    @Excel(name = "抽检类型", width = 15,needMerge = true,dicCode = "samplingType")
    @ApiModelProperty(value = "抽检类型 1入库 2 在库 3出库")
    private java.lang.String type;
    /**
     * 来料类型
     */
    @Dict(dicCode = "materialType")
    @Excel(name = "来料类型", width = 15,needMerge = true,dicCode = "materialType")
    @ApiModelProperty(value = "来料类型")
    private java.lang.String inType;

    @ExcelCollection(name = "")
    private List<ExportTackRecordVo> exportTackRecordVoList;
    /**
     * 是否复检
     */
    @Dict(dicCode = "repeatFlag")
    @Excel(name = "是否复检", width = 15,needMerge = true,dicCode = "repeatFlag")
    @ApiModelProperty(value = "是否复检")
    private java.lang.String repeatFlag;
    /**
     * 复检时间
     */
    @Excel(name = "计划复检时间", width = 15,needMerge = true, format = "yyyy-MM-dd")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "计划复检时间")
    private java.util.Date repeatTime;
    /**
     * 复检状态
     */
    @Dict(dicCode = "repCheckStatus")
    @Excel(name = "复检状态", width = 15,needMerge = true,dicCode = "repCheckStatus")
    @ApiModelProperty(value = "复检状态")
    private java.lang.String repCheckStatus;
    /**
     * 抽检状态
     */
    @Dict(dicCode = "samplingState")
    @Excel(name = "抽检状态", width = 15,needMerge = true,dicCode = "samplingState")
    @ApiModelProperty(value = "抽检状态")
    private java.lang.String checkStatus;
    /**
     * 抽检人
     */
    @Excel(name = "抽检人", width = 15,needMerge = true)
    @ApiModelProperty(value = "抽检人")
    private java.lang.String checkBy;

    /**
     * 备注
     */
    @Excel(name = "备注", width = 15,needMerge = true)
    @ApiModelProperty(value = "备注")
    private java.lang.String remark;
}

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecg.common.aspect.annotation.Dict;
import org.jeecgframework.poi.excel.annotation.Excel;

@Data
public class ExportTackRecordVo {
    /**
     * 箱号
     */
    @Excel(name = "箱号", width = 15)
    @ApiModelProperty(value = "箱号")
    private java.lang.String boxNo;

//    /**
//     * 货位
//     */
//    @Excel(name = "货位", width = 15)
//    @ApiModelProperty(value = "货位")
//    private java.lang.String boxNo;

    /**
     * 总分
     */
    @Excel(name = "总分", width = 15)
    @ApiModelProperty(value = "总分")
    private java.lang.String totalScore;

    /**
     * 虫情
     */
    @Dict(dicCode = "insectSituation")
    @Excel(name = "虫情", width = 15,dicCode = "insectSituation")
    @ApiModelProperty(value = "虫情")
    private java.lang.String pest;
    /**
     * 霉变
     */
    @Dict(dicCode = "mildew")
    @Excel(name = "霉变", width = 15,dicCode = "mildew")
    @ApiModelProperty(value = "霉变")
    private java.lang.String mildew;
    /**
     * 水份%
     */
    @Dict(dicCode = "moistureContent")
    @Excel(name = "水份%", width = 15,dicCode = "moistureContent")
    @ApiModelProperty(value = "水份%")
    private java.lang.Double water;
    /**
     * 包装
     */
    @Dict(dicCode = "appearance")
    @Excel(name = "包装", width = 15,dicCode = "appearance")
    @ApiModelProperty(value = "包装")
    private java.lang.String appearance;
    /**
     * 包温
     */
    @Dict(dicCode = "packageTemp")
    @Excel(name = "包温", width = 15,dicCode = "packageTemp")
    @ApiModelProperty(value = "包温")
    private java.lang.String temperature;
    /**
     * 图片
     */
//    @Excel(name = "图片", type = 2, width = 20, height = 20, imageType = 1)
    private java.lang.String picture;
    @Excel(name = "图片", type = 2, width = 20, height = 20,imageType = 2)
    private byte[] imageBytes;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在使用 EasyPoi 导出 Excel 文件插入图片,您需要使用 `ExcelExportUtil` 类和 `ImageExportUtil` 类。 以下是一个示例代码: ```java public class ExportWithImageTest { public static void main(String[] args) throws IOException { // 准备数据 List<User> userList = new ArrayList<>(); userList.add(new User("John", 25, getImageBytes("john.png"))); userList.add(new User("Jane", 30, getImageBytes("jane.png"))); // 导出 Excel 文件 Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), User.class, userList); // 插入图片 for (int i = 0; i < userList.size(); i++) { byte[] imageBytes = userList.get(i).getImageBytes(); if (imageBytes != null) { ImageExportUtil.createImage(workbook, (byte[]) imageBytes, i, 2, 3, 5); } } // 保存文件 FileOutputStream outputStream = new FileOutputStream("users.xlsx"); workbook.write(outputStream); outputStream.close(); } private static byte[] getImageBytes(String filename) throws IOException { InputStream inputStream = ExportWithImageTest.class.getResourceAsStream(filename); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } return outputStream.toByteArray(); } } ``` 在上面的代码中,我们首先创建了一个包含用户姓名、年龄和头像图片字节数组的列表。然后,我们使用 `ExcelExportUtil` 导出 Excel 文件,并遍历用户列表以插入图片。 请注意,我们需要将图片插入到正确的单元格中,以便它们与相应的用户匹配。在上面的示例中,我们将图片插入到第二列的第四行和第三列的第四行。 另外,我们还需要使用 `ImageExportUtil` 类的 `createImage` 方法将图片插入到工作簿中。该方法接受一个工作簿对象、一个字节数组、一个行索引、一个列索引和四个单元格偏移量,以确定图片应该插入到哪个单元格中。 最后,我们将工作簿保存到文件中。在上面的示例中,我们将文件保存为 `users.xlsx`。 请注意,如上所述,该示例仅适用于在单元格中插入静态图片的情况。如果您需要在单元格中插入动态图像或其他更高级的功能,可能需要使用其他库或更复杂的技术实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值