使用 Apache POI(对于 xls 和 xlsx 文件)时获取 excel 文件中的图像大小(高度和宽度)而不是原始大小
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)
目前我们正在开发 api 将 xls 和 xlsx 文件转换为 html,我们希望保留图像(excel 文件中的大小和位置)我们想要获得我们在 excel 文件中看到的图像大小(调整大小后)。我们尝试了这种方法:
for (HSSFPictureData pic : workbook.getAllPictures()) {
InputStream in = new ByteArrayInputStream(pic.getData());
BufferedImage image = ImageIO.read(in);
System.out.println(image.getWidth() + ":" + image.getHeight());
}
但它返回的是原始大小,而不是我们在 excel 文件中调整过的大小。有什么办法可以实现吗?
Workbook.getAllPictures()
获取嵌入在 Excel
文件中的所有图片文件。这些图片文件包含普通图片数据。所以无法通过任何方法获取缩放后的图片大小。
缩放后的图片包含在 sheet 绘图中,并固定在 sheet 的单元格中。因此,要获得缩放后的图片,需要遍历 sheet 个图形以从中提取 Picture
个图形。从 Picture
可以得到 sheet 中的锚定位置、锚点的尺寸(又名缩放大小)以及 PictureData
.
完整示例(使用当前 apache poi 5.0.0
进行测试和工作,同时适用于 HSSF
和 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();
}
}
目前我们正在开发 api 将 xls 和 xlsx 文件转换为 html,我们希望保留图像(excel 文件中的大小和位置)我们想要获得我们在 excel 文件中看到的图像大小(调整大小后)。我们尝试了这种方法:
for (HSSFPictureData pic : workbook.getAllPictures()) {
InputStream in = new ByteArrayInputStream(pic.getData());
BufferedImage image = ImageIO.read(in);
System.out.println(image.getWidth() + ":" + image.getHeight());
}
但它返回的是原始大小,而不是我们在 excel 文件中调整过的大小。有什么办法可以实现吗?
Workbook.getAllPictures()
获取嵌入在 Excel
文件中的所有图片文件。这些图片文件包含普通图片数据。所以无法通过任何方法获取缩放后的图片大小。
缩放后的图片包含在 sheet 绘图中,并固定在 sheet 的单元格中。因此,要获得缩放后的图片,需要遍历 sheet 个图形以从中提取 Picture
个图形。从 Picture
可以得到 sheet 中的锚定位置、锚点的尺寸(又名缩放大小)以及 PictureData
.
完整示例(使用当前 apache poi 5.0.0
进行测试和工作,同时适用于 HSSF
和 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();
}
}