样式同步工具包括: 单元格字体样式 和 单元格样式的同步
1.单元格中的字体样式
1.1 单元格中的字体:大小、颜色、位置、加粗、下划线...
2.单元格背景色、宽度、高度、线框........
2.1 单元格宽度为从excel读取过来并且按word的工作宽度进行缩放
2.2 单元格高度则为excel读取的高度
/**
* 字体位置映射集合
*/
private static final Map<String, STVerticalJc.Enum> STVERTICALJC_MAP = new HashMap<>();
private static final Map<String, STJc.Enum> STJC_MAP = new HashMap<>();
static {
STVERTICALJC_MAP.put("center", STVerticalJc.CENTER);
STVERTICALJC_MAP.put("top", STVerticalJc.TOP);
STVERTICALJC_MAP.put("both", STVerticalJc.BOTH);
STVERTICALJC_MAP.put("bottom", STVerticalJc.BOTTOM);
STJC_MAP.put("center", STJc.CENTER);
STJC_MAP.put("both", STJc.BOTH);
STJC_MAP.put("mediumkashida", STJc.MEDIUM_KASHIDA);
STJC_MAP.put("distribute", STJc.DISTRIBUTE);
STJC_MAP.put("numTab", STJc.NUM_TAB);
STJC_MAP.put("highkashida", STJc.HIGH_KASHIDA);
STJC_MAP.put("lowkashida", STJc.LOW_KASHIDA);
STJC_MAP.put("thaidistribute", STJc.THAI_DISTRIBUTE);
STJC_MAP.put("left", STJc.LEFT);
STJC_MAP.put("right", STJc.RIGHT);
}
public static void cellStyle(XSSFWorkbook workbook, XSSFSheet sheet, XWPFTable table) {
// 前置拦截
if (null == workbook || null == sheet || null == table) {
return;
}
// 遍历变表格
for (int i = 0; i < table.getRows().size(); i++) {
for (int j = 0; j < table.getRow(i).getTableCells().size(); j++) {
// excel的单元格为空跳过
if (null == sheet.getRow(i) || null == sheet.getRow(i).getCell(j)) {
continue;
}
XSSFCell excelCell = sheet.getRow(i).getCell(j);
// word table 单元格
XWPFTableCell cell = table.getRow(i).getCell(j);
List<XWPFRun> runs = cell.getParagraphArray(0).getRuns();
if (CollectionUtils.isEmpty(runs)) {
continue;
}
// 设置字体格式
XWPFRun xwpfRun = runs.get(0);
XSSFFont font = workbook.getFontAt(excelCell.getCellStyle().getFontIndex());//获取字体
xwpfRun.setFontSize(font.getFontHeightInPoints()); //设置字体大小
xwpfRun.setFontFamily(font.getFontName()); //设置字体
xwpfRun.setBold(font.getBold()); //设置加粗
xwpfRun.setItalic(font.getItalic()); // 斜体
xwpfRun.setUnderline(UnderlinePatterns.valueOf(font.getUnderline()));//下划线
XSSFColor xssffont = font.getXSSFColor();
byte[] rgb;
if (xssffont != null) {
rgb = xssffont.getRGB(); //得到rgb的byte数组
xwpfRun.setColor(String.format("%02X%02X%02X", rgb[0], rgb[1], rgb[2])); // 设置字体颜色
}
}
}
List<CTRow> trList = table.getCTTbl().getTrList();// 获取表格的所有行
// excel的所有列的宽度
Row row11 = sheet.getRow(0);
int count = 0;
for (int i2 = 0; i2 < row11.getLastCellNum(); i2++) {
count += sheet.getColumnWidth(i2);
}
// 遍历所有的表格行
for (int i1 = 0; i1 < trList.size(); i1++) {
Row row = sheet.getRow(i1);
List<CTTc> tcList = trList.get(i1).getTcList();// 获取当前行的所有列
// 单元格高度
short height = sheet.getRow(i1).getHeight();
CTRow ctRow = trList.get(i1);
if (null != ctRow){
CTHeight ht = ctRow.addNewTrPr().addNewTrHeight();
ht.setVal(BigInteger.valueOf(height));
ht.setHRule(STHeightRule.EXACT);
}
// 遍历表格当前行所有列
for (int i2 = 0; i2 < tcList.size(); i2++) {
// 设置每列宽度
int columnWidth = 0;
if (null != row) {
Cell cell = row.getCell(i2);
if (null != cell) {
columnWidth = sheet.getColumnWidth(i2);
}
Cell cell1 = row.getCell(i2);
if (null != cell1) {
CTTcPr ctPr = tcList.get(i2).addNewTcPr();
// 填充背景颜色
XSSFColor fillForegroundColorColor = (XSSFColor) cell1.getCellStyle().getFillForegroundColorColor();
if (null != fillForegroundColorColor) {
byte[] bytes;
bytes = fillForegroundColorColor.getRGB();
ctPr.addNewShd().setFill(String.format("%02X%02X%02X", bytes[0], bytes[1], bytes[2]));
}
// 设置字体位置
String alignment = cell1.getCellStyle().getAlignment().toString();
String verticalAlignment = cell1.getCellStyle().getVerticalAlignment().toString();
if (null != STVERTICALJC_MAP.get(verticalAlignment.toLowerCase())) {
ctPr.addNewVAlign().setVal(STVERTICALJC_MAP.get(verticalAlignment.toLowerCase()));
}
if (null != STJC_MAP.get(alignment.toLowerCase())) {
tcList.get(i2).getPList().get(0).addNewPPr().addNewJc().setVal(STJC_MAP.get(alignment.toLowerCase()));
}
// 设置单元格宽度
CTTcPr tcPr = tcList.get(i2).getTcPr();
if (null == tcPr) {
continue;
}
CTTblWidth ctTblWidth = tcPr.addNewTcW();
// word的工作范围为18.7 cm
BigDecimal temp = new BigDecimal(String.valueOf(18.7 * 567)).divide(new BigDecimal(String.valueOf(count)), 20, RoundingMode.UP);
BigDecimal w= new BigDecimal(String.valueOf(columnWidth)).multiply(temp).setScale(0, RoundingMode.UP);
ctTblWidth.setW(w);
ctTblWidth.setType(STTblWidth.DXA);
}
}
}
}
}