Java读取Excel中的图片,并取得图片在Excel中的坐标

参考文章地址:https://blog.csdn.net/weixin_41843053/article/details/81740521
参考文章作者:https://blog.csdn.net/weixin_41843053
@赏花同学

使用的工具的hutool

<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.2.3</version>
		</dependency>

Excel操作包是apache的poi

<dependency>
			<groupId>;.org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.17</version>
			<type>pom</type>
		</dependency>

我设置了一个bean用来查看结果,不需要的可以不用

import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class ImageBean {

    private int row;
    private int col;

    private String code1;
    private String code2;

    private String url;
}

解析Excel
我因为每个sheet中需要的字段所处的列不同,所以每个sheet是单独操作的,如果需要的内容所在位置一样,可以直接循环操作

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析Excel获取图片,并获取图片在表中的坐标
 */


public class ImageTest {
    public static void main(String[] args) throws InvalidFormatException, IOException {
    	//需要解析的Excel文件
        File file = new File("filePath");
        //图片文件的下载路径
        String dest = "downloadPath";

        int sheetIndex = 0;

        ExcelReader reader = ExcelUtil.getReader(file, sheetIndex);
        List<ImageBean> list = new ArrayList<>();
        Workbook workbook = reader.getWorkbook();
        XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(sheetIndex);

        List<String> fileNameList = new ArrayList<>();
        List<List<Object>> values = reader.read();
        //对表格进行操作

		//03版本
		//for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
		//07版本
        for (XSSFShape shape : sheet.getDrawingPatriarch().getShapes()) {
            XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
            if (shape instanceof XSSFPicture) {
                XSSFPicture pic = (XSSFPicture) shape;
                //获取行编号  
                //getRow2():获取图片右下角行号
                //getRow1():获取图片左上角行号
                int row = anchor.getRow2();
                //获取列编号
                //getCol2():获取图片右下角列号
                //getCol1():获取图片左上角列号
                int col = anchor.getCol2();

                List<Object> os = values.get(row);
				//这是我在业务中需要获取的单元格内容,不需要的话可以忽略
                String code1 = os.get(0).toString();
                String code2 = os.get(1).toString();

                Long id = IdWorker.getId();
                
                //03版本
                //int pictureIndex = pic.getPictureIndex()-1;
                //HSSFPictureData picData = pictures.get(pictureIndex);
                //07版本
				XSSFPictureData pictureData = pic.getPictureData();
                String fileSuffix = pictureData.suggestFileExtension();
                String fileName = id+"."+fileSuffix;
                if(fileNameList.contains(fileName)){
                    System.out.println(fileName+"已存在");
                }
                String url = dest+RandomUtil.randomString(10)+fileName;
                
              
                FileUtil.writeBytes(pictureData.getData(), url);

				//如果不需要查看行列及图片,可以忽略下面的内容了
                ImageBean bean = new ImageBean();
                bean.setRow(row).setCol(col).setUrl(url).setCode1(code1).setCode2(code2);
                list.add(bean);
            }
        }
        list.forEach(one -> System.out.println(one));
    }


}

本人需要解析的Excel文件是.xlsx格式的
xlsx与xls格式部分方法存在不同,07版本正常,03版本未测试

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值