2016-01-27 3 views
2

У меня есть Java-код для создания таблицы и некоторого текстового документа с использованием Apache POI , но он добавляет таблицу в последний документ. Я хочу написать текст, затем добавить таблицу и снова написать текст.apache poi добавить таблицу в текстовый документ

В настоящее время он добавляет таблицу первым и последний документ добавьте 2 теста (Привет & Bye)

Мой код:

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

Как я могу напечатать Hi первой страницы и добавить таблицу и печать Bye после таблицы ?

И как я могу сохранять документ каждый раз, когда хочу добавить к нему контент и, наконец, написать его и открыть?

Благодаря

ответ

2

Вы должны держать правильный порядок и что более важно: Вы должны создать новый пункт.

Это код, вам нужно:

public static void main(String[] args) throws IOException { 

    //Blank Document 
    XWPFDocument document = new XWPFDocument(); 

    //Write the Document in file system 
    FileOutputStream out = new FileOutputStream(new File("create_table.docx")); 

    //Write first Text in the 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 the table (by creating a 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"); 
} 

Это выход, вы получите:

Output

Смежные вопросы