【问题标题】:Apache POI and autofit XSSFRow heigth by contentApache POI 和按内容自动调整 XSSFRow 高度
【发布时间】:2021-06-22 09:15:02
【问题描述】:

我有固定的列宽,需要使用 apache POI 自动调整行高,但我找不到它的工作设计。我试过this decision,但它不适用于此文本

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua

列宽为 33.00(236 像素),表格中的单元格具有 自动换行 属性。只有当内容高度大于现有行高时,我才能更改行高。

所以,我想问一下,有没有什么方法可以根据内容自动调整行高?

private float calculateRowLinesForText(Font cellFont, float columnWidthInPoints, String value) {
        java.awt.Font currFont = new java.awt.Font(cellFont.getFontName(), 0, cellFont.getFontHeightInPoints());

        FontRenderContext frc = new FontRenderContext(null, true, true);

        int lineCnt = 0;
        for (String partValue : value.split("\n")) {
            AttributedString attrStr = new AttributedString(partValue);
            attrStr.addAttribute(TextAttribute.FONT, currFont);
            LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc);

            while (measurer.getPosition() < partValue.length()) {
                int nextPos = measurer.nextOffset(columnWidthInPoints);
                lineCnt++;
                measurer.setPosition(nextPos);
            }
        }

        return lineCnt;
}

【问题讨论】:

    标签: java excel apache-poi


    【解决方案1】:

    只要该行不在合并区域内,如果高度未明确设置,它将自动高度。所以你需要将高度设置为未定义(-1)。见Row.setHeight

    为未定义/默认高度设置行高或设置为 ff (-1)。

    完整示例:

    来源Excel.xlsx

    代码:

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.ss.util.CellUtil;
    
    public class CreateExcelCellWrapText {
    
     public static void main(String[] args) throws Exception {
      Workbook workbook = WorkbookFactory.create(new FileInputStream("./Excel.xlsx"));
    
      Sheet sheet = workbook.getSheetAt(0);
      
      Row row = sheet.getRow(2);
      Cell cell = row.getCell(2); // cell C3
      cell.setCellValue("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua");
      CellUtil.setCellStyleProperty(cell, CellUtil.WRAP_TEXT, true); // make sure wrap text is set.
      row.setHeight((short)-1); // set row heigth undefined so it is auto heigth
    
      FileOutputStream out = new FileOutputStream("./ExcelNew.xlsx");
      workbook.write(out);
      out.close();
      workbook.close();
     }
    }
    

    结果ExcelNew.xlsx:

    如果您不知道哪些行必须是自动高度并且需要根据所需高度来决定,那么您需要根据单元格宽度、文本和使用的字体计算所需的高度。这是一项具有挑战性的任务。请参阅How to get the needed height of a multi line rich-text field (any font, any font size) having defined width using Java? 了解可能的解决方案。

    【讨论】:

    • 是的,我看到了这个决定。我只需要为内容超过当前高度的行设置它。如果行有 53 高度和文本“B1”,我不会改变它。我需要遍历所有行。
    • 所以你不知道哪些行必须是自动高度并且需要从所需的高度来决定?所以你需要计算所需的高度吗?这是一项具有挑战性的任务。但是请参阅stackoverflow.com/questions/45420801/…
    • 你的决定很有效。请您解释一下,这个魔法是如何发挥作用的?)为什么我们使用 72?如果我开发服务器应用程序,我需要怎么做? (72f/ppi)
    • @Andrew Sulyz :72/ppi 需要将px 转换为pt,因为我们需要pt 来设置Excel 中的高度。 pt 是一英寸点,一英寸有 72pt。 ppi 是每英寸屏幕分辨率 px。我的解决方案使用Swing JTextPane 来呈现文本。所以它需要一个GUI 在后台。
    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多