java - apache poi add table in word document -


i have java code create table , text word document using apache poi adds table in last document. want write text, add table , write text again.

currently adds table first , last document add 2 test (hi & bye)

my code :

public static void main(string[] args)throws exception {         //blank document         xwpfdocument document= new xwpfdocument();          //write document in file system         fileoutputstream out = new fileoutputstream(         new file("create_table.docx"));          //create table         xwpftable table    = document.createtable();         xwpfparagraph para = document.createparagraph();         xwpfrun run        = para.createrun();          run.settext("hi");         //create first row         xwpftablerow tablerowone = table.getrow(0);         tablerowone.getcell(0).settext("col one, row one");         tablerowone.addnewtablecell().settext("col two, row one");         tablerowone.addnewtablecell().settext("col three, row one");         //create second row         xwpftablerow tablerowtwo = table.createrow();         tablerowtwo.getcell(0).settext("col one, row two");         tablerowtwo.getcell(1).settext("col two, row two");         tablerowtwo.getcell(2).settext("col three, row two");         //create third row         xwpftablerow tablerowthree = table.createrow();         tablerowthree.getcell(0).settext("col one, row three");         tablerowthree.getcell(1).settext("col two, row three");         tablerowthree.getcell(2).settext("col three, row three");          run.settext("bye");          document.write(out);         out.close();         system.out.println("create_table.docx written successully"); } 

how can print hi first of page , add table , print bye after table?

and how can save document every time when want add content , write , open ?

thanks

you have keep right order , more importantly : you have create new paragraph.

this code, you'll need :

public static void main(string[] args) throws ioexception {      //blank document     xwpfdocument document = new xwpfdocument();      //write document in file system     fileoutputstream out = new fileoutputstream(new file("create_table.docx"));      //write first text in beginning     xwpfparagraph para = document.createparagraph();     xwpfrun run = para.createrun();     run.settext("hi");      //create table     xwpftable table = document.createtable();      //create first row     xwpftablerow tablerowone = table.getrow(0);     tablerowone.getcell(0).settext("col one, row one");     tablerowone.addnewtablecell().settext("col two, row one");     tablerowone.addnewtablecell().settext("col three, row one");     //create second row     xwpftablerow tablerowtwo = table.createrow();     tablerowtwo.getcell(0).settext("col one, row two");     tablerowtwo.getcell(1).settext("col two, row two");     tablerowtwo.getcell(2).settext("col three, row two");     //create third row     xwpftablerow tablerowthree = table.createrow();     tablerowthree.getcell(0).settext("col one, row three");     tablerowthree.getcell(1).settext("col two, row three");     tablerowthree.getcell(2).settext("col three, row three");      //write second text after table (by creating new paragraph)     xwpfparagraph para2 = document.createparagraph();     xwpfrun run2 = para2.createrun();     run2.settext("bye");      document.write(out);     out.close();     system.out.println("create_table.docx written successully"); } 

this output, you'll get:

output


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -