preface

The customer has sent an Excel document, in which there are problems in the customer’s arrangement, one line for each problem. There are also screenshot instructions, and pictures are separately placed in the small grid of each line, as shown in the picture below

When I open an Excel document and double-click on an image, I can’t enlarge it. What do I think?

recommended

Apache POI is a free, open source, cross-platform Java API written in Java. Apache POI provides apis for Java programs to read and write files in Microsoft Office (Excel, WORD, PowerPoint, Visio, etc.) formats.

case

The development environment

  • Maven

  • JDK1.8

Pom. XML is introduced into:

 <!--hutool工具类-->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-poi</artifactId>
    <version>5.3.5</version>
</dependency>

 <! --Apache POI-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
Copy the code

The test code

public static void main(String[] args) throws Exception {
	ExcelReader reader = ExcelUtil.getReader("test.xlsx");
    Workbook workbook = reader.getWorkbook();
    List<? extends PictureData> pictures = workbook.getAllPictures();
    for (PictureData picture : pictures) {
        byte[] data = picture.getData();
        FileOutputStream out = new FileOutputStream(UUID.randomUUID() + ".jpg"); out.write(data); out.close(); }}Copy the code

summary

Finally, I found that the excel file was opened inside the nail, and the pictures in it could be previewed. However, I know how to import pictures in Java, so maybe I can use them in the next project