Browse Source

feat: 增加表格单元格自适应计算高度方法,优化巡视报告单元格文本展示不全的问题

master
yinhuaiwei 1 month ago
parent
commit
ccb345a489
1 changed files with 49 additions and 0 deletions
  1. +49
    -0
      inspect-main/inspect-main-task/src/main/java/com/inspect/resultmain/controller/PatrolTaskResultMainController.java

+ 49
- 0
inspect-main/inspect-main-task/src/main/java/com/inspect/resultmain/controller/PatrolTaskResultMainController.java View File

@ -3293,12 +3293,14 @@ public class PatrolTaskResultMainController extends BaseController {
Cell cell62 = row6.createCell(4);
cell62.setCellValue(inspectionReport.getInspectionTaskName());
cell62.setCellStyle(colStyle2);
autoFitRowHeight(sheet, 4, 4, colStyle2);
Cell cell63 = row6.createCell(8);
cell63.setCellValue(MessageUtils.get("巡视统计"));
cell63.setCellStyle(colStyle2);
Cell cell64 = row6.createCell(12);
cell64.setCellValue(inspectionReport.getPatrolStatistics());
cell64.setCellStyle(colStyle2);
autoFitRowHeight(sheet, 4, 12, colStyle2);
for (int i = 0; i < 15; ++i) {
if (i != 0 && i != 4 && i != 8 && i != 12) {
@ -3743,6 +3745,53 @@ public class PatrolTaskResultMainController extends BaseController {
return outputStream.toByteArray();
}
/**
* 自适应行高手动计算
* @param sheet
* @param rowNum 行号
* @param cellNum 列号
* @param style 样式
*/
public void autoFitRowHeight(Sheet sheet, int rowNum, int cellNum, CellStyle style) {
Row row = sheet.getRow(rowNum);
if (row == null) {
return;
}
Cell cell = row.getCell(cellNum);
if (cell == null) {
return;
}
String text = cell.getStringCellValue();
if (text == null || text.isEmpty()) {
return;
}
int columnWidth = sheet.getColumnWidth(0);
double widthInChars = columnWidth / 256.0;
// 获取字体
Font font = sheet.getWorkbook().getFontAt(style.getFontIndex());
double fontHeightInPoints = font.getFontHeightInPoints();
// 计算每行可容纳字符数近似公式
// 粗略估计
double charsPerLine = widthInChars * 1.0;
// 按换行符分割 + 按列宽折行
String[] lines = text.split("\n");
int totalLines = 0;
for (String line : lines) {
int lineChars = line.length();
int linesNeeded = (int) Math.ceil(lineChars / charsPerLine);
totalLines += Math.max(1, linesNeeded);
}
// 设置行高1 15 POI points
double rowHeightInPoints = totalLines * fontHeightInPoints;
row.setHeightInPoints((float) Math.max(rowHeightInPoints, row.getHeightInPoints()));
}
@GetMapping({"/test"})
public AjaxResult test(int count) {
String strs = "menglei/2.jpg,menglei/1123.jpg";


Loading…
Cancel
Save