apache poi agregar tabla en documento de word
Tengo un código Java para crear una tabla y algunos documentos de texto a palabra usando Apache POI, pero agrega la tabla en el último documento. Quiero escribir texto, luego agregar tabla y escribir texto nuevamente.
Actualmente agrega la tabla primero y el último documento agrega la prueba 2 (Hola y Adiós)
Mi código :
public static void main(String[] args)throws Exception {
//Blank Document
XWPFDocument document= new XWPFDocument();
//Write the 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");
}
Como puedo imprimirHi
primero de la página y agregar tabla e imprimirBye
después de la mesa?
¿Y cómo puedo guardar el documento cada vez que quiero agregarle contenido y finalmente escribirlo y abrirlo?
Gracias