zoukankan      html  css  js  c++  java
  • 记录POI获取excel中获取单元格内图片的数据和位置

    /**
         * 获取图片和位置 (xlsx)
         */
        public static Map<String, String> getPictures2(XSSFSheet sheet) throws Exception {
            Map<String, String> map = new HashMap<>(16);
            List<POIXMLDocumentPart> list = sheet.getRelations();
            for (POIXMLDocumentPart part : list) {
                if (part instanceof XSSFDrawing) {
                    XSSFDrawing drawing = (XSSFDrawing) part;
                    List<XSSFShape> shapes = drawing.getShapes();
                    for (XSSFShape shape : shapes) {
                        XSSFPicture picture = (XSSFPicture) shape;
                        // 会有空指针问题
                        // XSSFClientAnchor anchor = picture.getPreferredSize();
                        XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
                        CTMarker marker = anchor.getFrom();
                        String key = marker.getRow() + "-" + marker.getCol();
                        String imgName = "lihang/lihang_picture_".concat(System.currentTimeMillis() + ".png");
                        String url = OssUtils.uploadToOss(picture.getPictureData().getData(), imgName, 1L);
                        map.put(key, url);
                    }
                }
            }
            return map;
        }
    
        /**
         * 获取图片和位置 (xls)
         */
        public static Map<String, String> getPictures1(HSSFSheet sheet) throws Exception {
            Map<String, String> map = new HashMap<>(16);
            List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
            for (HSSFShape shape : list) {
                if (shape instanceof HSSFPicture) {
                    HSSFPicture picture = (HSSFPicture) shape;
                    HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
                    // 行号-列号
                    String key = cAnchor.getRow1() + "-" + cAnchor.getCol1();
                    String imgName = "lihang/lihang_picture_".concat(System.currentTimeMillis() + ".png");
                    String url = OssUtils.uploadToOss(picture.getPictureData().getData(), imgName, 1L);
                    map.put(key, url);
                }
            }
            return map;
        }

      

  • 相关阅读:
    BZOJ 4886 Lydsy1705月赛 叠塔游戏
    BZOJ 4552 TJOI2016&&HEOI2016 排序
    BZOJ 3702 二叉树
    BZOJ 4756 Usaco2017 Jan Promotion Counting
    BZOJ 4842 Neerc2016 Delight for a Cat
    BZOJ 1283 序列
    BZOJ 4819 SDOI2017 新生舞会
    BZOJ 1531 POI2005 Bank notes
    BZOJ 1925 SDOI2010 地精部落
    BZOJ WC2006 水管局长数据加强版
  • 原文地址:https://www.cnblogs.com/Crush123/p/15211254.html
Copyright © 2011-2022 走看看