从 .xls 文件中读取图像及其位置的参考 [英] Read images from .xls file together with the references for their locations

查看:18
本文介绍了从 .xls 文件中读取图像及其位置的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In one of my projects I need to read images from an .xls file. For each row there is a column containing an image which I need to read out.

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

It looks like I can read all the images together, but how can I also get the position of each image, like column and row numbers, so I can relate those images with other data?

推荐答案

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

As long as the shapes are pictures, the following is possible:

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

这篇关于从 .xls 文件中读取图像及其位置的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆