apache poi Tabelle in Word-Dokument hinzufügen
Ich habe Java-Code zum Erstellen einer Tabelle und eines Textes in ein Word-Dokument mit dem Apache-POI, aber es fügt eine Tabelle im letzten Dokument hinzu. Ich möchte Text schreiben, dann eine Tabelle hinzufügen und wieder Text schreiben.
Zurzeit werden das erste und das letzte Dokument der Tabelle hinzugefügt. 2 Test hinzufügen (Hi & Bye)
Mein Code:
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");
}
Wie kann ich druckenHi
erste Seite und Tabelle hinzufügen und druckenBye
after table?
Und wie kann ich ein Dokument jedes Mal speichern, wenn ich Inhalte hinzufügen und es schließlich schreiben und öffnen möchte?
Vielen Dan