class Image{
private String
suffix;//后缀
private String
fileName;//文件名
private InputStream
stream;//文件流
public String getSuffix()
{
return
suffix;
}
public void setSuffix(String
suffix) {
this.suffix =
suffix;
}
public String getFileName()
{
return
fileName;
}
public void setFileName(String
fileName) {
this.fileName
= fileName;
}
public InputStream getStream()
{
return
stream;
}
public void
setStream(InputStream stream) {
this.stream =
stream;
}
}
private static Map getExcelDrawingImages(final
ZipFile docxFile){
Map rt = new HashMap();
try {
if(null ==
docxFile) return rt;
ZipEntry
documentXML =
docxFile.getEntry("xl/drawings/_rels/drawing1.xml.rels");
InputStream
documentXMLIS =
docxFile.getInputStream(documentXML);
SAXReader
saxReader = new SAXReader ();
Document doc
= saxReader.read (documentXMLIS);
Element root
= doc.getRootElement();
List items =
root.elements();
for (Element
item : items) {
String
id = item.attributeValue("Id");
String
target = item.attributeValue("Target");
if(!StringUtils.isBlank(id)
&& !StringUtils.isBlank(target)){
rt.put(id,
target.replace("../", "xl/"));
}
}
} catch (Exception e) {}
return rt;
}
public static Map getExcelImageAtRowAndCol(File
file) throws IOException{
InputStream documentXMLIS =
null;
Map rt = new HashMap();
try {
ZipFile
docxFile = new ZipFile(file);
Map imageData
= Excel.getExcelDrawingImages(docxFile);
ZipEntry
documentXML =
docxFile.getEntry("xl/drawings/drawing1.xml");
documentXMLIS
=
docxFile.getInputStream(documentXML);
SAXReader
saxReader = new SAXReader();
Document doc
= saxReader.read (documentXMLIS);
Element root
= doc.getRootElement();
List items =
root.elements();
for (Element
item : items) {
String
row = item.element("from").elementTextTrim("row");
String
col = item.element("from").elementTextTrim("col");
String
imageId =
item.element("pic").element("blipFill").element("blip").attributeValue("embed");
String
imagePath = imageData.get(imageId);
//处理
返回数据
if(!StringUtils.isBlank(row)
&& !StringUtils.isBlank(col) &&
!StringUtils.isBlank(imageId) &&
!StringUtils.isBlank(imagePath)){
String
key = col+"_"+row;
//处理图片流
ZipEntry
image = docxFile.getEntry(imageData.get(imageId));
InputStream
image_in = docxFile.getInputStream(image);
//后缀
String
suffix = imagePath.substring(imagePath.lastIndexOf("."));
System.out.println(suffix);
Image
im = new Excel().new Image();
im.setFileName(key+suffix);
im.setStream(image_in);
im.setSuffix(suffix);
rt.put(key,
im);
}
//System.out.println("列:"+col+"行:"+row+"-图片:"+imageData.get(imageId));
//FileHelper.SaveFileFromInputStream(image_in,
"D:\", row+".png");
}
} catch (Exception e) {
} finally{
if(documentXMLIS!=null){
documentXMLIS.close();
}
}
return rt;
}
public static void main(String[] args) throws
IOException {
Map imageData =
Excel.getExcelImageAtRowAndCol(new File("d:\\测试ee.xlsx"));
for (String key :
imageData.keySet()) {
System.out.println(key);
}
}
加载中,请稍候......