使用poi 将excel内容复制到word中生成table后的单元格样式同步工具

本文介绍了一种将Excel单元格的字体样式、颜色、位置等属性同步到Word文档中的过程,涉及字体大小、加粗、下划线以及背景色、宽度和高度的调整,使用了Java库中的XSSF和XWPF对象来操作单元格样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

样式同步工具包括: 单元格字体样式 和 单元格样式的同步

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);
                    }
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小花客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值