java - 使用apache poi在ms word文档中创建 "Multiple Tables"

标签 java ms-word apache-poi

我想在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/

相关文章:

java - Spring 3 独立应用程序不会将输出写入文件

java - 更改字符串(将文件名放在方括号中并获得两个反斜杠)

java - MySQL语法错误异常 : Error in SQL Syntax

java - cucumber JVM 未定义的步骤

java - 如果在 Apache Spark Java 中为空,则用另一个值替换一行的值

excel - 错误1004 : Microsoft Excel cannot paste the data

html - 将 HTML 转换为 MSWord 可读格式

c# - Word.Application.Selection.Find.Execute 不适用于 Word 2013

java - 使用 apache poi 读取 excel - Hashmap、ArrayList

java - Excel单元格值精度