直接看代碼吧,主要邏輯吧excel的圖片拿到 壓縮上傳獲取url文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-651606.html
// 將文件轉(zhuǎn)成XSSFWorkbook工作簿
XSSFWorkbook wb = new XSSFWorkbook(uploadFile);
// 獲取工作薄中第一個(gè)excel表格
XSSFSheet sheet = wb.getSheetAt(0);
// 核心:::獲取excel表格中所有圖片,處理圖片上傳到oss key:行號(hào)-列號(hào)
Map<String, List<String>> picturesMap = getPictures(sheet);
public Map<String, List<String>> getPictures(XSSFSheet xssfSheet) throws IOException {
Map<String, List<String>> maps = new LinkedHashMap<>();
List<XSSFShape> list = xssfSheet.getDrawingPatriarch().getShapes();
for (int i = 0; i < list.size(); i++) {
XSSFPicture picture = (XSSFPicture) list.get(i);
// 行號(hào)-列號(hào)
XSSFClientAnchor xssfClientAnchor = (XSSFClientAnchor) picture.getAnchor();
// 獲取圖片
XSSFPictureData pdata = picture.getPictureData();
byte[] data = pdata.getData();
InputStream inputStream = new ByteArrayInputStream(data);
byte[] scalePicLater = scalePics(inputStream,0.5,0.5);
String url = ossFactory.build().upload(new ByteArrayInputStream(scalePicLater), IdUtil.objectId() + ".jpg");
inputStream.close();
// 行號(hào)-列號(hào)
String key = xssfClientAnchor.getRow1() - 1 + "-" + xssfClientAnchor.getCol1();
if (maps.containsKey(key)) {
List<String> strUrl = maps.get(key);
strUrl.add(url);
maps.put(key, strUrl);
} else {
List<String> strUrl = new ArrayList<>();
strUrl.add(url);
maps.put(key, strUrl);
}
}
return maps;
}
public static byte[] scalePics(InputStream inputStream, double accuracy,double scale) throws IOException {
// 壓縮圖片并保存到臨時(shí)文件中
File tempFile = File.createTempFile("thumbnail", ".jpg");
Thumbnails.of(inputStream)
.scale(scale)
.outputQuality(accuracy)
.toFile(tempFile);
// 讀取臨時(shí)文件的字節(jié)流設(shè)置到輸出流中
InputStream tempInputStream = new FileInputStream(tempFile);
byte[] buffer = new byte[tempInputStream.available()];
tempInputStream.read(buffer);
tempInputStream.close();
// 刪除臨時(shí)文件
tempFile.delete();
// 下載到本地,
// BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Code\\upload\\1.jpg"));
// bos.write(buffer);
// bos.close();
return buffer;
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-651606.html
到了這里,關(guān)于java導(dǎo)入excel圖片處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!