读取Excel中的图片

Maven:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>3.17</version>
 </dependency>

Code:

File excelFile = new File("C:\\Users\\Administrator\\Desktop\\C.xlsx");
		XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(excelFile));
		// 读取图片
		List<XSSFPictureData> pictures = wb.getAllPictures();
		for (int i = 0; i < pictures.size(); i++) {
			XSSFPictureData pictureData = pictures.get(i);
			byte[] picData = pictureData.getData();
			// File file = new File("C:\\Users\\Administrator\\Desktop\\IMG\\" +
			// RandomUtil.randomString(8) + ".png");
			FileOutputStream out = new FileOutputStream(
					"C:\\Users\\Administrator\\Desktop\\C\\c000" + (i + 1) + ".png");
			out.write(picData);
			out.close();
		}

ps: 注意读取的图片可能是无序的

你可能感兴趣的:(java,工具类)