EasyPoi导入Excel带有图片并上传至阿里云OSS服务器

本文介绍如何使用EasyPOI库实现Excel文件的数据导入,并处理包含图片的单元格,包括图片的上传与转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.pom.xml依赖

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.3</version>
            <scope>compile</scope>
        </dependency>

2.创建实体类

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

/**
 * @Author 阿杰
 * @create 2021-06-29 9:46
 */
@Data
public class TestVo {

    @Excel(name = "序号")
    private Integer index;

    @Excel(name = "所属系统")
    private String system;

    @Excel(name = "功能板块")
    private String function;

    @Excel(name = "问题描述")
    private String introduction;

	/**
	* type=2 将excel列的内容设置图片 ,默认为文本
	*/
    @Excel(name = "备注/图片", type = 2)
    private String remarkImage;
}

3.导入处理

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import io.netty.handler.codec.base64.Base64;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/**
 * @Author 阿杰
 * @create 2021-06-29 9:44
 */
@RestController
@RequestMapping("/import")
public class TestController {

    @Autowired
    private RemoteOssService ossService;

    @RequestMapping("/excel")
    public List<TestVo> importFun(@RequestParam("file")MultipartFile file) throws Exception{
        ImportParams params = new ImportParams();
        params.setNeedSave(false);
        List<TestVo> list = ExcelImportUtil.importExcel(file.getInputStream(), TestVo.class, params);
        for (TestVo test : list) {
            //如果图片不为空
            if (StringUtils.isNotEmpty(test.getRemarkImage())) {
                //将图片转为MultipartFile对象用于上传至oss服务器
                File file1 = new File(test.getRemarkImage());
                FileInputStream fileInputStream = new FileInputStream(file1);
                R<String> upload = ossService.upload(new MockMultipartFile("file", file1.getName(), "text/plain", IOUtils.toByteArray(fileInputStream)));
                test.setRemarkImage(upload.getData());
            }
        }
        return list;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿࿆杰࿆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值