Dear Sir
我有一個表格需要增加空白列,但改不太出來,可以如何修改呢?附上Java原始檔和word範本檔。insertRow.7z (34.9 KB)
以下代码将在目标表的末尾插入七个空白行。
Document doc = new Document("C:\\Temp\\insertRow\\C-4-17-temp.docx");
Table table = doc.getFirstSection().getBody().getTables().get(0);
Row cloneOfLastRow = (Row) table.getLastRow().deepClone(true);
for (Cell cell : cloneOfLastRow.getCells()) {
cell.removeAllChildren();
cell.ensureMinimum();
}
for (int i = 0; i < 7; i++) {
table.getRows().add(cloneOfLastRow.deepClone(true));
}
doc.save("C:\\temp\\insertRow\\awjava-21.9.docx");
您可以先找到包含文本“稽核組長”的行,然后在其上方插入空白行。
Document doc = new Document("C:\\Temp\\insertRow\\C-4-17-temp.docx");
Row row = null;
for (Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
if (para.toString(SaveFormat.TEXT).trim().equals("稽核組長")) {
row = (Row) para.getAncestor(NodeType.ROW);
if (row != null)
break;
}
}
// Clone any row that you want to copy
Row cloneOfRow = (Row) row.deepClone(true);
for (Cell cell : cloneOfRow.getCells()) {
cell.removeAllChildren();
cell.ensureMinimum();
}
Table table = row.getParentTable();
for (int i = 0; i < 7; i++)
table.insertBefore(cloneOfRow.deepClone(true), row);
doc.save("C:\\temp\\insertRow\\awjava-21.9.docx");