繁体   English   中英

从 .xls 文件中读取图像及其位置的参考

[英]Read images from .xls file together with the references for their locations

在我的一个项目中,我需要从.xls文件中读取图像。 对于每一行,有一列包含我需要读出的图像。

看起来我可以一起阅读所有图像,但是我怎样才能获得每个图像的位置,例如列号和行号,以便我可以将这些图像与其他数据相关联?

只要形状是图片,以下都是可能的:

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;

class GetShapePosition {

 public static void main(String[] args) {
  try {

   InputStream inp = new FileInputStream("workbook.xls");
   Workbook wb = WorkbookFactory.create(inp);

   HSSFSheet sheet = (HSSFSheet)wb.getSheetAt(0);

   HSSFPatriarch dravingPatriarch = sheet.getDrawingPatriarch();

   java.util.List<HSSFShape> shapes = dravingPatriarch.getChildren();

   for (HSSFShape shape : shapes) {
    if (shape instanceof HSSFPicture) {
     HSSFPicture hssfPicture = (HSSFPicture)shape;
     int picIndex = hssfPicture.getPictureIndex();
     String filename = hssfPicture.getFileName();
     int row = hssfPicture.getClientAnchor().getRow1();
     int col = hssfPicture.getClientAnchor().getCol1();
     System.out.println("Picture " + picIndex + " with Filename: " + filename + " is located row: " + row + ", col: " + col);
    }
  }

  } catch (InvalidFormatException ifex) {
  } catch (FileNotFoundException fnfex) {
  } catch (IOException ioex) {
  }
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM