简体   繁体   English

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

[英]How to increase the height of the excel row using Apache-POI having merged cell value greater than the cell width?

I am creating an big excel using java class. 我正在使用Java类创建一个很大的Excel。 Excel contains a merged cell which store string. Excel包含一个存储字符串的合并单元格。 Length of the string is very large, i am getting this string dynamically. 字符串的长度非常大,我正在动态获取此字符串。 I need to increase the height of the merged cell so that complete string will fit to that cell. 我需要增加合并单元格的高度,以使完整的字符串适合该单元格。 I have tried using "wrap text", it wrap the text but doesn't increase the height of the merged cell due to which complete string is not visible in excel. 我尝试使用“自动换行”,它可以换行,但是不会增加合并单元格的高度,因为在Excel中看不到完整的字符串。 Java class i am using are: 我正在使用的Java类是:

  1. XSSFWorkbook XSSF工作簿
  2. XSSFSheet XSSFSheet
  3. XSSFRow XSS流
  4. XSSFCell XSSFCell
  5. XSSFCellStype XSSFCellStype

And other required dependent classes. 和其他必需的依赖类。 Is there a way how i can increase the height of the merge cell as per the cell value. 有没有一种方法可以根据单元格值增加合并单元格的高度。

To give you an idea of the challenge how to calculate the needed row height. 让您了解如何计算所需的行高的挑战。

We can get the column width in character widths for the columns using Sheet.getColumnWidth(int) . 我们可以使用Sheet.getColumnWidth(int)获得以字符宽度表示的列宽度。 But this is not really accurate since it only is for number glyphs: 1,2,3,4,5,6,7,8,9,0. 但这并不是真的准确,因为它仅适用于数字字形:1、2、3、4、5、6、7、8、9、0。 In true type fonts there are glyphs (., |, l, ...) which need much less width. 在真型字体中,有一些字形(。,|,l,...)需要的宽度要少得多。 So this result will be as much more inaccurate as such less-width-glyphs are used in the text. 因此,与文本中使用的宽度较小的字形相比,此结果将更加不准确。

We might correct the column-width-in-chars by a factor. 我们可能会以一定的系数来校正字符列宽度。 I use 5/4. 我用5/4。 But this is highly dependent on the language used. 但这在很大程度上取决于所使用的语言。

Then we can calculate the needed row count and then get the needed row height by getting the default row height used for one line and multiply that with the needed row count . 然后,我们可以计算所需的行数,然后通过获取用于一行的默认行高并将其乘以所需的行数来获得所需的行高。

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

This works because default font and font size is used. 这是有效的,因为使用了默认字体和字体大小。 The challenge increases up to infinity when different fonts and different font sizes are used ;-). 当使用不同的字体和不同的字体大小时,挑战增加到无穷大。

See How to get the needed height of a multi line rich-text field (any font, any font size) having defined width using Java? 请参阅如何使用Java获取具有定义宽度的多行富文本字段(任何字体,任何字体大小)的所需高度? for an example rendering the formatted text in a JTextPane to get the preferred height. 例如,在JTextPane渲染格式化的文本以获取首选高度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过apache-poi在Excel中更改单元格的值 - Changing value of cell in Excel via apache-poi Java Apache-POI nullpointer异常,有关在现有行中创建单元格 - Java Apache-POI nullpointer exception on creating a cell in an existing row 在Java中使用Apache Poi的Excel工作表合并单元格读取 - Excel sheet merged cell reading using apache poi in java Apache-POI-如何在Cell-Editor中像Excel一样四舍五入数字 - Apache-POI - How do i round numbers like Excel does in Cell-Editor 如何在apache-poi的新版本中为单个单元格设置Excel单元格前景色? - How to set an Excel Cell foreground color for individual cells in the new version for apache-poi? 如何使用Apache POI在Excel中添加公式(单元格的动态值) - How Add a formula(Dynamic value of cell) in Excel Using Apache POI 如何使用apache-poi 3.9修改pptx文件中表格的单元格值? - How to modify the cell value of a table in an pptx file with apache-poi 3.9? 如何使用 Apache-POI 获取 Excel 工作表的默认列宽? - How to get the default column width of an Excel sheet with Apache-POI? 如何使用apache poi确定合并单元格中的行数? - How to determine number of rows in a merged cell using apache poi? 使用 Apache-POI 库获取单元格内容时,我得到“无法从文本单元格获取数值”和相反的结果。 我如何解决它? - When getting cell content using Apache-POI Library, I get both “Cannot get a numeric value from a text cell” and the reverse of that. How do I fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM