使用Java和Apache POI在Excel单元格中自定义图像大小

时间:2015-06-21 07:21:44

标签: java file-io apache-poi xls

我正在使用apache poi以xls格式提取某些数据。每列的单元格大小已经固定。

现在我的数据被完美地提取到每个相应的单元格,但我想更改在一个单元格中提取的图像的比例。

我想更改图像的缩放比例,让高度为12%,宽度为16%而不改变单元格大小。

我的代码:

ResultSetMetaData resultSetMetaData = givenSetForExcel.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook.createSheet(this.reportName);

int pictureIdx = 0;
if (substr.equalsIgnoreCase("jpg")
    || substr.equalsIgnoreCase("jpeg")) {
            pictureIdx = workbook.addPicture(bytes,
            Workbook.PICTURE_TYPE_JPEG);
} else if (substr.equalsIgnoreCase("png")) {
            pictureIdx = workbook.addPicture(bytes,
            Workbook.PICTURE_TYPE_PNG);
}
try {
        is.close();
    } catch (Exception e) {
        dataCell.setCellValue("");
        break;
    }

CreationHelper helper = workbook.getCreationHelper();
Drawing drawing = spreadsheet.createDrawingPatriarch();
// fit picture
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(i);
anchor.setRow1(rowCounter);
anchor.setCol2(i + 1);
anchor.setRow2(rowCounter + 1);
row.setHeight((short) 600);
drawing.createPicture(anchor, pictureIdx);

0 个答案:

没有答案