当前位置: 首页 > 知识库问答 >
问题:

需要在apache poi word中向右移动表

蒋飞捷
2023-03-14

我们在XWPFTable中是否有一个设置来设置表向右移动的对齐方式,或者我是否需要重新构建整个表

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.math.BigInteger;

public class WordBuilder {

    public static void main(String[] args) throws Exception {

        XWPFDocument document = new XWPFDocument();

        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("The table:");
        createNonGroupedSubTacTable(document);
        CTSectPr sectPr = document.getDocument().getBody().getSectPr();
        if (sectPr == null) sectPr = document.getDocument().getBody().addNewSectPr();
        CTPageSz pageSz = sectPr.addNewPgSz();
        pageSz.setOrient(STPageOrientation.PORTRAIT);
        pageSz.setW(BigInteger.valueOf(11900)); //12240 Twips = 12240/20 = 612 pt = 612/72 = 8.5"
        pageSz.setH(BigInteger.valueOf(16840)); //15840 Twips = 15840/20 = 792 pt = 792/72 = 11"

        FileOutputStream out = new FileOutputStream("example_docx");
        document.write(out);
        out.close();
    }

    private static void createNonGroupedSubTacTable(XWPFDocument document) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        XWPFTable table = document.createTable(10, 8);
        setTableAlign(table);
        int defaultColWidth = 1 * 1600 * 5 / 8; // 8 columns fits to 8 inches
        int[] colunmWidths = new int[]{
                defaultColWidth * 3 / 4, defaultColWidth * 8 / 4, defaultColWidth * 2 / 4, defaultColWidth * 3 / 4,
                defaultColWidth * 3 / 4, defaultColWidth * 2 / 4, defaultColWidth * 8 / 4, defaultColWidth * 3 / 4
        };
        table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[0]));
        setColumnWidth(table, 0, 0, colunmWidths[0]);
        //other columns
        for (int col = 1; col < colunmWidths.length; col++) {
            table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[col]));
            setColumnWidth(table, 0, col, colunmWidths[col]);
        }
        //set cell borders
        for (int col = 0; col < 3; col++) {
            setCellBorders(table.getRow(0).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.THICK, STBorder.NIL, STBorder.NIL});
        }
        setCellBorders(table.getRow(0).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.THICK, STBorder.THICK, STBorder.NIL});
        setCellBorders(table.getRow(0).getCell(4), new STBorder.Enum[]{STBorder.THICK, STBorder.THICK, STBorder.NIL, STBorder.NIL});
        for (int col = 5; col < 8; col++) {
            setCellBorders(table.getRow(0).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.THICK, STBorder.NIL, STBorder.NIL});
        }
        for (int col = 0; col < 3; col++) {
            setCellBorders(table.getRow(1).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
        }
        setCellBorders(table.getRow(1).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.THICK, STBorder.THICK});
        setCellBorders(table.getRow(1).getCell(4), new STBorder.Enum[]{STBorder.THICK, STBorder.NIL, STBorder.NIL, STBorder.THICK});
        for (int col = 5; col < 8; col++) {
            setCellBorders(table.getRow(1).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
        }
        table.getRow(0).setHeight(28 * 20); // 28pt row height
        table.getRow(0).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);

        XWPFTableCell cell = table.getRow(0).getCell(7);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setText("Species:");
        cell = table.getRow(0).getCell(6);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setAlignment(ParagraphAlignment.RIGHT);
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setText("Greater silver smelt");

        cell = table.getRow(0).getCell(3);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Zone:");

        cell = table.getRow(0).getCell(1);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Union and international waters of 1 and 2 (2)");

        table.getRow(1).setHeight(28 * 20); // 28pt row height
        table.getRow(1).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);

        cell = table.getRow(1).getCell(6);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        paragraph.setAlignment(ParagraphAlignment.RIGHT);
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.addBreak();
        run = paragraph.createRun();
        run.setText("Argentinia silos");

        cell = table.getRow(1).getCell(1);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.addTab();
        run = paragraph.createRun();
        run.addTab();
        run = paragraph.createRun();
        run.addBreak();
        run = paragraph.createRun();
        run.setText("(ARU/1/2)");
        for (int row = 2; row < 6; row++) {
            for (int col = 5; col < 8; col++) {
                setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }
            setCellBorders(table.getRow(row).getCell(5), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            setCellBorders(table.getRow(row).getCell(6), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            for (int col = 0; col < 5; col++) {
                setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }

            XWPFTableCell cell1 = table.getRow(row).getCell(7);
            paragraph = (cell1.getParagraphs().size() > 0) ? cell1.getParagraphs().get(0) : cell1.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setFontSize(10);
            run.setText("Belgium");

            cell = table.getRow(row).getCell(6);
            paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            paragraph.setAlignment(ParagraphAlignment.RIGHT);
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setText("6  ");
            run = paragraph.createRun();
            run.setText("(2)");
            run.setSubscript(VerticalAlign.SUPERSCRIPT); // superscript (2)
        }

        mergeCellHorizontally(table, 2, 0, 3);
        cell = table.getRow(2).getCell(0);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20);
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Analytical TAC");

        mergeCellHorizontally(table, 3, 0, 3);
        cell = table.getRow(3).getCell(0);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Article 3 of Regulation (EC) No 847/96 shall not apply");

        mergeCellHorizontally(table, 4, 0, 3);
        cell = table.getRow(4).getCell(0);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(10);
        run.setText("Article 4 of Regulation (EC) No 847/96 shall not apply.");

        for (int col = 0; col < 8; col++) {
            setCellBorders(table.getRow(6).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
        }

        for (int row = 7; row < 8; row++) {
            for (int col = 5; col < 8; col++) {
                setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }
            setCellBorders(table.getRow(row).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            setCellBorders(table.getRow(row).getCell(4), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            for (int col = 0; col < 3; col++) {
                setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }

            XWPFTableCell cell1 = table.getRow(row).getCell(7);
            paragraph = (cell1.getParagraphs().size() > 0) ? cell1.getParagraphs().get(0) : cell1.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setText("TAC");

            cell = table.getRow(row).getCell(6);
            paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            paragraph.setAlignment(ParagraphAlignment.RIGHT);
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run = paragraph.createRun();
            run.setText("10");
            run = paragraph.createRun();
            run.setText("(2)");
            run.setSubscript(VerticalAlign.SUPERSCRIPT); // superscript (2)
        }
        for (int row = 8; row < 10; row++) {
            if (row == 9) {
                for (int col = 0; col < 3; col++) {
                    setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
                }
                setCellBorders(table.getRow(row).getCell(5), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
                setCellBorders(table.getRow(row).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
                for (int col = 4; col < 8; col++) {
                    setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
                }
            } else {
                for (int col = 0; col < 3; col++) {
                    setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
                }
                setCellBorders(table.getRow(row).getCell(5), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
                setCellBorders(table.getRow(row).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
                for (int col = 4; col < 8; col++) {
                    setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
                }

            }
            XWPFTableCell cell1 = table.getRow(row).getCell(7);
            paragraph = (cell1.getParagraphs().size() > 0) ? cell1.getParagraphs().get(0) : cell1.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setText("1");


            mergeCellHorizontally(table, row, 0, 5);
            cell = table.getRow(row).getCell(0);
            paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run = paragraph.createRun();
            run.setFontSize(9);
            run.setText("Up to 2 % of the quota may consist of by-catches of whiting and mackerel (OT1/*2A3A4). " +
                    "By-catches of whiting and mackerel counted against the quota pursuant to this provision and by-catches of species counted " +
                    "against the quota pursuant to Article 15(8) of Regulation (EU) No 1380/2013 shall, together, not exceed 9% of the quota.");

        }

    }

    private static void setColumnWidth(XWPFTable table, int row, int col, int width) {
        CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
        tblWidth.setW(BigInteger.valueOf(width));
        tblWidth.setType(STTblWidth.DXA);
        CTTcPr tcPr = table.getRow(row).getCell(col).getCTTc().getTcPr();
        if (tcPr != null) {
            tcPr.setTcW(tblWidth);
        } else {
            tcPr = CTTcPr.Factory.newInstance();
            tcPr.setTcW(tblWidth);
            table.getRow(row).getCell(col).getCTTc().setTcPr(tcPr);
        }
    }

    private static void setCellBorders(XWPFTableCell cell, STBorder.Enum[] borderTypesLTRB) {
        CTTcBorders borders = CTTcBorders.Factory.newInstance();
        borders.addNewLeft().setVal(borderTypesLTRB[0]);
        borders.addNewTop().setVal(borderTypesLTRB[1]);
        borders.addNewRight().setVal(borderTypesLTRB[2]);
        borders.addNewBottom().setVal(borderTypesLTRB[3]);
        CTTcPr tcPr = cell.getCTTc().getTcPr();
        if (tcPr != null) {
            tcPr.setTcBorders(borders);
        } else {
            tcPr = CTTcPr.Factory.newInstance();
            tcPr.setTcBorders(borders);
            cell.getCTTc().setTcPr(tcPr);
        }
    }

    private static void mergeCellHorizontally(XWPFTable table, int row, int fromCol, int toCol) {
        XWPFTableCell cell = table.getRow(row).getCell(fromCol);
        CTTcPr tcPr = cell.getCTTc().getTcPr();
        if (tcPr == null) tcPr = cell.getCTTc().addNewTcPr();
        if (tcPr.isSetGridSpan()) {
            tcPr.getGridSpan().setVal(BigInteger.valueOf(toCol - fromCol + 1));
        } else {
            tcPr.addNewGridSpan().setVal(BigInteger.valueOf(toCol - fromCol + 1));
        }
        for (int colIndex = toCol; colIndex > fromCol; colIndex--) {
            table.getRow(row).getCtRow().removeTc(colIndex);
            table.getRow(row).removeCell(colIndex);
        }
    }

    private static void setTableAlign(XWPFTable table) {
        CTTbl cttblp = table.getCTTbl();
        CTTblPr cttblpr;
        cttblpr = (cttblp.getTblPr() == null ? cttblp.addNewTblPr() : cttblp.getTblPr());
        cttblpr.addNewBidiVisual().setVal(STOnOff.ON);
        CTJc ctjc = (cttblpr.isSetJc() ? cttblpr.getJc() : cttblpr.addNewJc());
        ctjc.setVal(STJc.LEFT);
    }
}

共有2个答案

昝光临
2023-03-14

这个答案是关于您提供的完整示例的问题:

第一:不建议使用双向文本设置对齐,以便表格从右向左显示。这只适用于真正需要双向文本的语言,例如希伯来语或阿拉伯语。所以你的表对齐应该是这样的:

private static void setTableAlign(XWPFTable table, STJc.Enum align) {
    CTTbl cttblp = table.getCTTbl();
    CTTblPr cttblpr = (cttblp.getTblPr() == null ? cttblp.addNewTblPr() : cttblp.getTblPr());
    CTJc ctjc = (cttblpr.isSetJc() ? cttblpr.getJc() : cttblpr.addNewJc());
    ctjc.setVal(align);
}

第二:合并单元格时,应设置新合并单元格的列宽。其他Word可能会使用各种不必要的自动方法来确定列宽。所以举个例子:

...
mergeCellHorizontally(table, 2, 4, 7);
setColumnWidth(table, 2, 4, colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);
...

...
mergeCellHorizontally(table, row, 1, 7);
setColumnWidth(table, row, 1, colunmWidths[1]+colunmWidths[2]+colunmWidths[3]+colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);
...

根据提供的屏幕截图创建右对齐表格的完整示例:

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileOutputStream;
import java.math.BigInteger;

public class WordBuilder {

    public static void main(String[] args) throws Exception {

        XWPFDocument document = new XWPFDocument();

        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("The table:");
        createNonGroupedSubTacTable(document);
        CTSectPr sectPr = document.getDocument().getBody().getSectPr();
        if (sectPr == null) sectPr = document.getDocument().getBody().addNewSectPr();
        CTPageSz pageSz = sectPr.addNewPgSz();
        pageSz.setOrient(STPageOrientation.PORTRAIT);
        pageSz.setW(BigInteger.valueOf(11900)); //12240 Twips = 12240/20 = 612 pt = 612/72 = 8.5"
        pageSz.setH(BigInteger.valueOf(16840)); //15840 Twips = 15840/20 = 792 pt = 792/72 = 11"

        FileOutputStream out = new FileOutputStream("example.docx");
        document.write(out);
        out.close();
        document.close();
    }

    private static void createNonGroupedSubTacTable(XWPFDocument document) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        XWPFTable table = document.createTable(10, 8);

        setTableAlign(table, STJc.RIGHT);

        int defaultColWidth = 1 * 1600 * 5 / 8; // 8 columns fits to 8 inches
        int[] colunmWidths = new int[]{
                defaultColWidth * 3 / 4, defaultColWidth * 2 / 4, defaultColWidth * 8 / 4, defaultColWidth * 3 / 4,
                defaultColWidth * 3 / 4, defaultColWidth * 2 / 4, defaultColWidth * 8 / 4, defaultColWidth * 3 / 4
        };
        table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[0]));
        setColumnWidth(table, 0, 0, colunmWidths[0]);
        //other columns
        for (int col = 1; col < colunmWidths.length; col++) {
            table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[col]));
            setColumnWidth(table, 0, col, colunmWidths[col]);
        }
        //set cell borders
        for (int col = 0; col < 3; col++) {
            setCellBorders(table.getRow(0).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.THICK, STBorder.NIL, STBorder.NIL});
        }
        setCellBorders(table.getRow(0).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.THICK, STBorder.THICK, STBorder.NIL});
        setCellBorders(table.getRow(0).getCell(4), new STBorder.Enum[]{STBorder.THICK, STBorder.THICK, STBorder.NIL, STBorder.NIL});
        for (int col = 5; col < 8; col++) {
            setCellBorders(table.getRow(0).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.THICK, STBorder.NIL, STBorder.NIL});
        }
        for (int col = 0; col < 3; col++) {
            setCellBorders(table.getRow(1).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
        }
        setCellBorders(table.getRow(1).getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.THICK, STBorder.THICK});
        setCellBorders(table.getRow(1).getCell(4), new STBorder.Enum[]{STBorder.THICK, STBorder.NIL, STBorder.NIL, STBorder.THICK});
        for (int col = 5; col < 8; col++) {
            setCellBorders(table.getRow(1).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
        }
        table.getRow(0).setHeight(28 * 20); // 28pt row height
        table.getRow(0).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);

        XWPFTableCell cell = table.getRow(0).getCell(0);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setText("Species:");
        cell = table.getRow(0).getCell(2);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setAlignment(ParagraphAlignment.RIGHT);
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setText("Greater silver smelt");

        cell = table.getRow(0).getCell(4);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Zone:");

        cell = table.getRow(0).getCell(6);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Union and international waters of 1 and 2 (2)");

        table.getRow(1).setHeight(28 * 20); // 28pt row height
        table.getRow(1).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);

        cell = table.getRow(1).getCell(2);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        paragraph.setAlignment(ParagraphAlignment.RIGHT);
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.addBreak();
        run = paragraph.createRun();
        run.setText("Argentinia silos");

        cell = table.getRow(1).getCell(6);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.addTab();
        run = paragraph.createRun();
        run.addTab();
        run = paragraph.createRun();
        run.addBreak();
        run = paragraph.createRun();
        run.setText("(ARU/1/2)");

        for (int row = 2; row < 6; row++) {
            for (int col = 5; col < 8; col++) {
                setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }
            setCellBorders(table.getRow(row).getCell(5), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            setCellBorders(table.getRow(row).getCell(6), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            for (int col = 0; col < 5; col++) {
                setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }

            XWPFTableCell cell1 = table.getRow(row).getCell(0);
            paragraph = (cell1.getParagraphs().size() > 0) ? cell1.getParagraphs().get(0) : cell1.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setFontSize(10);
            run.setText("Belgium");

            cell = table.getRow(row).getCell(3);
            paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            //paragraph.setAlignment(ParagraphAlignment.RIGHT);
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setText("6  ");
            run = paragraph.createRun();
            run.setText("(2)");
            run.setSubscript(VerticalAlign.SUPERSCRIPT); // superscript (2)
        }

        mergeCellHorizontally(table, 2, 4, 7);
        setColumnWidth(table, 2, 4, colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);
        cell = table.getRow(2).getCell(4);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20);
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Analytical TAC");

        mergeCellHorizontally(table, 3, 4, 7);
        setColumnWidth(table, 3, 4, colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);
        cell = table.getRow(3).getCell(4);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Article 3 of Regulation (EC) No 847/96 shall not apply");

        mergeCellHorizontally(table, 4, 4, 7);
        setColumnWidth(table, 4, 4, colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);
        cell = table.getRow(4).getCell(4);
        paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontSize(9);
        run.setText("Article 4 of Regulation (EC) No 847/96 shall not apply.");

        for (int col = 0; col < 8; col++) {
            setCellBorders(table.getRow(6).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
        }

        for (int row = 7; row < 8; row++) {
            for (int col = 0; col < 8; col++) {
                setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }

            XWPFTableCell cell1 = table.getRow(row).getCell(0);
            paragraph = (cell1.getParagraphs().size() > 0) ? cell1.getParagraphs().get(0) : cell1.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setText("TAC");

            cell = table.getRow(row).getCell(3);
            paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            //paragraph.setAlignment(ParagraphAlignment.RIGHT);
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run = paragraph.createRun();
            run.setText("10");
            run = paragraph.createRun();
            run.setText("(2)");
            run.setSubscript(VerticalAlign.SUPERSCRIPT); // superscript (2)
        }


        for (int row = 8; row < 10; row++) {
            if (row == 9) {
                for (int col = 0; col < 8; col++) {
                    setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK});
                }
            } else {
                for (int col = 0; col < 8; col++) {
                    setCellBorders(table.getRow(row).getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
                }
            }

            XWPFTableCell cell1 = table.getRow(row).getCell(0);
            paragraph = (cell1.getParagraphs().size() > 0) ? cell1.getParagraphs().get(0) : cell1.addParagraph();
            paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run.setText("1");


            mergeCellHorizontally(table, row, 1, 7);
            setColumnWidth(table, row, 1, colunmWidths[1]+colunmWidths[2]+colunmWidths[3]+colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);

            cell = table.getRow(row).getCell(1);
            paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
            run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
            run = paragraph.createRun();
            run.setFontSize(9);
            run.setText("Up to 2 % of the quota may consist of by-catches of whiting and mackerel (OT1/*2A3A4). " +
                    "By-catches of whiting and mackerel counted against the quota pursuant to this provision and by-catches of species counted " +
                    "against the quota pursuant to Article 15(8) of Regulation (EU) No 1380/2013 shall, together, not exceed 9% of the quota.");
        }

    }

    private static void setColumnWidth(XWPFTable table, int row, int col, int width) {
        CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
        tblWidth.setW(BigInteger.valueOf(width));
        tblWidth.setType(STTblWidth.DXA);
        CTTcPr tcPr = table.getRow(row).getCell(col).getCTTc().getTcPr();
        if (tcPr != null) {
            tcPr.setTcW(tblWidth);
        } else {
            tcPr = CTTcPr.Factory.newInstance();
            tcPr.setTcW(tblWidth);
            table.getRow(row).getCell(col).getCTTc().setTcPr(tcPr);
        }
    }

    private static void setCellBorders(XWPFTableCell cell, STBorder.Enum[] borderTypesLTRB) {
        CTTcBorders borders = CTTcBorders.Factory.newInstance();
        borders.addNewLeft().setVal(borderTypesLTRB[0]);
        borders.addNewTop().setVal(borderTypesLTRB[1]);
        borders.addNewRight().setVal(borderTypesLTRB[2]);
        borders.addNewBottom().setVal(borderTypesLTRB[3]);
        CTTcPr tcPr = cell.getCTTc().getTcPr();
        if (tcPr != null) {
            tcPr.setTcBorders(borders);
        } else {
            tcPr = CTTcPr.Factory.newInstance();
            tcPr.setTcBorders(borders);
            cell.getCTTc().setTcPr(tcPr);
        }
    }

    private static void mergeCellHorizontally(XWPFTable table, int row, int fromCol, int toCol) {
        XWPFTableCell cell = table.getRow(row).getCell(fromCol);
        CTTcPr tcPr = cell.getCTTc().getTcPr();
        if (tcPr == null) tcPr = cell.getCTTc().addNewTcPr();
        if (tcPr.isSetGridSpan()) {
            tcPr.getGridSpan().setVal(BigInteger.valueOf(toCol - fromCol + 1));
        } else {
            tcPr.addNewGridSpan().setVal(BigInteger.valueOf(toCol - fromCol + 1));
        }
        for (int colIndex = toCol; colIndex > fromCol; colIndex--) {
            table.getRow(row).getCtRow().removeTc(colIndex);
            table.getRow(row).removeCell(colIndex);
        }
    }

    private static void setTableAlign(XWPFTable table, STJc.Enum align) {
        CTTbl cttblp = table.getCTTbl();
        CTTblPr cttblpr = (cttblp.getTblPr() == null ? cttblp.addNewTblPr() : cttblp.getTblPr());
        CTJc ctjc = (cttblpr.isSetJc() ? cttblpr.getJc() : cttblpr.addNewJc());
        ctjc.setVal(align);
    }
}
柳玄裳
2023-03-14

您可以使用低级的org.openxmlformats.schemas.wordprocessingml.x2006.main.*类缩进表。

XWPFTable table = document.createTable(3,3);
// set indentation of the table
CTTblWidth tableIndentation = table.getCTTbl().getTblPr().addNewTblInd();
tableIndentation.setW(BigInteger.valueOf(720)); //720 TWentieths of an Inch Point (Twips) = 720/20 = 36 pt = 36/72 = 0.5"
tableIndentation.setType(STTblWidth.DXA);

但也有XWPFTable.setTableAlignment。使用该选项可以对齐整个表格。

XWPFTable table = document.createTable(3,3);
table.setTableAlignment(TableRowAlign.RIGHT); //table right aligned

对于apache poi4.1.1之前的版本,可以使用以下低级方法右对齐表格:

XWPFTable  table = document.createTable(3,3);
table.getCTTbl().getTblPr().addNewJc().setVal(STJc.RIGHT);  //table right aligned

完整示例:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.math.BigInteger;

public class CreateWordTableIndent {

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("The table indented:");
  XWPFTable table = document.createTable(3,3);
  // set indentation of the table
  CTTblWidth tableIndentation = table.getCTTbl().getTblPr().addNewTblInd();
  tableIndentation.setW(BigInteger.valueOf(720)); //720 TWentieths of an Inch Point (Twips) = 720/20 = 36 pt = 36/72 = 0.5"
  tableIndentation.setType(STTblWidth.DXA);
  for (int row = 0; row < 3; row++) {
   for (int col = 0; col < 3; col++) {
    table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
   }
  }
  //create CTTblGrid for this table with widths of the 3 columns. 
  //necessary for Libreoffice/Openoffice to accept the column widths.
  //first column
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(1440+1440/8));
  //other columns
  for (int col = 1; col < 3; col++) {
   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1440+1440/8));
  }
  paragraph = document.createParagraph();

  paragraph = document.createParagraph();
  run = paragraph.createRun();  
  run.setText("The table right aligned:");
  table = document.createTable(3,3);
  table.setTableAlignment(TableRowAlign.RIGHT); //table right aligned
  for (int row = 0; row < 3; row++) {
   for (int col = 0; col < 3; col++) {
    table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
   }
  }
  //create CTTblGrid for this table with widths of the 3 columns. 
  //necessary for Libreoffice/Openoffice to accept the column widths.
  //first column
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(1440+1440/8));
  //other columns
  for (int col = 1; col < 3; col++) {
   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1440+1440/8));
  }
  paragraph = document.createParagraph();


  FileOutputStream out = new FileOutputStream("CreateWordTableIndent.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

结果:

 类似资料:
  • 我正在使用添加元素的JscrollPane,当元素超过7个时,JscrollPane中的JScrollBar将被激活。我需要当我引入一个新元素时,滚动条向右移动 构造函数: 滚动条向右移动,但从来没有达到最大值,总是有多一点的距离滚动。 有人知道为什么会这样吗?我看到使用setValue和getMaximum将滚动条向右移动,但对我来说,它停留在靠近右边的地方,而不是右边。 下面是一张截图来看看。

  • 我有一个下拉菜单,除了一个我无法解决的小错误外,它工作得很好。 第一个下拉框项正常出现。然而,第二个下拉框项稍微向右移动。我猜想是我设置的链接的边距导致下拉框稍微向右移动。如果是这样的话,我能做些什么来解决这个问题呢? 这是一个简单的导航菜单,它将drop菜单项的位置设置为绝对位置,并由父元素的overflow:隐藏功能隐藏。悬停时,下拉框会显示溢出:可见。 现场在这里- CSS 超文本标记语言

  • 所以我在一个网站上建立了一个包含搜索栏的navbar,同时还有一个分组按钮。它在桌面视图上运行良好,但当我缩小到移动视图时,站点会产生可折叠菜单,搜索栏会留在左侧,但按钮会浮到右侧。当我用它周围的input-group-btn类移除span时,就没有这个问题了。 图片包括http://bit.ly/1G706RC 编辑:我修复了我自己的问题!我只需将一个类添加到span中

  • 我们用AngularJS HTML创建了一个应用程序。在这种情况下,文本框、链接、按钮和其他对象上的选项卡顺序应该是从左到右的。一旦到达行上最右边的对象,选项卡就应该向下移动到最左边的文本框。 为了实现这一点,我在元素上添加了tab-index,但根据需要,我需要从左到右移动。 下面添加了一个示例代码: 由于所设计的UI是列式设计,所以如果设计分为两部分,每部分有四个组合框,则添加为: DIV1

  • 当我单击uitextfield时,如何在uitextfield中向右移动和显示光标3个空格。我使uitextfield变成圆角,并将约束从Main.Storyboard左“10”放置,当我单击uitextfield时,光标显示在uitextfield之外。下面是swift代码。

  • 我看到很多视频和教程在GUI中移动对象时使用计时器,但我尝试在没有计时器的情况下做一些事情,它似乎工作正常。我不太明白什么时候需要计时器。非常感谢任何帮助 没有计时器的两个移动对象的代码示例 带有计时器的代码示例