简体   繁体   English

在使用 Apache POI 时获取 excel 文件中的图像大小(高度和宽度)而不是原始大小(对于 xls 和 xlsx 文件)

[英]Get the image size (height and width) in excel files instead of the original size when using Apache POI (for both xls and xlsx files)

Currently we're developing an api to convert xls and xlsx files to html, we want to keep the images (both the size and position in excel files) We want to get the size of the images exactly what we see in the excel files (after being resized).目前我们正在开发一个 api 来将 xls 和 xlsx 文件转换为 html,我们希望保留图像(excel 文件中的大小和位置)我们希望获得与我们在 excel 文件中看到的完全相同的图像大小(调整大小后)。 We tried this approach:我们尝试了这种方法:

    for (HSSFPictureData pic : workbook.getAllPictures()) {
    InputStream in = new ByteArrayInputStream(pic.getData());
    BufferedImage image = ImageIO.read(in);
    System.out.println(image.getWidth() + ":" + image.getHeight());
}

But it returned the original size, not the one we resized in the excel file.但它返回了原始大小,而不是我们在 excel 文件中调整大小的大小。 Is there any way to achieve this?有没有办法实现这一目标?

Workbook.getAllPictures() gets all picture files embedded in Excel file. Workbook.getAllPictures()获取嵌入在Excel文件中的所有图片文件。 Those picture files contain the plain picture data.这些图片文件包含纯图片数据。 So there cannot be any method to get the scaled picture size.所以没有任何方法可以获得缩放后的图片大小。

The scaled pictures are contained in sheet drawings and are anchored to sheet's cells.缩放的图片包含在图纸中并锚定到图纸的单元格中。 So to get the scaled pictures, one needs traversing the sheet drawings to get the Picture s out of them.因此,要获得缩放的图片,需要遍历图纸以从中获取Picture From Picture one can get the anchored position in sheet, the dimension of the anchor (aka scaled size) and also the PictureData .Picture一可以得到工作表中的锚定位置、锚点的尺寸(又名缩放尺寸)以及PictureData

Complete example (tested and works using current apache poi 5.0.0 , works for both HSSF as well as XSSF ).完整示例(使用当前的apache poi 5.0.0测试和工作,适用于HSSFXSSF )。

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.ss.util.ImageUtils;
import org.apache.poi.util.Units;

import java.awt.Dimension;

import java.io.FileInputStream;

import java.util.List;

class ExcelGetPicturesWithPositionAndSize {

 public static void main(String[] args) throws Exception {

  //Workbook workbook = WorkbookFactory.create(new FileInputStream("ExcelWithImages.xlsx"));
  Workbook workbook = WorkbookFactory.create(new FileInputStream("ExcelWithImages.xls"));

  for (Sheet sheet : workbook) {
   String sheetname = sheet.getSheetName();

   Drawing drawing = sheet.getDrawingPatriarch();
   
   List<?> shapes = null;
   if (drawing instanceof XSSFDrawing) {
    shapes = ((XSSFDrawing)drawing).getShapes();
   } else if (drawing instanceof HSSFPatriarch) {
    shapes = ((HSSFPatriarch)drawing).getChildren();
   }
   
   for (Object shape : shapes) {
    if (shape instanceof Picture) {
     Picture picture = (Picture)shape;
     
     String shapename = picture.getShapeName();
     int row = picture.getClientAnchor().getRow1();
     int col = picture.getClientAnchor().getCol1();
System.out.println(
 "Picture with Shapename: " + shapename + 
 " is located sheet: " + sheetname + 
 ", row: " + row + 
 ", col: " + col
 );

     Dimension dimension = ImageUtils.getDimensionFromAnchor(picture);
System.out.println(
 "Picture's size in EMU: width=" + dimension.getWidth() + ", height=" + dimension.getHeight() +
 ", in pixel: width=" + dimension.getWidth()/Units.EMU_PER_PIXEL + ", height=" + dimension.getHeight()/Units.EMU_PER_PIXEL
 ); 
 
     PictureData pictureData =  picture.getPictureData();
System.out.println(
 "Picture's data: bytes=" + pictureData.getData().length +
 ", mime type=" + pictureData.getMimeType() +
 ", suggested file extension=" + pictureData.suggestFileExtension()
 ); 

    }
   } 
  }

  workbook.close();
 }
}

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

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