我想在Word文档中有2个表格,如下所示:
测试 ID:TS1
测试数据:示例数据
测试描述:示例描述
测试 ID:TS2
测试数据:示例数据
测试描述:示例描述
对于上面的表格,我编写了这样的代码:
XWPFDocument document = new XWPFDocument();
XWPFParagraph tmpParagraph = document.createParagraph();
XWPFRun tmpRun = tmpParagraph.createRun();
FileOutputStream fos = new FileOutputStream(new File("E:\\Selenium\\yoj.doc"));
for(int tabNo = 0;tabNo<=1;tabNo++) //to get two tables
{
XWPFTable tab = document.createTable(10,2);
XWPFTableRow row = tab.getRow(0);
tab.getRow(0).getCell(0).setText("Test Scenario ID: ");
tab.getRow(0).getCell(1).setText("TS1");
tab.getRow(0).setCantSplitRow(true);
tab.getRow(1).getCell(0).setText("Test Scenario Description: ");
tab.getRow(1).getCell(1).setText("2");
tab.getRow(2).getCell(0).setText("Test Data: ");
tab.getRow(2).getCell(1).setText("3");
}
我得到这样的输出(两个表被合并):
Test ID : TS1
Test Data : Sample Data
Test Description : Sample Description
Test ID : TS2
Test Data : Sample Data
Test Description : Sample Description
最佳答案
在你的毛圈末尾添加这一行:
document.createParagraph().createRun().addBreak();
关于java - 使用apache poi在ms word文档中创建 "Multiple Tables",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28293243/