导入excel列表时读取里面的图片(只适用于浮动图片,嵌入的需解析xml比较麻烦),兼容03与07两版本

主要思路:
1.读取所有图片,先传到服务器本地,用row行编号命名图片名称.
2.传到图片服务器,获取图片url
3.与excel其它数据行匹配

	基于poi 5.2.2
<poi-version>5.2.2</poi-version>
  <poi-ooxml-version>5.2.2</poi-ooxml-version>

`
--------------- 不知为啥首尾两行不进去的分割线 ------------------------------
//上传到本地图片
public void importExcel(MultipartFile files) {

InputStream inputStream = files.getInputStream();
//hutool工具类
ExcelReader excelReader = ExcelUtil.getReader(inputStream);

List<String> urlList =new ArrayList<>();
	
	//excel版本判断
    Workbook workbook = excelReader.getWorkbook();
    boolean check = workbook instanceof HSSFWorkbook ? true : false;
    //excel2003
    if(check){
        HSSFSheet  sheet = (HSSFSheet) workbook.getSheetAt(0);
        if(ObjectUtil.isNotEmpty(sheet.getDrawingPatriarch())){
            for (HSSFShape shape : sheet.getDrawingPatriarch()){
                if(ObjectUtil.isNotEmpty(shape)){
                    HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
                    if (shape instanceof HSSFPicture) {
                        HSSFPicture pic = (HSSFPicture) shape;
                        //获取行编号
                        int row = anchor.getRow2();
                        //获取列编号
                        int col = anchor.getCol2();
                        log.info("####### row: "+row+" ###### col:"+col);
						
						HSSFPictureData pictureData = pic.getPictureData();
                        String fileSuffix = pictureData.suggestFileExtension();
                        String url = "//data//test//img//"+ RandomUtil.randomString(10)+"_"+row+"_"+col+"."+fileSuffix;
                        log.info("####### 图片地址: "+url);
                        FileUtil.writeBytes(pictureData.getData(), url);
                        urlList.add(url);
                    }
				}
            }
        }
	  }else{
        //excel 2007
        XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(0);
        if(ObjectUtil.isNotEmpty(sheet.getRelations())){
            for (POIXMLDocumentPart dr : sheet.getRelations()) {
                if (dr instanceof XSSFDrawing) {
                    XSSFDrawing drawing = (XSSFDrawing) dr;
                    List<XSSFShape> shapes = drawing.getShapes();
                    for (XSSFShape shape : shapes) {
                        XSSFPicture pic = (XSSFPicture) shape;
                        XSSFClientAnchor anchor = pic.getPreferredSize();
						 //获取行编号
                        int row = anchor.getRow1();
                        //获取列编号
                        int col = anchor.getCol2();

                        log.info("######### row1: "+row);
                        XSSFPictureData pictureData = pic.getPictureData();
                        String fileSuffix = pictureData.suggestFileExtension();
                        String url = "//data//test//img//"+ RandomUtil.randomString(10)+"_"+row+"_"+col+"."+fileSuffix;
                        log.info("####### 图片地址: "+url);
                        FileUtil.writeBytes(pictureData.getData(), url);
                        urlList.add(url);
					 }
                }
            }
        }
    }

	for (int i = 0; i < urlList.size(); i++) {
        String localimageUrl = urlList.get(i);
        log.info("####### 上传的图片地址"+localimageUrl);
        File file =new File(localimageUrl);

		//上传到fastdfs
        FileInputStream fileInputStream = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.IMAGE_JPEG.toString(), fileInputStream);

        StorePath storePath = fastFileStorageClientUtil.uploadFile(multipartFile);
        String fullPath = storePath.getFullPath();
        log.info("######### fullPath: "+ fullPath);
		.....
      
        String[] split = localimageUrl.split("\\.");
        String imageName = split[0];
        String[] imageNo = imageName.split("_");
        ... 切割,整理为map
        
      
    
	}

--------------- 不知为啥首尾两行不进去的分割线 ------------------------------

`

posted @ 2024-07-24 17:41  水滴aym  阅读(38)  评论(0编辑  收藏  举报