当前位置:   article > 正文

java 获取解析excel中的图片_java 读取excel图片

java 读取excel图片

以xlsx2007的excel为例

//获取整个文档
XSSFWorkbook wb;
// 页
XSSFSheet sheet;//表
// 行
XSSFRow row;//行
// 打开文件
try {
    wb = new XSSFWorkbook(contactFile.get(i).getInputStream());
} catch (Exception e) {
    e.printStackTrace();
    wb = new XSSFWorkbook();
}
//获取第1页
sheet = wb.getSheetAt(0);
// 得到总行数
int rowNum2 = sheet.getLastRowNum();

String uploadpath = "D://";//需要将图片保存的地址
//获取excel中的图片
Map<String, XSSFPictureData> pictureDataMap = getPictures(sheet);
 String savePath = "";
 for (int k = 1; k <= rowNum2; k++) {
     String mapKey = k + "-" + 12;//指定行和列
     if(pictureDataMap.get(mapKey)!=null){
         XSSFPictureData xssfPictureData = pictureDataMap.get(mapKey);
         byte[] data = xssfPictureData.getData();
         long time = System.currentTimeMillis();
		 
		 //得到保存的file
         File file = bytesToFile(data, uploadpath, String.valueOf(time) + k + ".jpg");
     }
 }

//解析获取excel中的图片
public static Map<String, XSSFPictureData> getPictures( XSSFSheet xssfSheet){
     Map<String,XSSFPictureData> sheetIndexPicMap=new HashMap<>();

     for (POIXMLDocumentPart dr : xssfSheet.getRelations()) {
         if (dr instanceof XSSFDrawing) {
             XSSFDrawing drawing = (XSSFDrawing) dr;
             List<XSSFShape> shapes = drawing.getShapes();
             for (XSSFShape shape : shapes) {
                 XSSFPicture pic = (XSSFPicture) shape;
                 XSSFClientAnchor xssfClientAnchor=(XSSFClientAnchor) pic.getAnchor();
                 XSSFPictureData pdata = pic.getPictureData();
                 // 行号-列号
                 String key = xssfClientAnchor.getRow1() + "-" + xssfClientAnchor.getCol1();
                 sheetIndexPicMap.put(key, pdata);
             }
         }
     }

     return sheetIndexPicMap;
 }

 /**
  * 文件byte[]类型转File
  *
  * @param bytes     bytes
  * @param outPath   输出目录
  * @param fileName  文件名
  * @return
  */
 public static File bytesToFile(byte[] bytes, String outPath, String fileName) {
     BufferedOutputStream bos = null;
     FileOutputStream fos = null;
     File file = null;
     try {
         File dir = new File(outPath);
         if (!dir.exists() && dir.isDirectory()) { //判断文件目录是否存在
             dir.mkdirs();
         }
         file = new File(outPath + File.separator + fileName);
         fos = new FileOutputStream(file);
         bos = new BufferedOutputStream(fos);
         bos.write(bytes);
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         if (bos != null) {
             try {
                 bos.close();
             } catch (IOException e1) {
                 e1.printStackTrace();
             }
         }
         if (fos != null) {
             try {
                 fos.close();
             } catch (IOException e1) {
                 e1.printStackTrace();
             }
         }
     }
     return file;
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/273096
推荐阅读
相关标签
  

闽ICP备14008679号