参考文章地址:https://blog.csdn.net/weixin_41843053/article/details/81740521
参考文章作者:https://blog.csdn.net/weixin_41843053
@赏花同学
使用的工具的hutool
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.2.3</version>
</dependency>
Excel操作包是apache的poi
<dependency>
<groupId>;.org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
<type>pom</type>
</dependency>
我设置了一个bean用来查看结果,不需要的可以不用
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class ImageBean {
private int row;
private int col;
private String code1;
private String code2;
private String url;
}
解析Excel
我因为每个sheet中需要的字段所处的列不同,所以每个sheet是单独操作的,如果需要的内容所在位置一样,可以直接循环操作
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 解析Excel获取图片,并获取图片在表中的坐标
*/
public class ImageTest {
public static void main(String[] args) throws InvalidFormatException, IOException {
//需要解析的Excel文件
File file = new File("filePath");
//图片文件的下载路径
String dest = "downloadPath";
int sheetIndex = 0;
ExcelReader reader = ExcelUtil.getReader(file, sheetIndex);
List<ImageBean> list = new ArrayList<>();
Workbook workbook = reader.getWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(sheetIndex);
List<String> fileNameList = new ArrayList<>();
List<List<Object>> values = reader.read();
//对表格进行操作
//03版本
//for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
//07版本
for (XSSFShape shape : sheet.getDrawingPatriarch().getShapes()) {
XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
if (shape instanceof XSSFPicture) {
XSSFPicture pic = (XSSFPicture) shape;
//获取行编号
int row = anchor.getRow2();
//获取列编号
int col = anchor.getCol2();
List<Object> os = values.get(row);
//这是我在业务中需要获取的单元格内容,不需要的话可以忽略
String code1 = os.get(0).toString();
String code2 = os.get(1).toString();
Long id = IdWorker.getId();
//图片名称是随机定义的,我的业务中图片类型因为只涉及到静态图片,所以我写死了
String fileName = RandomUtil.randomString(20)+".png";
if(fileNameList.contains(fileName)){
System.out.println(fileName+"已存在");
}
String url = dest+RandomUtil.randomString(10)+fileName;
//03版本
//int pictureIndex = pic.getPictureIndex()-1;
//HSSFPictureData picData = pictures.get(pictureIndex);
//07版本
XSSFPictureData pictureData = pic.getPictureData();
FileUtil.writeBytes(pictureData.getData(), url);
//如果不需要查看行列及图片,可以忽略下面的内容了
ImageBean bean = new ImageBean();
bean.setRow(row).setCol(col).setUrl(url).setCode1(code1).setCode2(code2);
list.add(bean);
}
}
list.forEach(one -> System.out.println(one));
}
}
本人需要解析的Excel文件是.xlsx格式的
xlsx与xls格式部分方法存在不同,07版本正常,03版本未测试