多余话不说,直接上代码
package org.springblade.common.utils;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
public class ExcelImageConverter {
static String path = "C:\\Users\\Administrator\\Downloads\\1\\";
public static void main(String[] args) throws Exception {
String filePath = "C:\\Users\\Administrator\\Downloads\\Image1.xlsx";
Map<String, PictureData> image = getImage(filePath);
printImg(image);
}
public static void printImg(Map<String, PictureData> picList) {
picList.forEach((key, pic) -> {
String ext = pic.suggestFileExtension();
byte[] data = pic.getData();
String filePath = path + key + "." + ext;
System.out.println(filePath);
try (FileOutputStream out = new FileOutputStream(filePath);){
out.write(data);
} catch (Exception e) {
e.printStackTrace();
}
});
}
public static Map<String, PictureData> getImage(String filePath) throws IOException {
if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx")) {
throw new RuntimeException("后缀必须是.xls或.xlsx");
}
Workbook wookbook;
Sheet sheet;
try(FileInputStream fis = new FileInputStream(filePath)) {
if (filePath.endsWith(".xls")) {
wookbook = new HSSFWorkbook(fis);
sheet = wookbook.getSheetAt(0);
return getImageXLS((HSSFSheet) sheet);
} else {
wookbook = new XSSFWorkbook(fis);
sheet = wookbook.getSheetAt(0);
return getImageXLSX((XSSFSheet) sheet);
}
}
}
public static Map<String, PictureData> getImageXLS(HSSFSheet sheet) {
AtomicInteger index = new AtomicInteger();
Map<String, PictureData> map = new HashMap<>();
List<HSSFShape> shapes = sheet.getDrawingPatriarch().getChildren();
shapes.forEach(v -> {
if (v instanceof HSSFPicture) {
HSSFPicture picture = (HSSFPicture) v;
HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
PictureData data = picture.getPictureData();
String key = cAnchor.getRow1() + "-" + cAnchor.getCol1() + "-" + index.getAndIncrement();
map.put(key, data);
}
});
return map;
}
public static Map<String, PictureData> getImageXLSX(XSSFSheet sheet) {
AtomicInteger index = new AtomicInteger();
Map<String, PictureData> map = new HashMap<>();
List<POIXMLDocumentPart> documentParts = sheet.getRelations();
documentParts.forEach(v -> {
if (v instanceof XSSFDrawing) {
XSSFDrawing drawing = (XSSFDrawing) v;
List<XSSFShape> shapes = drawing.getShapes();
shapes.forEach(s -> {
XSSFPicture picture = (XSSFPicture) s;
XSSFClientAnchor anchor = picture.getPreferredSize();
CTMarker marker = anchor.getFrom();
String key = marker.getRow() + "-" + marker.getCol() + "-" + index.getAndIncrement();
map.put(key, picture.getPictureData());
System.out.println(key + "-" + picture.getPictureData().getData().length/1024);
});
}
});
return map;
}
}