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?
不幸的是,虽然文本正在正确地包装,但是行并不总是增长到足以显示内容的高度。如何确保行始终保持正确的高度?
8 个解决方案
#1
15
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.
上面的代码将生成行的动态高度。
#2
13
currentRow.setHeight((short)-1)
Works for XSSFCell and Excel 2013
为XSSFCell和Excel 2013工作
#3
5
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 Taro project, 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 SpreadsheetTab class. 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)方法。它基本上遍历行,对每个单元格查找文本行数,然后使用字体大小计算最优单元格高度。然后,它将行高度设置为最高单元格的高度。
#4
4
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.
#5
3
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);
#6
0
Row aitosize work for me:
行aitosize为我工作:
cell.getRow().setHeight((short)0);
Here 0
for calculate autoheight.
这里0表示自动高度。
#7
-1
short getRowHeight(Row row, String cellValue, Font font, float cellWidth) {
if(cellValue.isEmpty())
return row.getHeight();
// Create Font object with Font attribute (e.g. Font family, Font size, etc) for calculation
int style = java.awt.Font.PLAIN;
if(font.getBoldweight() == Font.BOLDWEIGHT_BOLD)
style = java.awt.Font.BOLD;
java.awt.Font currFont = new java.awt.Font(font.getFontName(), style, font.getFontHeight());
double requiredWidth = currFont.getStringBounds(cellValue, new FontRenderContext(currFont.getTransform(), false, false)).getBounds().getWidth();
return (short) Math.ceil(font.getFontHeight() * requiredWidth / cellWidth);
// The above solution doesn't handle the newline character, i.e. "\n", and only for row having merged cells.
}
#8
-7
//we can use column width for sheet
//我们可以用列宽来表示
Ex: sheet.setColumnWidth(0, 2000);
#1
15
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.
上面的代码将生成行的动态高度。
#2
13
currentRow.setHeight((short)-1)
Works for XSSFCell and Excel 2013
为XSSFCell和Excel 2013工作
#3
5
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 Taro project, 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 SpreadsheetTab class. 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)方法。它基本上遍历行,对每个单元格查找文本行数,然后使用字体大小计算最优单元格高度。然后,它将行高度设置为最高单元格的高度。
#4
4
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.
#5
3
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);
#6
0
Row aitosize work for me:
行aitosize为我工作:
cell.getRow().setHeight((short)0);
Here 0
for calculate autoheight.
这里0表示自动高度。
#7
-1
short getRowHeight(Row row, String cellValue, Font font, float cellWidth) {
if(cellValue.isEmpty())
return row.getHeight();
// Create Font object with Font attribute (e.g. Font family, Font size, etc) for calculation
int style = java.awt.Font.PLAIN;
if(font.getBoldweight() == Font.BOLDWEIGHT_BOLD)
style = java.awt.Font.BOLD;
java.awt.Font currFont = new java.awt.Font(font.getFontName(), style, font.getFontHeight());
double requiredWidth = currFont.getStringBounds(cellValue, new FontRenderContext(currFont.getTransform(), false, false)).getBounds().getWidth();
return (short) Math.ceil(font.getFontHeight() * requiredWidth / cellWidth);
// The above solution doesn't handle the newline character, i.e. "\n", and only for row having merged cells.
}
#8
-7
//we can use column width for sheet
//我们可以用列宽来表示
Ex: sheet.setColumnWidth(0, 2000);