java poi导入文字和图片

public void vendorImportExcel(MultipartFile file) throws Exception {
        int suffixIndex = file.getOriginalFilename().lastIndexOf(".");
        String suffix = file.getOriginalFilename().substring(suffixIndex);
        if (".xlsx".equals(suffix)) {
            throw new RuntimeException("请上传xlsx格式的excel");
        }
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        List<ImportPackingEntity> list = ExcelImportUtil.importExcel(file.getInputStream(), ImportPackingEntity.class, params);
        if (CollectionUtils.isEmpty(list)) {
            throw new RuntimeException("表格数据为空");
        }

        //读取表格中的图片
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFDrawing dp = sheet.createDrawingPatriarch();
        List<XSSFShape> pics = dp.getShapes();
        for (XSSFShape shape : pics) {
            XSSFPicture inpPic = (XSSFPicture) shape;
            XSSFClientAnchor clientAnchor = inpPic.getClientAnchor();
            PictureData pict = inpPic.getPictureData();

            //图片必须要严格定位到一个具体的单元格子内
            int col = clientAnchor.getCol1();
            int row = clientAnchor.getRow1();
//            System.out.println("col1: " + clientAnchor.getCol1() + ", col2: " + clientAnchor.getCol2() + ", row1: " + clientAnchor.getRow1() + ", row2: " + clientAnchor.getRow2());

            ImportPackingEntity entity = list.get(row - 1);
            String processNo = getNextProcessNo();
            entity.setPackingNo(processNo);
            String fileName = processNo;
            if (col == 23) {//零件图片
                fileName = fileName + "零件图片";
            } else if (col == 24) {//内包装图片
                fileName = fileName + "内包装图片";
            } else if (col == 25) {//外包装图片
                fileName = fileName + "外包装图片";
            }
            String fileExtension = pict.suggestFileExtension();
            fileName = fileName + "." + fileExtension;
            byte[] data = pict.getData();
            FastDFSFile file1 = new FastDFSFile(data, fileExtension);
            NameValuePair[] metaList = new NameValuePair[4];
            metaList[0] = new NameValuePair("fileName", fileName);
            metaList[1] = new NameValuePair("fileLength", String.valueOf(data.length));
            metaList[2] = new NameValuePair("fileExt", fileExtension);
            metaList[3] = new NameValuePair("fileAuthor", "");
            String filePathJson = FileManager.upload(file1, metaList);
            cn.hutool.json.JSONObject json = new cn.hutool.json.JSONObject(filePathJson);
            String uri = json.toString();
            String filepath = Base64Util.getBase64(uri);
            System.out.println(filepath);
            if (col == 23) {//零件图片
                entity.setImageName1(fileName);
                entity.setImageUrl1(filepath);
            } else if (col == 24) {//内包装图片
                entity.setImageName2(fileName);
                entity.setImageUrl2(filepath);
            } else if (col == 25) {//外包装图片
                entity.setImageName3(fileName);
                entity.setImageUrl3(filepath);
            }
        }
        List<PackingEntity> packingEntityList = new ArrayList<>();
        PackingEntity entity = null;
        for (ImportPackingEntity importPackingEntity : list) {
            entity = new PackingEntity();
            BeanUtils.copyProperties(importPackingEntity, entity);
            packingEntityList.add(entity);
        }
        this.saveBatch(packingEntityList);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值