Search in sources :

Example 1 with CTHMerge

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge in project pmph by BCSquad.

the class WordHelper method insertXWPFParagraph.

public static void insertXWPFParagraph(String key, XWPFDocument doc2, XWPFTable xwpfTable) {
    List<XWPFParagraph> paragraphList = doc2.getParagraphs();
    if (paragraphList != null && paragraphList.size() > 0) {
        for (int i = 0; i < paragraphList.size(); i++) {
            List<XWPFRun> runs = paragraphList.get(i).getRuns();
            for (XWPFRun run : runs) {
                String text = run.getText(0);
                if (text != null) {
                    if (text.equals(key)) {
                        XmlCursor cursor = paragraphList.get(i + 1).getCTP().newCursor();
                        XWPFTable table = doc2.insertNewTbl(cursor);
                        XWPFTableRow tableRow = table.getRow(0);
                        for (int cellNum = 0; cellNum < 27; cellNum++) {
                            tableRow.addNewTableCell();
                        }
                        for (int rowNum = 0; rowNum < 3; rowNum++) {
                            table.createRow();
                        }
                        // 合并列(没有作用)
                        for (int rowNum = 0; rowNum < table.getRows().size(); rowNum++) {
                            for (int cellIndex = 0; cellIndex < tableRow.getTableCells().size(); cellIndex++) {
                                CTHMerge hmerge = CTHMerge.Factory.newInstance();
                                if (cellIndex == 0) {
                                    // The first merged cell is set with RESTART merge value
                                    hmerge.setVal(STMerge.RESTART);
                                } else {
                                    // Cells which join (merge) the first one, are set with CONTINUE
                                    hmerge.setVal(STMerge.CONTINUE);
                                }
                                XWPFTableCell cell = table.getRow(rowNum).getCell(cellIndex);
                                // Try getting the TcPr. Not simply setting an new one every time.
                                CTTcPr tcPr = cell.getCTTc().getTcPr();
                                if (tcPr != null) {
                                    tcPr.setHMerge(hmerge);
                                } else {
                                    // only set an new TcPr if there is not one already
                                    tcPr = CTTcPr.Factory.newInstance();
                                    tcPr.setHMerge(hmerge);
                                    cell.getCTTc().setTcPr(tcPr);
                                }
                            }
                        }
                        // 合并行
                        for (int col = 0; col < tableRow.getTableCells().size(); col++) {
                            for (int rowIndex = 0; rowIndex < table.getRows().size(); rowIndex++) {
                                XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
                                if (rowIndex == 0) {
                                    // The first merged cell is set with RESTART merge value
                                    cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
                                } else {
                                    // Cells which join (merge) the first one, are set with CONTINUE
                                    cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) CTHMerge(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) CTTcPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr) XmlCursor(org.apache.xmlbeans.XmlCursor) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow)

Aggregations

XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)1 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)1 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)1 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)1 XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 CTHMerge (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge)1 CTTcPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr)1