我正在使用Apache POI将值输入电子表格。这些值具有换行符,并且我已成功地使用以下代码:
CellStyle style = cell.getCellStyle()
style.setWrapText(true)
cell.setCellStyle(style)
很遗憾,虽然文本的换行是正确的,但有时行高不足以显示全部内容。我该如何确保行高始终正确?
我正在使用Apache POI将值输入电子表格。这些值具有换行符,并且我已成功地使用以下代码:
CellStyle style = cell.getCellStyle()
style.setWrapText(true)
cell.setCellStyle(style)
很遗憾,虽然文本的换行是正确的,但有时行高不足以显示全部内容。我该如何确保行高始终正确?
currentRow.setHeight((short)-1)
适用于XSSFCell和Excel 2013
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)
就足够了。谢谢! - Rubens Mariuzzo// 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.
int numberOfLines = cell.getStringCellValue().split("\n").length;
row.setHeightInPoints(numberOfLines*sheet.getDefaultRowHeightInPoints());
cell.getRow().setHeight((short) -1);
适用于Apache POI 3.9或更高版本中的HSSFCell
它适用于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);
}
你不能直接调整单元格的高度。 但是你可以改变行的高度。
final HSSFSheet fs = wb.createSheet("sheet1");
final HSSFRow row0 = fs.createRow(0);
final HSSFCell cellA1 = row0.createCell(0);
row0.setHeight((short)700);
对我来说,行自适应大小很有效:
cell.getRow().setHeight((short)0);
这里的0
用于计算自动高度。
解决“LibreOffice Calc”和“WPS Spreadsheet”合并单元格自动调整行高的问题。
在主文档右侧添加一列(在我的情况下是第32列)。 将所有具有相同文本的合并单元格设置为相同的宽度。 将样式WrapText设置为true。 将样式设置为Align Top。 复制将显示在合并单元格中的内容。 将该列设置为隐藏。 将行高设置为-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);
}
row.Height = -1;
- Tyler Forsythe