在用excel导入数据的时候,如果一条数据,包含图片,甚至每条数据图片数量不固定。
图片放到excel里面直接拖进去就可以,再鼠标拉缩小到指定单元格内
package com.ydcloud.smart.parking.util;
import com.ydcloud.smart.parking.parking.model.ParkingPicturesVO;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.PictureData;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author:
* @date: 2022/10/9
* @Modified By:
*/
public class ReadExcelPictureUtil {
public static Map<String, List<PictureData>> readExcelPictureMap( MultipartFile uploadFile) throws IOException {
//获取所有图片
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(uploadFile.getInputStream());// 工作簿
HSSFSheet sheet = hssfWorkbook.getSheetAt(0);// 工作表
// int lastRowNum = sheet.getLastRowNum();// 获取最后一行序号,从零开始
Map<String, List<PictureData>> picMap = new HashMap<>();// 存储图片信息和坐标
// List<HSSFShape> list = Optional.ofNullable(sheet.getDrawingPatriarch().getChildren()).orElse(null);
List<HSSFShape> list = Optional.ofNullable(sheet).map(HSSFSheet::getDrawingPatriarch).map(HSSFPatriarch::getChildren).orElse(null);
if (list != null && list.size() > 0) {// 处理获取图片信息和坐标
list = list.stream().filter(item -> item instanceof HSSFPicture).collect(Collectors.toList());// 过滤出图片的数据
for (HSSFShape hssfShape : list) {
HSSFPicture hSSFPicture = (HSSFPicture) hssfShape;
HSSFClientAnchor hSSFClientAnchor = (HSSFClientAnchor) hSSFPicture.getAnchor();
PictureData pictureData = hSSFPicture.getPictureData();
String point = hSSFClientAnchor.getRow1() + "," + hSSFClientAnchor.getCol1();
// 如果存在这个坐标KEY表示相同单元格中的图片,直接集合添加该图片,不存在该坐标key直接创建添加
if (picMap.containsKey(point)) {
picMap.get(point).add(pictureData);
} else {
List<PictureData> arrayList = new ArrayList<PictureData>();
arrayList.add(pictureData);
picMap.put(point, arrayList);
}
}
}
return picMap;//图片map集合
}
//将上面所有的图片map集合循环,每次循环调用这个方法,进行每条数据的多张图片进行保存,返回图片的路径集合
public static List<String> savePicture(int dataRowNum,Map<String, List<PictureData>> picMap){//dataRowNum 是你真正开始读取的行数,如果第一行是表头,第二行才是真正数据,那就设为1
List<PictureData> pictureDataList = picMap.get(dataRowNum + "," + 31);//31 是因为图片位于32列
List<String> fileList = new ArrayList<>();
if (pictureDataList != null){
for (PictureData pictureData : pictureDataList) {
File picDirF = null;
Date curDate = new Date();
SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyyMMdd");
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// 获取jar包运行同级目录 eg:/opt/project/city/image/20221012/20221012133137286.jpg
String projectPath = System.getProperty("user.dir");
String jarUrl = projectPath.replace("\\","/") + "/image/";
// picloc是本地保存图片的位置
String picDir = jarUrl + ymdFormat.format(curDate);
File picDirGsPic = new File(jarUrl);
// 判断存图片的文件夹是否存在,不存在则创建
if (!picDirGsPic.exists()) {
picDirGsPic.mkdir();
}
picDirF = new File(picDir);
//存图片的文件夹按日期一天生成一个,这里判断当天的是否已存在,不存在则创建
if (!picDirF.exists()) {
picDirF.mkdir();
}
//按日期生成保存图片的文件名
String picPath = picDir + "/" + dateFormat.format(new Date()) + ".jpg";
FileOutputStream fout;
try {
fout = new FileOutputStream(picPath);
fout.write(pictureData.getData());
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
fileList.add(picPath);
}
}
return fileList;//返回图片路径集合
}
}