0

我正在尝试建立一张桌子。我需要减少 2 行之间的空间,但我无法使用row.setheight().

下面是代码:

import eu.europa.ec.fisheries.quotaprepservice.constants.StrConstants;
import eu.europa.ec.fisheries.quotaprepservice.report.bean.Annexe1ATacBeanQuotaItem;
import eu.europa.ec.fisheries.quotaprepservice.report.bean.TacBean;
import org.apache.logging.log4j.util.Strings;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

import static eu.europa.ec.fisheries.quotaprepservice.constants.StrConstants.*;

public class WordBuilder1 {
    public static void main(String[] args) throws Exception {
        XWPFDocument document = new XWPFDocument();
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("The table:");
        XWPFTable table = document.createTable(4, 8);
        prepareQuotaList(table);
        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("C:\\Users\\mishrne\\example.docx");
        document.write(out);
        out.close();
    }

    private static void prepareQuotaList(XWPFTable table) {
        int count = 0;
        for (int row = 0; row < 4; row++) {
            XWPFTableRow tableRow = table.getRow(row);
            tableRow.setHeight((short) 0);
            for (int col = 0; col < 3; col++) {
                setCellBorders(tableRow.getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }
            setCellBorders(tableRow.getCell(3), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            setCellBorders(tableRow.getCell(4), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            for (int col = 5; col < 8; col++) {
                setCellBorders(tableRow.getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }
            if (count == 2) {
                createCell(tableRow.getCell(0), TAC, ParagraphAlignment.LEFT, 9);
                createCell(tableRow.getCell(2), "80", "(2)", ParagraphAlignment.RIGHT);
            } else {
                createCell(tableRow.getCell(0), "Belgium", ParagraphAlignment.LEFT, 9);
                mergeCellHorizontally(table, row, 2, 3);
                createCell(tableRow.getCell(2), "80", "(2)", ParagraphAlignment.RIGHT);
            }
            count++;
        }
    }

    private static void createCell(XWPFTableCell cell, String text, ParagraphAlignment paragraphAlignment, int fontSize) {
        XWPFParagraph paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        paragraph.setAlignment(paragraphAlignment);
        paragraph.setWordWrapped(true);
        XWPFRun run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setText(text);
        run.setFontFamily(TIMES_NEW_ROMAN);
        run.setFontSize(fontSize);
        cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
    }

    private static void createCell(XWPFTableCell cell, String text, String superscriptText, ParagraphAlignment left) {
        XWPFRun run;
        XWPFParagraph paragraph = (cell.getParagraphs().size() > 0) ? cell.getParagraphs().get(0) : cell.addParagraph();
        paragraph.setIndentationLeft(5 * 20); // 10pt left indentation
        paragraph.setAlignment(ParagraphAlignment.LEFT);
        paragraph.setSpacingBefore(0);
        paragraph.setSpacingBeforeLines(0);
        run = (paragraph.getRuns().size() > 0) ? paragraph.getRuns().get(0) : paragraph.createRun();
        run.setFontFamily(TIMES_NEW_ROMAN);
        cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        run.setFontSize(9);
        run.setText(text);
        run = paragraph.createRun();
        run.setText(StrConstants.BLANK_SPACE + StrConstants.BLANK_SPACE + StrConstants.BLANK_SPACE + superscriptText);
        run.setFontSize(9);
        run.setFontFamily(TIMES_NEW_ROMAN);
        run.setSubscript(VerticalAlign.SUPERSCRIPT); // superscript (2)
    }

    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 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);
        }
    }

}
4

1 回答 1

1

要在高度规则中设置表格行高,XWPFTable table还必须设置高度本身。例如:

table.getRow(0).setHeight(12*20); // row height in twentieth pt
table.getRow(0).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT); // height rule exact

可能有以下高度规则:

AT_LEAST: 至少给定高度。如果需要,可以更高,但不能更低。

AUTO: 默认。一个默认段落高度的默认高度包括前后的间距。视内容而定,视需要而定。

EXACT: 准确的给定高度。内容可能会被删减。

请注意,不接受XWPFTableRow.setHeight的 0 值与EXACT高度规则结合使用。这会回落到默认AUTO高度。

根据您的代码示例:

...
    private static void prepareQuotaList(XWPFTable table) {
        int count = 0;
        for (int row = 0; row < 4; row++) {
            XWPFTableRow tableRow = table.getRow(row);
            tableRow.setHeight(12*20); // row height = 12 pt
            tableRow.getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT); // height rule exact
            for (int col = 0; col < 3; col++) {
                setCellBorders(tableRow.getCell(col), new STBorder.Enum[]{STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL});
            }
...
    }
...

如上所述,只有三个高度规则是可能的。因此,如果目标是让表格行与其内容一样高,但不是更多或更少,那么AUTO必须使用高度规则。然后段落的高度决定了行高。请注意,XWPFParagraph 可能在文本行之前、之间和之后设置了间距。所以你可能需要设置:

...
paragraph.setSpacingAfter(0);
paragraph.setSpacingBetween(1); // don't set this 0
paragraph.setSpacingBefore(0);
...

这必须为每个已经包含段落的表格单元格设置。因此,如果整个表都需要,那么:

XWPFTable table = document.createTable(4, 8);
for (XWPFTableRow row : table.getRows()) {
 for (XWPFTableCell cell : row.getTableCells()) {
  for (XWPFParagraph p : cell.getParagraphs()) {
   p.setSpacingAfter(0);
   p.setSpacingBetween(1); // don't set this 0
   p.setSpacingBefore(0);
  }
 }
}
于 2019-11-20T16:11:09.200 回答