10,612
社区成员




Java poi 导出excel时,设置自动换行。之后获取行的高度,始终是一行的高度。 求解如何获取excel里显示的行高?
根据行内容重新计算行高
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());
}