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;
@Data
public class TestVo {
@Excel(name = "序号")
private Integer index;
@Excel(name = "所属系统")
private String system;
@Excel(name = "功能板块")
private String function;
@Excel(name = "问题描述")
private String introduction;
@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;
@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())) {
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;
}
}