简体   繁体   中英

Create “Multiple Tables” in ms word document using apache poi

I want 2 tables in the word document like this:

Test ID : TS1

Test Data : Sample Data

Test Description : Sample Description

Test ID : TS2

Test Data : Sample Data

Test Description : Sample Description

For above tables i written code like this:

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");
}

Iam getting output like this(Two tables are getting clubed) :

Test ID :   TS1
Test Data : Sample Data
Test Description :  Sample Description
Test ID :   TS2
Test Data : Sample Data
Test Description :  Sample Description

Add this line in the end of your boucle:

document.createParagraph().createRun().addBreak();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM