POI导入excel读取excel中图片

1.首先excel图片需要跟单元格绑定

2.pom文件

<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>${poi.version}</version>
</dependency>

3.编写测试方法进行导入

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Shape;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * 资产清单Controller
 * 
 * @author hhxx
 * @date 2024-03-13
 */
@RestController
@RequestMapping("/expand/property")
public class BuPropertyListController extends BaseController
{
    @PreAuthorize("@ss.hasPermi('expand:property:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
    	// 获取文件流
    	InputStream inputStream = file.getInputStream();
    	// 创建一个工作薄对象
    	Workbook workbook = new XSSFWorkbook(inputStream);
    	// 获取第一个工作表对象
    	String sheetName = workbook.getSheetName(0);
    	Sheet sheet = workbook.getSheet(sheetName);
    	// 获取excel所有的图片对象
    	List<XSSFShape> shapes = ((XSSFDrawing) sheet.getDrawingPatriarch()).getShapes();
        for (Shape shape : shapes) {
            if (shape instanceof XSSFPicture) {
                XSSFPicture picture = (XSSFPicture) shape;
                ClientAnchor anchor2 = picture.getClientAnchor();
                int row1 = anchor2.getRow1();
                short col1 = anchor2.getCol1();
                // 获取图片单元格坐标。例R3、A4等
//                String cellRef = new CellReference(row1, col1).formatAsString();
                // 根据图片位置获取对应的第一行标题单元格
                Row row = sheet.getRow(row1-row1);
                Cell cell3 = row.getCell(col1);
                // 获取标题单元格文本内容
                String stringCellValue = cell3.getStringCellValue();
                System.out.println("图片所在的单元格标题: " + stringCellValue);
                // 获取图片数据
                XSSFPictureData pictureData = picture.getPictureData();
                byte[] bytes = pictureData.getData();
                // 图片保存至本地
                FileOutputStream out = new FileOutputStream("D:\\img\\pic\\image_" + UUID.randomUUID().toString() + "." + pictureData.suggestFileExtension());
                out.write(bytes);
                out.close();
            }
        }
        workbook.close();
        return AjaxResult.success();
    }
}

4.展示效果

5.根据此内容可以扩展保存数据库功能,在此暂不实现。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在使用POI读取Excel时,可以通过 HSSFClientAnchor 类来获取 Excel 图片的位置和大小信息,并通过 PictureData 类来获取图片的二进制数据,然后可以将图片保存到本地或者上传到服务器。 以下是一个示例代码: ```java FileInputStream inputStream = new FileInputStream("test.xls"); Workbook workbook = new HSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); // 获取所有图片并处理 List<PictureData> pictures = new ArrayList<>(); Map<Integer, PictureData> picturesMap = new HashMap<>(); List<HSSFShape> shapes = ((HSSFSheet) sheet).getDrawingPatriarch().getChildren(); for (HSSFShape shape : shapes) { if (shape instanceof HSSFPicture) { HSSFPicture picture = (HSSFPicture) shape; PictureData pictureData = picture.getPictureData(); pictures.add(pictureData); picturesMap.put(picture.getPictureIndex(), pictureData); } } // 处理单元格图片 for (Row row : sheet) { for (Cell cell : row) { if (cell.getCellTypeEnum() == CellType.BLANK) { continue; } if (cell.getCellTypeEnum() == CellType.STRING && StringUtils.isNotBlank(cell.getStringCellValue())) { RichTextString richTextString = cell.getRichStringCellValue(); for (int i = 0; i < richTextString.numFormattingRuns(); i++) { int startIndex = richTextString.getIndexOfFormattingRun(i); int endIndex = startIndex + richTextString.getLengthOfFormattingRun(i); HSSFFont font = (HSSFFont) richTextString.getFontOfFormattingRun(i); if (font.getUnderline() == Font.U_SINGLE && font.getColor() == HSSFColor.AUTOMATIC.index) { for (int j = startIndex; j < endIndex; j++) { HSSFRichTextString hssfRichTextString = (HSSFRichTextString) richTextString; int pictureIndex = hssfRichTextString.getIndexOfFormattingRun(i) / 3; PictureData pictureData = picturesMap.get(pictureIndex); if (pictureData != null) { String fileName = UUID.randomUUID().toString() + "." + pictureData.suggestFileExtension(); byte[] data = pictureData.getData(); // 处理图片数据 } } } } } } } ``` 在上面的代码,首先获取 Excel 的所有图片,然后遍历单元格,通过 RichTextString 获取单元格的所有文本和图片,并通过 HSSFRichTextString.getIndexOfFormattingRun() 方法获取图片在文本的位置,最后可以通过 HSSFClientAnchor 类获取图片Excel 的位置和大小信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wengelovelian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值