带有图片的Excel信息导入

 
```/**
     * 获取图片保存后的路径
     * @param sheet
     * @param request
     * @return
     */
    public static Map<String, String> getPictures(XSSFSheet sheet,HttpServletRequest request){
        Map<String, String> map = new HashMap<>();
        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();
                    //上图图片至服务器,返回路径
                   String path = uploadPicture(key,picture.getPictureData(),request);
                   map.put(key,path);
                }
            }
        }
        return map;
    }

    /**
     * 保存图片至服务器
     * @param str
     * @param pictureData
     * @param request
     * @return
     */
    public static String uploadPicture(String str,PictureData pictureData,HttpServletRequest request){
        //获取服务器路径
        String savePath = request.getSession().getServletContext().getRealPath("");
        String dateDir = new SimpleDateFormat("yyMMdd").format(new Date());
        // 获取图片格式
        String ext = pictureData.suggestFileExtension();
        //设置保存目录
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid + str.replace("-","");
        File fileDir = new File(savePath + PIC_PATH + dateDir);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        //设置保存路径
        String filePath = savePath + PIC_PATH + dateDir + File.separator +fileName + "." + ext;
        //设置生成缩略图路径
        String smallFilePath = savePath + PIC_PATH + dateDir + File.separator +fileName + "_80_80" + "." + ext;
        //将图片转为字节数组
        byte[] data = pictureData.getData();
        try {
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
            stream.write(data);
            stream.close();
            //生成缩略图方法
            createThumbnails(filePath, 80, 80, smallFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String relativePath = PIC_PATH + dateDir + File.separator +fileName + "." + ext;
        return relativePath;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值