Java Apache POI 中行的自动调整高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19145628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:30:02  来源:igfitidea点击:

Auto size height for rows in Apache POI

javaexcelapache-poi

提问by user1007895

I am inputting values into a spreadsheet using Apache POI. These values have newlines, and I was able to use this code successfully:

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

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

Unfortunately, while the text is wrapping correctly, the rows are not always growing in height enough to show the content. How do I ensure that my rows are always the correct height?

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

回答by swamy

//we can use column width for sheet

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

Ex: sheet.setColumnWidth(0, 2000);

回答by JMB

The only way I got this to work was write my own implementation to calculate the row height. The code is now released as the Taroproject, so you could use that. It has numerous convenience methods to let you write an Excel file in far fewer lines of code.

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

If you prefer to put the implementation in your own code, you can find it in the SpreadsheetTabclass. There is an autoSizeRow(int rowIndex) method half way down. It basically iterates down the row and for each cell finds the number of lines of text, then uses the font size to calculate the optimal cell height. It then sets the row height to the height of the tallest cell.

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

回答by Arthur

You can't adjust cell height directly. But you can change the row's height

您不能直接调整单元格高度。但是你可以改变行的高度

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

回答by Michael Kazarian

Row aitosize work for me:

Row aitosize 为我工作:

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

Here 0for calculate autoheight.

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

回答by GreenGiant

See all this link, which provides some code to manually calculate the correct height for a row, based on the column width and cell content. I've not personally tested it. Also pasted below for convenience:

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

// 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.

回答by Vinil Vijayan

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);

The above code will generate dynamic height of rows.

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

回答by Franz Frühwirth

currentRow.setHeight((short)-1)

Works for XSSFCell and Excel 2013

适用于 XSSFCell 和 Excel 2013

回答by Miller Cy Chan

cell.getRow().setHeight((short) -1);

Worked for HSSFCell in apache poi 3.9 or above

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

回答by Valery Tarasenko

It works in Excel 2010. I set the limit of cell length of 50 characters

它适用于 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);
    }