Java 如何使用合并单元格值大于单元格宽度的Apache POI增加excel行的高度?
我正在使用java类创建一个大型excel。Excel包含存储字符串的合并单元格。字符串的长度非常大,我动态地得到这个字符串。我需要增加合并单元格的高度,以便完整的字符串适合该单元格。我尝试过使用“包装文本”,它包装文本,但不会增加合并单元格的高度,因为完整的字符串在excel中不可见。 我使用的Java类有:Java 如何使用合并单元格值大于单元格宽度的Apache POI增加excel行的高度?,java,algorithm,apache-poi,Java,Algorithm,Apache Poi,我正在使用java类创建一个大型excel。Excel包含存储字符串的合并单元格。字符串的长度非常大,我动态地得到这个字符串。我需要增加合并单元格的高度,以便完整的字符串适合该单元格。我尝试过使用“包装文本”,它包装文本,但不会增加合并单元格的高度,因为完整的字符串在excel中不可见。 我使用的Java类有: XSSF工作簿 XSSFSheet XSSFRow XSSFCell XSSFCellStype 和其他必需的依赖类。 有没有办法根据单元格值增加合并单元格的高度 让您了解如何计算所需的
有没有办法根据单元格值增加合并单元格的高度 让您了解如何计算所需的行高 我们可以使用获取列的列宽(字符宽度)。但这并不准确,因为它只适用于数字符号: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();
}
}
import java.io.FileOutputStream;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.apache.poi.ss.util.CellRangeAddress;
公共类CreateExcelCellWrapText{
公共静态void main(字符串[]args)引发异常{
XSSFWorkbook工作簿=新XSSFWorkbook();
CellStyle CellStyle=workbook.createCellStyle();
cellstyle.setWrapText(true);
工作表=工作簿.createSheet();
//__________________________未合并=高度自动调整大小
Row Row=sheet.createRow(0);
Cell Cell=row.createCell(2);
cell.setCellValue(“字符串单元格内容\n带换行符”);
cell.setCellStyle(cellstyle);
//__________________________合并=高度未自动调整大小
行=工作表。创建行(2);
cell=row.createCell(2);
cell.setCellValue(“字符串单元格内容\n带换行符”);
cell.setCellStyle(cellstyle);
CellRangeAddress CellRangeAddress=新的CellRangeAddress(2,2,2,3);
表.地址合并区域(cellRangeAddress);
//__________________________与计算高度合并
行=表。创建行(4);
String text=“字符串单元格内容\n换行。\n它有新行标记,然后是没有新行标记的长文本。\n有短文本部分。\n\n\n换行标记”;
cell=row.createCell(2);
cell.setCellValue(文本);
cell.setCellStyle(cellstyle);
cellRangeAddress=新的cellRangeAddress(4,4,2,3);
表.地址合并区域(cellRangeAddress);
//获取合并列的列宽(以字符宽度为单位)
//这并不准确,因为它只适用于数字图示符:1,2,3,4,5,6,7,8,9,0
//在真字型字体中,有glyps(,|,l,…)需要更少的宽度
int colwidthinchars=(sheet.getColumnWidth(2)+sheet.getColumnWidth(3))/256;
System.out.println(colwidthinchars);
//按系数5/4校正colwidthinchars(高度依赖于所使用的语言)
colwidthinchars=数学圆(colwidthinchars*5f/4f);
System.out.println(colwidthinchars);
//根据文本和列宽(字符宽度)计算所需的行
字符串[]字符=text.split(“”);
int neededrows=1;
int计数器=0;
for(int i=0;i
这是因为使用了默认字体和字体大小。当使用不同的字体和不同的字体大小时,挑战增加到无穷大;-)
有关在
JTextPane
中呈现格式化文本以获得首选高度的示例,请参见。了解如何计算所需行高度的挑战
我们可以使用获取列的列宽(字符宽度)。但这并不准确,因为它只适用于数字符号: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();
}
}
import java.io.FileOutputStream;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.apache.poi.ss.util.CellRangeAddress;
公共类CreateExcelCellWrapText{
公共静态void main(字符串[]args)引发异常{
XSSFWorkbook工作簿=新XSSFWorkbook();
CellStyle CellStyle=workbook.createCellStyle();
cellstyle.setWrapText(true);
工作表=工作簿.createSheet();
//__________________________未合并=高度自动调整大小
Row Row=sheet.createRow(0);
Cell Cell=row.createCell(2);
cell.setCellValue(“字符串单元格内容\n带换行符”);
cell.setCellStyle(cellstyle);
//_