我有一个使用 Apache POI for docx 制作的简单 XWPFTable,但如果单元格的文本很短,则单元格/行会调整大小以适合文本。如何设置它,以便无论文本长度如何,表格/行都保持从整个页面的左到右拉伸(stretch)?谢谢。
如果有帮助的话,这是代码的表格部分:
XWPFTable header = document.createTable(1, 3);
header.getRow(0).getCell(0).setText("LCode");
header.getRow(0).getCell(1).setText("QTY");
header.getRow(0).getCell(2).setText("Description/Justification");
XWPFTable table = document.createTable(LCodes.size(), 3);
int x = 0;
for (Map.Entry entry : new TreeMap<Integer, List<String>>(LCodes).entrySet()) {
Object LCode = entry.getKey();
table.getRow(x).getCell(0).setText("L" + LCode);
table.getRow(x).getCell(1).setText(LCodes.get(LCode).get(2));
XWPFParagraph para = table.getRow(x).getCell(2).getParagraphs().get(0);
for (int i = 0; i < 2; i++) {
if (i == 1 && LCodes.get(LCode).get(i).trim().equals("")) {
continue;
}
XWPFRun leading = para.createRun();
leading.setItalic(true);
if (i == 0) {
leading.setText("Description: ");
} else {
leading.setText("Justification: ");
}
XWPFRun trailing = para.createRun();
trailing.setText(LCodes.get(LCode).get(i));
if (i == 0 && !(LCodes.get(LCode).get(i).trim().equals(""))) {
trailing.addBreak();
}
}
x++;
}
最佳答案
要拉伸(stretch) xwpf 表的宽度,您需要执行以下操作。 1. 在示例 xwpf 文档中创建表。 2. 下载与 zip 相同的文档并解压 document.xml 文件。 3. 查找
另一个解决方案:如果您不想离开所有这些并且您的表格边框被隐藏。您可以在表格的第一列中添加下面的 XWPFparagraph,这样就可以了。
//add this para in first column to get table width, 84pts total
private XWPFParagraph getFirstColumnText(){
XWPFParagraph para=new XWPFDocument().createParagraph();
XWPFRun run=para.createRun();
run.setColor("ffffff");
run.setText("...................................................................................");
return para;
}
关于Java (Apache POI) 如何在整个页面上拉伸(stretch) XWPFTable 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15805010/