当前位置: 首页 > 知识库问答 >
问题:

如何增加Excel行的高度使用Apache-POI合并单元格值大于单元格宽度?

唐弘益
2023-03-14

我正在使用java类创建一个大型excel。Excel包含一个存储字符串的合并单元格。字符串的长度非常大,我动态地得到这个字符串。我需要增加合并单元格的高度,使整个字符串适合该单元格。我试过使用“包装文本”,它包装文本,但不会增加合并单元格的高度,因为完整的字符串在excel中不可见。我使用的Java类是:

  1. XS SF工作簿
  2. XSSFSheet
  3. XSSFRow
  4. XSSFCell
  5. XS SF CellS类型

和其他必需的依赖类。有没有一种方法可以根据单元格值增加合并单元格的高度。

共有1个答案

华宇
2023-03-14

让您了解如何计算所需的行高。

我们可以使用Sheet.getColumnWidth(int)获得列的列宽(以字符宽度为单位)。但这并不真正准确,因为它只适用于数字字形:1,2,3,4,5,6,7,8,9,0。在true type字体中,有一些字形(,|,l,…)需要更少的宽度。因此,该结果将与文本中使用的宽度较小的图示符一样不准确。

我们可以将列宽(以字符为单位)校正一个因子。我使用5/4。但这在很大程度上取决于所使用的语言。

然后我们可以计算所需的行数,然后通过获得一行使用的默认行高并将其与所需的行数相乘来获得所需的行高。

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

public class CreateExcelCellWrapText {

 public static void main(String[] args) throws Exception {
  XSSFWorkbook workbook = new XSSFWorkbook();

  CellStyle cellstyle = workbook.createCellStyle();
  cellstyle.setWrapText(true);

  Sheet sheet = workbook.createSheet();

//__________________________not merged = height is auto sized

  Row row = sheet.createRow(0);

  Cell cell = row.createCell(2);
  cell.setCellValue("String cell content\nhaving line wrap.");
  cell.setCellStyle(cellstyle);

//__________________________merged = height is not auto sized

  row = sheet.createRow(2);

  cell = row.createCell(2);
  cell.setCellValue("String cell content\nhaving line wrap.");
  cell.setCellStyle(cellstyle);

  CellRangeAddress cellRangeAddress = new CellRangeAddress(2, 2, 2, 3);
  sheet.addMergedRegion(cellRangeAddress);

//__________________________merged with calculated height

  row = sheet.createRow(4);

  String text = "String cell content\nhaving line wrap.\nIt has new line marks and then a long text without new line marks.\nFollowed by short text part.\n\n\nGreetings";

  cell = row.createCell(2);
  cell.setCellValue(text);
  cell.setCellStyle(cellstyle);

  cellRangeAddress = new CellRangeAddress(4, 4, 2, 3);
  sheet.addMergedRegion(cellRangeAddress);

  //get the column width in character widths for the merged columns
  //this is not really accurate since it only is for number glyphs: 1,2,3,4,5,6,7,8,9,0
  //in true type fonts there are glyps (., |, l, ...) which need much less width
  int colwidthinchars = (sheet.getColumnWidth(2) + sheet.getColumnWidth(3)) / 256;
System.out.println(colwidthinchars);
  //correct the colwidthinchars by a factor 5/4 (highly dependent on the language used)
  colwidthinchars = Math.round(colwidthinchars * 5f/4f);
System.out.println(colwidthinchars);

  //calculate the needed rows dependent on the text and the column width in character widths
  String[] chars = text.split("");
  int neededrows = 1;
  int counter = 0;
  for (int i = 0; i < chars.length; i++) {
   counter++;
   if (counter == colwidthinchars) {
System.out.println("new line because of charcter count");
    neededrows++;
    counter = 0;
   } else if ("\n".equals(chars[i])) {
System.out.println("new line because of new line mark");
    neededrows++;
    counter = 0;
   }
  }

System.out.println(neededrows);

  //get default row height
  float defaultrowheight = sheet.getDefaultRowHeightInPoints();
System.out.println(defaultrowheight);

  row.setHeightInPoints(neededrows * defaultrowheight);

  workbook.write(new FileOutputStream("CreateExcelCellWrapText.xlsx"));
  workbook.close();
 }
}

这是因为使用了默认字体和字号。当使用不同的字体和不同的字体大小时,挑战增加到无穷大;-)。

请参阅如何使用Java获得具有定义宽度的多行富文本字段(任何字体、任何字体大小)所需的高度?例如在JTextPane中呈现格式化文本以获得首选高度的示例。

 类似资料:
  • 我正在使用Java和Apache POI创建一个带有表的word文档。 我可以很容易地创建表格,用不同的宽度设置每一列,然后合并单元格以产生所需的效果(见下面的图像)。然而,当我打开word文档时,一些单元格已经被调整,以便它们的边缘对齐。我发现,在表的开头添加一个额外的行并保留所有单元格不合并将保持其余行不变,但稍后使用table.removerow(0)删除该行;影响其余行。如果打开word文

  • 问题内容: 还有其他方法可以使用Apache POI库在Excel中合并单元格? 我正在尝试使用以下内容,但无法正常工作 问题答案: 您可以使用 该示例将从B2合并到E2。请记住,它是基于零的索引(例如POI版本3.12)。 有关详细信息,请参见《BusyDeveloper指南》。

  • 我试着用下面的方法,但它不起作用

  • 我要做一个垂直合并使用这个函数: 但我不能用类似的函数进行水平合并: 谢谢!

  • 我想数一下一行中有多少单元格被合并了?我在Excel中合并(缩放)它,在Java中我想显示例如:“合并的单元格:4”(A1,B1,C1,D1)?

  • 我正在构建一个函数,读取excel文件中可用的数据。 我在Spring项目中使用Apache POI。 但是,我在阅读合并单元格时遇到了困难。我需要的是得到如图所示的红色高亮显示的内容。