开发者

java读取excel图片导入代码示例(亲测有效)

开发者 https://www.devze.com 2023-11-20 10:45 出处:网络 作者: 学
目录从excel文件中获取图片(兼容新老版本)从sheet页中获取图片及图片位置main方法-获取excel文件中的图片下载到本地总结从excel文件中获取图片(兼容新老版本)
目录
  • 从excel文件中获取图片(兼容新老版本)
  • 从sheet页中获取图片及图片位置
  • main方法-获取excel文件中的图片下载到本地
  • 总结

从excel文件中获取图片(兼容新老版本)

    /**
     * 获取excel表中的图片
     *
     * @return
     * @throws IOException
     * @throws InvalidFormatException
     * @throws EncryptedDocumentException
     * @Param fis 文件输入流
     * @Param sheetNum Excel表中的sheet编号
     */
    public static Map<String, PictureData> getPictureFromExcel(File file, int sheetNum) throws EncryptedDocumentException, IOException {

        //获取图片PictureData集合
        String fileName = file.getName();
        Workbook workbook = null;
        if (StringUtil.isEmpty(fileName)) {
            return null;
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        if (fileName.endsWith("xls")) {
            //2003
            workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(sheetNum - 1);
            Map<String, PictureData> pictures = getPicturesHyeduU(sheet);
            return pictures;
      js  } else if (fileName.endsWith("xlsx")) {
            //2007
            workbook = new XSSFWorkbook(fileInputStream);
            XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(sheetNum - 1);
            Map<String, PictureData> pictures = getPictures(sheet);
            return pictures;
        }
        return new HashMap();
    }

从sheet页中获取图片及图片位置

/**
     * 获取图片和位置 (xls版)
     * @param sheet
     * @return
     * @throws IOException
     */
    public static Map<String, PictureData> getPictures (HSSFSheet sheet) throws IOException {
        Map<String, PictureData> map = new HashMap<String, PictureData>();
        List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
        for (HSSFShape shape : list) {
            if (shape instanceof HSSFPicture) {
                HSSFPicture picture = (HSSFPicture) shape;
                HSSFClientAnchor cAnchor = picture.getClientAnchor();
                PictureData pdata = picture.getPictureData(http://www.devze.com);
                String key = cAnchor.getRow1() + "-" + cAnchor.getCol1(); // 行号-列号
                map.put(key, pdata);
            }
        }
        return map;
    }

    /**
     * 获取图片和位置 (xlsx版)
     * @param sheet
     * @return
     * @throws IOException
     */
    public static Map<String, PictureData> getPictures (XSSFSheet sheet) throws IOException {
        Map<String, PictureData> map = new HashMap<String, PictureData>();
        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();
                    CTMarker marker = anchor.getFrom();
                    String key = marker.getRow() + "-" + marker.getCol();
                    mHyeduUap.put(key, picture.getPictureData());
                }
            }
        }
        return map;
    }

main方法-获取excel文件中的图片下载到本地

    public static void main(String [] args) throws IOException {
        File file = new File("C:\\Users\\A\\Desktop\\test.xlsx");
        //获取图片数据
        Map<String, PictureData> map = getPictureFromExcel(file, 1);
        //根据图片行列位置获取图片
        PictureData picData = (PictureData)map.get("3"+"-"+14);
        //后缀名
        String extension = picData.suggestFileExtension();
        byte[] data = picData.getData();
        FileOutputStream out = new FileOutputStream("C:\\Users\\A\\Desktop\\" + "picture." + extension);
        out.write(data);
        out.close();
    }

遇到问题:图片导入解析报错异常Java.lang.ClassCastException: org.apache.poi.xssf.usermodel.

XSSFSimpleShape cannot be cast to org.apache.poi.xssf.usermodel.XSSFPicture问题

解决方法:经过排查,为xslx版本shap对象强转为图片对象报错,添加逻辑首先检验if (shape instanceof XSSFPicture)类型然后HyeduU再进行强转,问题解决

总结

到此这篇关于java读取excel图片导入的文章就介绍到这了,更多相关java读取excel图片导入内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消