how to insert the table at the specified location with poi XWPF?

2.8k views Asked by At

I made a mark in a .docx file like ${table} ,and now I want insert a table with apache poi xwpf in this marked place.

In other words I want to replace this mark ${table} with a table.

Here is my code

List<XWPFParagraph> parasList = document.getParagraphs();
for(XWPFParagraph paragraph:parasList){
    String paraText=paragraph.getText();
    if(paraText.equals("${table}")) {
        System.out.println("find:"+paraText);
        int pos=document.getPosOfParagraph(paragraph);
        XWPFTable newT=document.createTable();
        document.insertTable(pos,newT);
    }
}

How ever, the table can be created, but it appeared at the bottom of the word document.

What should I do?

1

There are 1 answers

0
Meng.Qi On BEST ANSWER

here is code

public static void main(String[] args) throws IOException, InvalidFormatException { 
    String filepath = "D:\\users\\IDEA\\POIUtils\\doc\\测试文档.docx"; 
    String destpath = "D:\\users\\IDEA\\POIUtils\\doc\\测试文档_result.docx"; 
    OPCPackage opcPackage = POIXMLDocument.openPackage(filepath); 
    XWPFDocument xwpfDocument = new XWPFDocument(opcPackage); 
    List<XWPFParagraph> xwpfParas = xwpfDocument.getParagraphs(); 
    int num=0; 
    for(int i=0;i<xwpfParas.size();i++){   
        if(num==3) break;   
        XWPFParagraph xwpfParagraph = xwpfParas.get(i);   
        String text=xwpfParagraph.getText();   
        //插入段落   
        if(text.equals("${mark_newParagraph}")){
            XmlCursor cursor = xwpfParagraph .getCTP().newCursor();
            XWPFParagraph newPara = xwpfDocument.insertNewParagraph(cursor);
            newPara.setAlignment(ParagraphAlignment.BOTH);//两端对齐
            newPara.setIndentationFirstLine(480);//首行缩进24磅
            XWPFRun newParaRun = newPara.createRun();
            newParaRun.setText("这是新插入的段落!");
            newParaRun.setFontFamily("宋体");
            newParaRun.setFontSize(12);
            newParaRun.setBold(false);


xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph)); 
          }   //插入表格   if(text.equals("${mark_newTable}")){
          XmlCursor cursor= xwpfParagraph.getCTP().newCursor();
          XWPFTable table = xwpfDocument.insertNewTbl(cursor);
          XWPFTableRow row_0 = table.getRow(0);
          row_0.getCell(0).setText("姓名");
          row_0.addNewTableCell().setText("年龄");
          XWPFTableRow row_1 = table.createRow();
          row_1.getCell(0).setText("隔壁老王");
          row_1.getCell(1).setText("48");
          setTableLocation(table,"center");
          setCellLocation(table,"CENTER","center");

xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph));
          }   
          //插入图片   if(text.equals("${mark_newPicture}")){
          XmlCursor cursor = xwpfParagraph .getCTP().newCursor();
          XWPFParagraph newPara = xwpfDocument.insertNewParagraph(cursor);
          newPara.setAlignment(ParagraphAlignment.CENTER);//居中
  XWPFRun newParaRun = newPara.createRun();
  newParaRun.addPicture(new FileInputStream("./doc/bus.png"),XWPFDocument.PICTURE_TYPE_PNG,"bus.png,",Units.toEMU(200), Units.toEMU(200));   xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph)); } } FileOutputStream outStream = null; try {
outStream = new FileOutputStream(destpathString);
xwpfDocument.write(outStream);
outStream.flush();
outStream.close(); } catch (IOException e) {
e.printStackTrace(); } }