【问题标题】:Auto size height for rows in Apache POIApache POI 中行的自动大小高度
【发布时间】:2013-10-02 19:51:53
【问题描述】:

我正在使用 Apache POI 将值输入到电子表格中。这些值有换行符,我能够成功使用这段代码:

CellStyle style = cell.getCellStyle()
style.setWrapText(true)
cell.setCellStyle(style)

不幸的是,虽然文本正确换行,但行的高度并不总是增长到足以显示内容的程度。如何确保我的行总是正确的高度?

【问题讨论】:

    标签: java excel apache-poi


    【解决方案1】:
    currentRow.setHeight((short)-1)
    

    适用于 XSSFCell 和 Excel 2013

    【讨论】:

    • 令人震惊的是,这也适用于 NPOI(.NET 实现)并在 Excel 2013 中正确打开。语法略有不同:row.Height = -1;
    • 此解决方案不适用于 Ubuntu 的 LibreOffice 版本 5.1.6.2 和 POI 3.17,但如果使用 MSExcel 打开文档则可以使用
    【解决方案2】:
    HSSFWorkbook workbook=new HSSFWorkbook();
    HSSFSheet sheet =  workbook.createSheet("FirstSheet");  
    HSSFRow rowhead=   sheet.createRow((short)0);
    HSSFCellStyle style = workbook.createCellStyle();
    style.setWrapText(true);
    row.setRowStyle(style);
    row.getCell(0).setCellStyle(style);
    

    上面的代码会生成动态的行高。

    【讨论】:

    • style.setWrapText(true) 就足够了。谢谢!
    • Eee...不,这不起作用。至少不是在带有 Libre Office 的 Ubuntu Linux 上
    • @ed22,对我也不起作用。相同的环境。
    • 这是与 Libre Office 相关的问题。电子表格在 MS Excel 中正确打开
    • 我还必须使用 Libre Office,但这并不能解决行高问题。有什么方法可以让 Libre Office 显示更好的行高?我有一个字体稍大的单元格。 Libre Office 不会自动增加行高,而 MS Office 和 Open Office 可以很好地增加行高。
    【解决方案3】:

    我让它工作的唯一方法是编写自己的实现来计算行高。该代码现在作为Taro 项目发布,因此您可以使用它。它有许多方便的方法,让您用更少的代码行编写 Excel 文件。

    如果您更愿意将实现放在您自己的代码中,您可以在SpreadsheetTab 类中找到它。中间有一个 autoSizeRow(int rowIndex) 方法。它基本上遍历行并为每个单元格找到文本行数,然后使用字体大小来计算最佳单元格高度。然后它将行高设置为最高单元格的高度。

    【讨论】:

    • 注意:这仅适用于其值为 \n 的单元格。您必须自行决定将\n 放置在何处。如果没有,该行将有一个行高
    【解决方案4】:

    查看全部this link,它提供了一些代码来根据列宽和单元格内容手动计算行的正确高度。我没有亲自测试过。为了方便也贴在下面:

    // Create Font object with Font attribute (e.g. Font family, Font size, etc) for calculation
    java.awt.Font currFont = new java.awt.Font(fontName, 0, fontSize);
    AttributedString attrStr = new AttributedString(cellValue);
    attrStr.addAttribute(TextAttribute.FONT, currFont);
    
    // Use LineBreakMeasurer to count number of lines needed for the text
    FontRenderContext frc = new FontRenderContext(null, true, true);
    LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc);
    int nextPos = 0;
    int lineCnt = 0;
    while (measurer.getPosition() < cellValue.length())
    {
        nextPos = measurer.nextOffset(mergedCellWidth); // mergedCellWidth is the max width of each line
        lineCnt++;
        measurer.setPosition(nextPos);
    }
    
    Row currRow = currSht.getRow(rowNum);
    currRow.setHeight((short)(currRow.getHeight() * lineCnt));
    
    // The above solution doesn't handle the newline character, i.e. "\n", and only
    // tested under horizontal merged cells.
    

    【讨论】:

    • 最后一行必须使用:row.getHeightInPoints() 或 currSht.getDefaultRowHeightInPoints() 而不是 "getHeight()"
    【解决方案5】:
    cell.getRow().setHeight((short) -1);
    

    在 apache poi 3.9 或更高版本中为 HSSFCell 工作

    【讨论】:

      【解决方案6】:

      它适用于 Excel 2010。 我将单元格长度限制为 50 个字符

          Row row = sheet.createRow(0);
          CellStyle style = workbook.createCellStyle();
          style.setWrapText(true);
          if (data.length() > 50) {
              for (int i = 1; i <= Math.abs(data.length() / 50); i++) {
                  data = data.substring(0, i * 50) + "\n" + data.substring(i * 50);
              }
              Cell cell = row.createCell(0);
              row.setRowStyle(style);
              cell.setCellStyle(style);
              cell.setCellValue(data);
              sheet.autoSizeColumn(0);
          }
      

      【讨论】:

      • 所以没有自动高度
      【解决方案7】:

      您无法直接调整单元格高度。 但是你可以改变行高

      final HSSFSheet fs = wb.createSheet("sheet1");
      final HSSFRow row0 = fs.createRow(0);
      final HSSFCell cellA1 = row0.createCell(0);
      row0.setHeight((short)700);
      

      【讨论】:

      • 这不是自动调整大小(auto-size),是手动调整大小。
      【解决方案8】:

      就我而言,一个可靠的解决方案是计算行数并将行高设置为默认行高的倍数:

      int numberOfLines = cell.getStringCellValue().split("\n").length;
      row.setHeightInPoints(numberOfLines*sheet.getDefaultRowHeightInPoints());
      

      【讨论】:

        【解决方案9】:

        行aitosize为我工作:

        cell.getRow().setHeight((short)0);
        

        这里0 用于计算自动高度。

        【讨论】:

        • 这似乎不起作用。当我在 Excel (2010) 中打开时,该行的高度为零。
        • @GreenGiant,我不使用 Windows 和 Excel。此解决方案适用于 OpenOffice。此外,在 Excel 中为merged 单元格自动设置行高也存在问题(need macro 用于修复它)。
        • 可悲的是,这将设置为零高度,也就是不可见
        【解决方案10】:

        “LibreOffice Calc”和“WPS 电子表格”的解决方法,合并销售具有自动高度。

        我在主文档的右侧添加一列(在我的例子中是 32 列) 将宽度设置为具有相同文本的所有合并单元格。 将样式 WrapText 设置为 true 将样式设置为上对齐 复制将在合并单元格中显示的内容 将该列设置为隐藏 设置行高 = -1

        代码示例:

           private void applyRowHightWorkaroundForMergedCells(HSSFCell cell0) {
               HSSFSheet sheet = cell0.getSheet();
               HSSFRow row = cell0.getRow();
            
               String value = cell0.getStringCellValue();
               HSSFCell cell = row.createCell(32);
               sheet.setColumnWidth(32, 32000);
               cell.getCellStyle().setWrapText(true);
               cell.getCellStyle().setVerticalAlignment(VerticalAlignment.TOP);
               cell.setCellValue(value);
               sheet.setColumnHidden(32, true);
               row.setHeight((short) -1);
           }
        

        【讨论】:

          【解决方案11】:

          //我们可以使用表格的列宽

          Ex: sheet.setColumnWidth(0, 2000);
          

          【讨论】:

          • OP 要求调整行高而不是列宽。
          猜你喜欢
          • 2021-06-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多