简体   繁体   中英

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). 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. Is there any way to achieve this?

Workbook.getAllPictures() gets all picture files embedded in Excel file. 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. From Picture one can get the anchored position in sheet, the dimension of the anchor (aka scaled size) and also the PictureData .

Complete example (tested and works using current apache poi 5.0.0 , works for both HSSF as well as XSSF ).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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