我正在尝试用表格生成一个word文档。只有一个页面,它有5行2列。我用的是信页,大小是8.5“x11”。我给了这个节目的空白。
这是我的密码
XWPFDocument xWPFDocument = new XWPFDocument();
CTSectPr cTSectPr = xWPFDocument.getDocument().getBody().addNewSectPr();
CTPageMar cTPageMar = cTSectPr.addNewPgMar();
cTPageMar.setLeft(BigInteger.valueOf(475));
cTPageMar.setTop(BigInteger.valueOf(720));
cTPageMar.setRight(BigInteger.valueOf(446));
cTPageMar.setBottom(BigInteger.valueOf(605));
XWPFTable xWPFTable = xWPFDocument.createTable(5, 2);
xWPFTable.getCTTbl().getTblPr().unsetTblBorders();
xWPFTable.setTableAlignment(TableRowAlign.CENTER);
xWPFTable.setWidth("100%");
使用以下代码设置单元格宽度和行高。但我没有注意到任何变化。
XWPFTableRow xWPFTableRow;
for (int i = 0; i < 5; i++) {
xWPFTableRow = xWPFTable.getRow(i);
xWPFTableRow.setHeight(2880);
xWPFTableRow.getCell(i).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(6033));
}
我要找的是如何设置Horizontal and Vertical Spacing
,还有使用Apache设置Horizontal and Vertical Pitch
的方法吗?
发布于 2020-08-21 17:28:19
当前的apache poi 4.1.2
提供了一个方法setWidth(java.lang.String widthValue)
,其中widthValue
可以是在XWPFTable
和XWPFTableCell
中提供百分比宽度的String
。
直到现在才直接支持设置单元格间距。因此,必须为此使用底层的ooxml-schemas
类。
完整的例子:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class CreateWordTableCellSpacing {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The table");
int cols = 3;
int rows = 3;
XWPFTable table = document.createTable(rows, cols);
table.setWidth("100%");
table.getRow(0).getCell(0).setWidth("20%");
table.getRow(0).getCell(1).setWidth("30%");
table.getRow(0).getCell(2).setWidth("50%");
//set spacing between cells
table.getCTTbl()
.getTblPr()
.addNewTblCellSpacing()
.setType(
org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth.DXA
);
table.getCTTbl()
.getTblPr()
.getTblCellSpacing()
.setW(java.math.BigInteger.valueOf(
180 // 180 TWentieths of an Inch Point (Twips) = 180/20 = 9 pt = 9/72 = 0.125"
));
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordTableCellSpacing.docx");
document.write(out);
out.close();
document.close();
}
}
https://stackoverflow.com/questions/63516969
复制相似问题