我根据自己的需求,把excel的所有图片都弄出来,然后,保存起来
package cn.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFPictureData;
import org.apache.poi.hssf.usermodel.HSSFShape;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import fr.opensagres.xdocreport.core.io.internal.ByteArrayOutputStream;
public class ExcelGetImg {
public static Map<String, PictureData> map =null;
public static Map<String, PictureData> getPictureFromExcel(FileInputStream fis, int
sheetNum) throws EncryptedDocumentException, InvalidFormatException, IOException{
//创建Map
map = new HashMap<String, PictureData>();
//获取HSSFWorkbook对象
HSSFWorkbook workbook = (HSSFWorkbook) WorkbookFactory.create(fis);
//获取图片HSSFPictureData集合
List<HSSFPictureData> pictures = workbook.getAllPictures();
//获取当前表编码所对应的表
HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(sheetNum - 1);
//对表格进行操作
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();
int pictureIndex = pic.getPictureIndex()-1;
HSSFPictureData picData = pictures.get(pictureIndex);
map.put(row+":"+col, picData);
}
}
return map;
}
public static void main(String [] args) throws Exception{
File sourcefile = new File("E:\test\11.xls");
FileInputStream is = new FileInputStream(sourcefile);
map= getPictureFromExcel(is, 1);
for(Entry<String, PictureData> a:map.entrySet()){
System.out.println("键是"+a.getKey());
System.out.println("值是"+a.getValue());
PictureData picData = map.get(a.getKey());
//String ext = picData.suggestFileExtension();//获取图片的后缀名
byte[] data = picData.getData();
String fileName = UUID.randomUUID().toString();
//文件夹路径:temporarypath
String temporarypath ="E:\test\857";
FileOutputStream out = new FileOutputStream(temporarypath + fileName + ".jpg");
out.write(data);
out.close();
}
}
}