加载中…
个人资料
KimMx
KimMx
  • 博客等级:
  • 博客积分:0
  • 博客访问:307
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

JAVA读取Excel中的图片,图片所在行、列数据

(2013-10-22 11:27:51)
标签:

数据

文件名

后缀

java读excel图片

it

分类: Java开发

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);
  }
 }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有