package com.estate.expert.admin.stat.report.render;
|
|
import com.deepoove.poi.util.TableTools;
|
import lombok.extern.log4j.Log4j2;
|
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
|
import org.apache.poi.xwpf.usermodel.XWPFTable;
|
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
|
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
|
|
import java.math.BigInteger;
|
|
/**
|
* 渲染帮助类
|
*/
|
@Log4j2
|
public class RenderHelper {
|
|
/**
|
* 渲染单元格
|
* @param row 行
|
* @param text 文本
|
*/
|
public static void renderCell(XWPFTableRow row, String text) {
|
var cell = row.createCell();
|
cell.getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
|
renderContentCell(cell, text, false, null);
|
}
|
|
/**
|
* 渲染单元格
|
* @param row 行
|
* @param text 文本
|
* @param textColor 文本颜色
|
*/
|
public static void renderCell(XWPFTableRow row, String text, String textColor) {
|
var cell = row.createCell();
|
cell.getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
|
renderContentCell(cell, text, false, textColor);
|
}
|
|
/**
|
* 渲染内容单元格
|
* @param cell 单元格
|
* @param text 文本
|
* @param isTitleCell 是否是标题单元格
|
*/
|
public static void renderContentCell(XWPFTableCell cell, String text, boolean isTitleCell) {
|
renderContentCell(cell, text, isTitleCell, null);
|
}
|
|
/**
|
* 渲染内容单元格
|
* @param cell 单元格
|
* @param text 文本
|
* @param isTitleCell 是否是标题单元格
|
* @param textColor 文本颜色
|
*/
|
public static void renderContentCell(XWPFTableCell cell, String text, boolean isTitleCell, String textColor){
|
var borders = cell.getCTTc().addNewTcPr().addNewTcBorders();
|
borders.addNewTop().setVal(STBorder.SINGLE);
|
borders.addNewBottom().setVal(STBorder.SINGLE);
|
borders.addNewLeft().setVal(STBorder.SINGLE);
|
borders.addNewRight().setVal(STBorder.SINGLE);
|
|
// 设置边框的颜色和宽度
|
borders.getTop().setColor("000000"); // 设置为黑色
|
borders.getTop().setSz(BigInteger.valueOf(4)); // 设置宽度为4
|
borders.getBottom().setColor("000000");
|
borders.getBottom().setSz(BigInteger.valueOf(4));
|
borders.getLeft().setColor("000000");
|
borders.getLeft().setSz(BigInteger.valueOf(4));
|
borders.getRight().setColor("000000");
|
borders.getRight().setSz(BigInteger.valueOf(4));
|
|
// 设置字体样式
|
var run = cell.getParagraphs().get(0).createRun();
|
run.setFontFamily("宋体"); // 设置字体为宋体
|
run.setFontSize(12); // 设置字体大小为小四(12磅)
|
run.setText(text);
|
|
if(textColor != null) {
|
run.setColor(textColor);
|
} else {
|
run.setColor("000000");
|
}
|
|
// 设置单元格的水平和垂直对齐方式
|
cell.getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
|
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
|
|
// 设置单元格的背景颜色
|
if(isTitleCell)
|
cell.setColor("D9D9D9");
|
}
|
|
/**
|
* 合并垂直单元格
|
* @param table 表格
|
* @param startRow 开始行
|
* @param col 列
|
*/
|
public static void mergeCellsVertically(XWPFTable table, int startRow, int col) {
|
int start = 0;
|
int size = table.getRows().size();
|
String content = table.getRow(startRow).getCell(col).getText();
|
for(int i = 1; i < size; i++){
|
var cell = table.getRow(i).getCell(col);
|
String cellText = cell.getText();
|
if(cellText.equals(content)){
|
continue;
|
}
|
if(i - start > 1) {
|
// log.info("Merge cells from {} to {}", start, i - 1);
|
TableTools.mergeCellsVertically(table, col, start, i - 1);
|
}
|
start = i;
|
content = cellText;
|
}
|
|
// Merge the last group of cells
|
if(size - start > 1){
|
// log.info("Merge cells from {} to {}", start, size - 1);
|
TableTools.mergeCellsVertically(table, col, start, size - 1);
|
}
|
}
|
}
|