赞
踩
excel文件如图,获取张三的基本信息及图片信息。
代码如下:
需要导入poi相关的jar 版本3.9及以上
public class ReadExcel {
public static void main(String[] args) throws Exception {
InputStream inp = new FileInputStream("F:\\xxx.xls");
HSSFWorkbook workbook = (HSSFWorkbook) WorkbookFactory.create(inp);
List<HSSFPictureData> pictures = workbook.getAllPictures();
HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(0);
Map<String,Object> map = new HashMap<String,Object>();
for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
if (shape instanceof HSSFPicture) {
HSSFPicture pic = (HSSFPicture) shape;
int row = anchor.getRow2();
int col = anchor.getCol2();
System.out.println("--->" + anchor.getRow2() + ":" + anchor.getCol2());
//map.put(row+":"+col, row+":"+col);
int pictureIndex = pic.getPictureIndex()-1;
HSSFPictureData picData = pictures.get(pictureIndex);
System.out.println("--->" + picData);
map.put(row+":"+col, picData);
savePic(UUID.randomUUID().toString(), picData);
}
}
System.out.println(map);
}
private static void savePic(String i, PictureData pic) throws Exception {
String ext = pic.suggestFileExtension();
byte[] data = pic.getData();
if (ext.equals("jpeg")) {
FileOutputStream out = new FileOutputStream("F:\\" + i + ".jpg");
out.write(data);
out.close();
File file = new File("F:\\" + i + ".jpg");
FileInputStream in = new FileInputStream(file);
System.out.println("in===>"+in);
if(file.isFile()){
file.delete();
System.out.println("=============delete");
}
}
/* if (ext.equals("png")) {
FileOutputStream out = new FileOutputStream("F:\\" + i + ".jpg");
out.write(data);
out.close();
} */
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。