【发布时间】:2015-08-21 11:26:41
【问题描述】:
您好,我正在使用 apache POI,我必须使用它创建 docx 文件。 现在我遍历一个 html 文档并获取标签以创建一个有效的 docx 文档。
当我在表格单元格中有一个表格时,无法正确显示。
<table>
<tr>
<td style="text-align: left">Status</td>
<td>
<table>
<tr>
<td>test</td>
</tr>
</table>
</td>
</tr>
</table>
从 docx 加载带有嵌套表的正确文件是没有问题的。
FileOutputStream fos = null;
XWPFDocument document = null;
try {
fos = new FileOutputStream(docRes);
if (fileType == docType.DOCX) {
try {
String fileName = "D:\\test.docx";
if (!(fileName.endsWith(".doc") || fileName.endsWith(".docx"))) {
throw new FileFormatException();
} else {
XWPFDocument doc = new XWPFDocument(new FileInputStream(fileName));
List<XWPFTable> table = doc.getTables();
}
}
}
}
当我再次保存此文件时,我会在 Word 中获得一个带有 POI 的嵌套表格。
但是如何在不加载正确的 docx 文件的情况下创建嵌套表呢?
我检查了许多选项,但已经简单的解决方案不起作用。
document = new XWPFDocument();
XWPFTable tableOne = document.createTable();
XWPFTableRow tableOneRow1 = tableOne.getRow(0);
XWPFTableRow tableOneRow2 = tableOne.createRow();
tableOneRow1.getCell(0).setText("Test");
tableOneRow1.addNewTableCell();
tableOneRow1.getCell(1).setText("Test");
tableOneRow2.getCell(0).setText("Test");
tableOneRow2.addNewTableCell();
tableOneRow2.getCell(1).setText("include nestedTable");
XWPFTable tableTwo = document.createTable();
XWPFTableRow tableTwoRow1 = tableTwo.getRow(0);
tableTwoRow1.getCell(0).setText("Test");
tableTwoRow1.addNewTableCell();
tableTwoRow1.getCell(0).setText("nestedTable");
tableOneRow2.getCell(1).insertTable(0, tableTwo);
与从 docx 直接加载的变体不同的是,在自制解决方案中,文档在文档根目录中有两个表。
如何构建嵌套表?
谢谢
祝菲利克斯
【问题讨论】:
标签: java apache-poi