我正在尝试建立一张桌子。我需要减少 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);
}
}
}