Java poi 根据单元格内容动态计算行高【宽度固定】
具体方法
public static float calcAndSetRowHeight(HSSFRow contentRow, String cellContent, int cellWidth) {
double totalRows = 0;
String[] arr = cellContent.split("\n");
for (int i = 0; i < arr.length; i++) {
String s = arr[i];
int totalCount = 0;
for(int j=0;j<s.length();j++){
String b=Character.toString(s.charAt(j));
if (b.getBytes().length == 1) {
totalCount++;
} else {
totalCount = totalCount + 2;
}
}
double l = totalCount * 256;
if (l < cellWidth) {
l = cellWidth;
}
double stringNeedsRows = Math.ceil(l / cellWidth);
totalRows += stringNeedsRows;
}
return (float) ((totalRows + 2) * contentRow.getHeightInPoints());
}