Use poi to copy the excel sheet page content into word to generate table cell content data copy tool: (font size, color, position, bold, underline… cell background color, width, height)

Styles include:

1. Fonts in cells
1.1 Fonts in cells: size, color, position, bold, underline…
2. Cell background color, width, height, wireframe…
2.1 The cell width is read from excel and scaled according to the working width of word
2.2 The cell height is the height read by excel
 /**
     * Font position mapping collection
     */
    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) {
        // Pre-interception
        if (null == workbook || null == sheet || null == table) {
            return;
        }
        // Traverse the variable table
        for (int i = 0; i < table.getRows().size(); i + + ) {
            for (int j = 0; j < table.getRow(i).getTableCells().size(); j + + ) {
                // The cells in excel are skipped if they are empty.
                if (null == sheet.getRow(i) || null == sheet.getRow(i).getCell(j)) {
                    continue;
                }
                XSSFCell excelCell = sheet.getRow(i).getCell(j);
                // word table cell
                XWPFTableCell cell = table.getRow(i).getCell(j);
                List<XWPFRun> runs = cell.getParagraphArray(0).getRuns();
                if (CollectionUtils.isEmpty(runs)) {
                    continue;
                }
                //Set font format
                XWPFRun xwpfRun = runs.get(0);
                XSSFFont font = workbook.getFontAt(excelCell.getCellStyle().getFontIndex());//Get the font
                xwpfRun.setFontSize(font.getFontHeightInPoints()); //Set the font size
                xwpfRun.setFontFamily(font.getFontName()); //Set the font
                xwpfRun.setBold(font.getBold()); //Set bold
                xwpfRun.setItalic(font.getItalic()); // italic
                xwpfRun.setUnderline(UnderlinePatterns.valueOf(font.getUnderline()));//underline
                XSSFColor xssffont = font.getXSSFColor();
                byte[] rgb;
                if (xssffont != null) {
                    rgb = xssffont.getRGB(); //Get the byte array of rgb
                    xwpfRun.setColor(String.format(" X X X", rgb[0], rgb[1], rgb[2])); // Set font color
                }
            }
        }


        List<CTRow> trList = table.getCTTbl().getTrList(); // Get all rows of the table
        //The width of all columns in excel
        Row row11 = sheet.getRow(0);
        int count = 0;
        for (int i2 = 0; i2 < row11.getLastCellNum(); i2 + + ) {
            count + = sheet.getColumnWidth(i2);
        }

        //Loop through all table rows
        for (int i1 = 0; i1 < trList.size(); i1 + + ) {
            Row row = sheet.getRow(i1);
            List<CTTc> tcList = trList.get(i1).getTcList(); // Get all columns of the current row
            // cell height
           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);
            }

            // Traverse all columns of the current row of the table
            for (int i2 = 0; i2 < tcList.size(); i2 + + ) {
                //Set the width of each column
                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();
                        //Fill background color
                        XSSFColor fillForegroundColorColor = (XSSFColor) cell1.getCellStyle().getFillForegroundColorColor();
                        if (null != fillForegroundColorColor) {
                            byte[] bytes;
                            bytes = fillForegroundColorColor.getRGB();
                            ctPr.addNewShd().setFill(String.format(" X X X", bytes[0], bytes[1], bytes[2]));
                        }

                        //Set font position
                        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()));
                        }

                        //Set cell width
                        CTTcPr tcPr = tcList.get(i2).getTcPr();
                        if (null == tcPr) {
                            continue;
                        }
                        CTTblWidth ctTblWidth = tcPr.addNewTcW();
                        // The working range of word is 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);
                    }
                }
            }
        }
    }

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 136804 people are learning the system