java - 使用 POI 在 Excel 中拉伸(stretch)图像

标签 java excel apache-poi

在我的应用程序中,我使用 POI API 生成了一个 Excel 文件。文档左上角的一个单元格包含一张图像。我遇到的问题是图像被拉伸(stretch)以填充容纳它的单元格。

根据我在 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;

有谁知道有什么办法可以避免这个问题,让图片保持原来的比例吗?

最佳答案

documentation 中有一点警告.

void resize()

Reset the image to the original size.

Please note, that this method works correctly only for workbooks with default font size (Arial 10pt for .xls and Calibri 11pt for .xlsx). If the default font is changed the resized image can be streched vertically or horizontally.

关于java - 使用 POI 在 Excel 中拉伸(stretch)图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8182689/

相关文章:

c# - 在 C# 中读取 Excel 文件

excel - 将工作表保存为 CSV,且 Excel 公式完好无损

java - 使用 Java 编程处理 Excel 表格

java - Apache-poi : cannot add image in docx header

java - iText : PDFPTable - Not able to insert data in specific column

java - 如何从 Java 应用程序中的 Main 方法运行线程?

java - 并发应用中的 PreparedStatement 批量更新

java - 通过 JSON Android Java 动态设置选项卡标题

java - 将xml文件转换为jrxml文件

excel - 如果满足条件,则计算唯一值