文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

JAVA POI的excel中包含图片进行读取保存,单张图片,多张图片

2023-09-13 10:17

关注

JAVA POI的excel中包含图片进行读取保存,单张图片,多张图片

---------------------------------------------效果----------------------------------------------------------

单张图片
在这里插入图片描述

多张图片
在这里插入图片描述

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.stream.Collectors;import org.apache.poi.hssf.usermodel.HSSFClientAnchor;import org.apache.poi.hssf.usermodel.HSSFPicture;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFShape;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.PictureData;public class importExcelByImg {public static void main(String[] args) {// 1.一个单元格一个图片importExcelOne();// 2.一个单元格多个图片importExcelTwo();}public static void importExcelOne() {try {FileInputStream fileInputStream = new FileInputStream("D://1603169080802.xls");HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileInputStream);// 工作簿HSSFSheet sheet = hssfWorkbook.getSheetAt(0);// 工作表int lastRowNum = sheet.getLastRowNum();// 获取最后一行序号,从零开始Map<String, PictureData> picMap = new HashMap<>();// 存储图片信息和坐标List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();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();picMap.put(hSSFClientAnchor.getRow1() + "," + hSSFClientAnchor.getCol1(), pictureData);}}for (int i = 1; i <= lastRowNum; i++) {HSSFRow row = sheet.getRow(i);String name = row.getCell(0) == null ? "" : row.getCell(0).getStringCellValue();String sex = row.getCell(1) == null ? "" : row.getCell(1).getStringCellValue();// 获取图片数据PictureData pictureData = picMap.get(i + "," + 2);String path = "";if (pictureData != null) {String suggestFileExtension = pictureData.suggestFileExtension();// 图片格式path = "D://" + i + "-2." + suggestFileExtension;// 存储路径FileOutputStream out = new FileOutputStream(path);// 流写入out.write(pictureData.getData());out.close();}System.out.println("名称:" + name + ",    性别:" + sex + ",    图片存储路径:" + path);}} catch (IOException e) {e.printStackTrace();}}public static void importExcelTwo() {try {FileInputStream fileInputStream = new FileInputStream("D://1603169081675.xls");HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileInputStream);// 工作簿HSSFSheet sheet = hssfWorkbook.getSheetAt(0);// 工作表int lastRowNum = sheet.getLastRowNum();// 获取最后一行序号,从零开始Map<String, List<PictureData>> picMap = new HashMap<>();// 存储图片信息和坐标List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();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);}}}for (int i = 1; i <= lastRowNum; i++) {HSSFRow row = sheet.getRow(i);String name = row.getCell(0) == null ? "" : row.getCell(0).getStringCellValue();String sex = row.getCell(1) == null ? "" : row.getCell(1).getStringCellValue();// 获取图片数据List<PictureData> pictureDataList = picMap.get(i + "," + 2);String paths = "";if (pictureDataList != null)for (PictureData pictureData : pictureDataList) {String suggestFileExtension = pictureData.suggestFileExtension();// 图片格式String path = "D://" + i + "-2--" + pictureDataList.indexOf(pictureData) + "."+ suggestFileExtension;// 存储路径paths = paths + path;FileOutputStream out = new FileOutputStream(path);// 流写入out.write(pictureData.getData());out.close();}System.out.println("名称:" + name + ",    性别:" + sex + ",    图片存储路径:" + paths);}} catch (IOException e) {e.printStackTrace();}}}

来源地址:https://blog.csdn.net/qq_43409973/article/details/131556630

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯