2017-08-28 234 views
0

我在.docx文件中做了一個標記,如${table},現在我想在這個標記的位置插入一個包含apache poi xwpf的表。如何在poi XWPF的指定位置插入表格?

換句話說,我想用表格替換這個標記${table}

這裏是我的代碼

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

如何過,可以創建表,但它出現在Word文檔的底部。

我該怎麼辦?

回答

0

這裏是代碼

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(); } } 
相關問題