如何在.docx文件中的表之间添加段落或文本与apache POI

时间:2017-10-03 18:15:35

标签: java apache ms-word apache-poi

我正在尝试使用Apache POI在.docx文件中设置一些段落或文本,我正在读取我的war文件中WEB-INF / resources / templates文件夹中用作模板的.docx文件,一旦读取,我想要在第9个表用作模板后开始动态创建更多表,我可以添加更多表,但其他类型的内容(段落)安排在文档的其他部分中¿是否有必要的表单来执行此操作?< / p>

XWPFDocument doc = null;
try {
    doc = new XWPFDocument(OPCPackage.open(request.getSession().getServletContext().getResourceAsStream("/resources/templates/twd.docx")));
} catch (Exception e) {
    e.printStackTrace();
} 

XWPFParagraph parrafo = null;
XWPFTable table=null;
org.apache.xmlbeans.XmlCursor cursor = null;
XWPFParagraph newParagraph = null;
XWPFRun run = null;

for(int j=0; j < 3; j++) { //create 3 more tables
    table = doc.getTables().get(9);
    cursor = table.getCTTbl().newCursor();
    cursor.toEndToken();

    if (cursor.toNextToken() != org.apache.xmlbeans.XmlCursor.TokenType.START);
    {
        table = doc.insertNewTbl(cursor);               

        table.getRow(0).getCell(0).addParagraph().createRun()
        .setText("Name");
        table.createRow().getCell(0).addParagraph().createRun().setText("Version");
        table.createRow().getCell(0).addParagraph().createRun().setText("Description");
        table.createRow().getCell(0).addParagraph().createRun().setText("Comments");
        table.createRow().getCell(0).addParagraph().createRun().addCarriageReturn();        

        table.getRow(0).createCell().addParagraph().createRun().setText("some text");
        table.getRow(1).createCell().addParagraph().createRun().setText("some text");
        table.getRow(2).createCell().addParagraph().createRun().setText("some text");
        table.getRow(3).createCell().addParagraph().createRun().setText("some text");

        table.getRows().get(0).getCell(0).setColor("183154");
        table.getRows().get(1).getCell(0).setColor("183154");
        table.getRows().get(2).getCell(0).setColor("183154");
        table.getRows().get(3).getCell(0).setColor("183154");           
        table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(4000));
        table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(4000));
    }

    //OTHER CONTENT BETWEEN CREATED TABLES (PARAGRAPHS, BREAK LINES,ETC)
    doc.createParagraph().createRun().setText("text after table");
}

1 个答案:

答案 0 :(得分:4)

如果您曾经使用过光标,那么如果您想要位于光标所在的文档部分,则必须使用该光标进一步插入内容。不要相信,文档会自动记录您创建的光标。

例如:

import java.io.FileOutputStream;
import java.io.FileInputStream;

import org.apache.poi.xwpf.usermodel.*;

public class WordTextAfterTable {

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

  XWPFDocument document = new XWPFDocument(new FileInputStream("WordTextAfterTable.docx"));

  XWPFTable table = document.getTables().get(9);

  org.apache.xmlbeans.XmlCursor cursor = table.getCTTbl().newCursor();
  cursor.toEndToken(); //now we are at end of the CTTbl
  //there always must be a next start token after the table. Either a p or at least sectPr.
  while(cursor.toNextToken() != org.apache.xmlbeans.XmlCursor.TokenType.START); //we loop over the tokens until next TokenType.START
  //now we are at next TokenType.START and insert the new table
  //note: This is immediately after the table. So both tables touch each other.
  table = document.insertNewTbl(cursor);     
  table.getRow(0).getCell(0).addParagraph().createRun().setText("Name");
  table.createRow().getCell(0).addParagraph().createRun().setText("Version");
  table.createRow().getCell(0).addParagraph().createRun().setText("Description");
  table.createRow().getCell(0).addParagraph().createRun().setText("Comments");
  table.createRow().getCell(0).addParagraph().createRun().addCarriageReturn();        
  //...
System.out.println(cursor.isEnd()); //cursor is now at the end of the new table
  //there always must be a next start token after the table. Either a p or at least sectPr.
  while(cursor.toNextToken() != org.apache.xmlbeans.XmlCursor.TokenType.START); //we loop over the tokens until next TokenType.START
  XWPFParagraph newParagraph = document.insertNewParagraph(cursor);
  XWPFRun run = newParagraph.createRun(); 
  run.setText("text after table");

  document.write(new FileOutputStream("WordTextAfterTableNew.docx"));
  document.close();
 }
}