在我的应用程序中,我使用POI API生成Excel文件.文档的左上角之一保留图像.我得到的问题是图像被拉伸以填充容纳它的单元格.

根据我在Picture对象上使用的调整大小的方法,图像以各种大小显示,但它始终具有其所在单元格所具有的水平 – 垂直比率,换句话说,它不会保持自己的比例.

这是我的代码:

titleRow = sheet.createRow(0);

titleRow.setHeightInPoints(25f);

titleRow.createCell(0);

sheet.getRow(0).getCell(0).setCellStyle(defaultTitleStyle(wb));

WebApplicationContext webCtx = ((WebApplicationContext)AtlasApplicationUtils.getCurrentApplication().getContext());

ServletContext sc = webCtx.getHttpSession().getServletContext();

String contextPath = sc.getContextPath();

// sc.getResourceAsStream(contextPath + "/VAADIN/themes/m2m/../m2m/img/gnd_logo_white.png")

String f = new File("").getAbsolutePath();

InputStream is = new FileInputStream(System.getProperty("user.dir") + "/src/main/webapp/VAADIN/themes/m2m/img/gnd_logo_white.png");

byte[] bytes = IOUtils.toByteArray(is);

int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);

is.close();

CreationHelper helper = wb.getCreationHelper();

Drawing drawing = sheet.createDrawingPatriarch();

ClientAnchor anchor = helper.createClientAnchor();

anchor.setCol1(0);

anchor.setRow1(0);

sheet.autoSizeColumn(0);

Picture pict = drawing.createPicture(anchor, pictureIdx);

pict.getPreferredSize();

pict.resize(0.25);

造型方法:

protected CellStyle defaultTitleStyle(final HSSFWorkbook wb) {

CellStyle style;

final Font titleFont = wb.createFont();

titleFont.setFontHeightInPoints((short) 18);

titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);

titleFont.setColor(IndexedColors.GREY_80_PERCENT.getIndex());

style = wb.createCellStyle();

style.setAlignment(CellStyle.ALIGN_CENTER);

style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());

style.setFillPattern(CellStyle.SOLID_FOREGROUND);

// style.setFillBackgroundColor(IndexedColors.BLUE_GREY.getIndex());

style.setFont(titleFont);

HSSFPalette palette = wb.getCustomPalette();

palette.setColorAtIndex(HSSFColor.BLUE_GREY.index,

(byte) 7,

(byte) 64,

(byte) 127);

palette.setColorAtIndex(HSSFColor.GREY_80_PERCENT.index,

(byte) 228,

(byte) 228,

(byte) 228);

return style;

有谁知道避免这个问题的方法,让图像保持原来的比例?

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐