[英]How to increase the height of the excel row using Apache-POI having merged cell value greater than the cell width?
我正在使用Java類創建一個很大的Excel。 Excel包含一個存儲字符串的合並單元格。 字符串的長度非常大,我正在動態獲取此字符串。 我需要增加合並單元格的高度,以使完整的字符串適合該單元格。 我嘗試使用“自動換行”,它可以換行,但是不會增加合並單元格的高度,因為在Excel中看不到完整的字符串。 我正在使用的Java類是:
和其他必需的依賴類。 有沒有一種方法可以根據單元格值增加合並單元格的高度。
讓您了解如何計算所需的行高的挑戰。
我們可以使用Sheet.getColumnWidth(int)獲得以字符寬度表示的列寬度。 但這並不是真的准確,因為它僅適用於數字字形:1、2、3、4、5、6、7、8、9、0。 在真型字體中,有一些字形(。,|,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
渲染格式化的文本以獲取首選高度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.