本文介绍了读取.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文件的映像与它们的位置引用在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 00:05