2017-07-14 45 views
0

長時間潛伏者,第一次張貼海報。所以,我試圖使用Apache POI(Vers。3.15)在.docx文件的某個特定點插入一個表。我可以使用下面的代碼創建它。這會將我需要的表引入文檔的末尾。 (!如果這是多餘的,我道歉,試圖讓儘可能多的信息越好!)現在使用Apache POI(Java)替換.docx中的文本

/* Creates table for the questions */ 
private XWPFTable createMainTable(XWPFDocument doc, ArrayList<String> qs, ArrayList<String> avgScores, ArrayList<String> favScores){ 
    XWPFTable table = doc.createTable(); 

    int currRow = 0; 
    for(String q : qs){ 
     XWPFTableRow curRow = table.getRow(currRow); 
     if(currRow == 0){ 
      curRow.getCell(0).setText("Question"); 
     }else{ 
     curRow.getCell(0).setText(q); 
     } 
     if(currRow < qs.size()-1){ 
      table.createRow(); 
      currRow++; 
     }else{ 
      currRow++; 
     } 
    } 
    currRow = 0; 
    for(String avg : avgScores){ 
     XWPFTableRow curRow = table.getRow(currRow); 
     curRow.addNewTableCell(); 
     if(currRow == 0){ 
      curRow.getCell(1).setText(" Average Score "); 
     }else{ 
      curRow.getCell(1).setText(avg); 
     } 
     currRow++;  
    } 
    currRow = 0; 
    for(String fav : favScores){ 
     XWPFTableRow curRow = table.getRow(currRow); 
     curRow.addNewTableCell(); 
     if(currRow == 0){ 
      curRow.getCell(2).setText(" Favorable Score "); 
     }else{ 
      curRow.getCell(2).setText(fav); 
     } 
     currRow++; 
    } 
    return table; 
} 

,以替換文本的其餘部分在文件中,我用下面的方法:

/* Replace text in the document */ 
private long replaceText(ArrayList<String> comments, String targ, XWPFDocument doc) { 
long count = 0; 
for (XWPFParagraph paragraph : doc.getParagraphs()) { 
    List<XWPFRun> runs = paragraph.getRuns(); 

    StringBuilder sb = new StringBuilder(); 
    for (String c : comments){ 
     sb.append(c); 
     sb.append("|"); 
    } 
    String find = targ; 
    String repl = sb.toString(); 
    TextSegement found = paragraph.searchText(find, new PositionInParagraph()); 
    if (found != null) { 
     count++; 
     if (found.getBeginRun() == found.getEndRun()) { 
     // whole search string is in one Run 
     XWPFRun run = runs.get(found.getBeginRun()); 
     String runText = run.getText(run.getTextPosition()); 
     String replaced = runText.replace(find, repl); 
     run.setText(replaced, 0); 
     } else { 
     // The search string spans over more than one Run 
     // Put the Strings together 
     StringBuilder b = new StringBuilder(); 
     for (int runPos = found.getBeginRun(); runPos <= found.getEndRun(); runPos++) { 
      XWPFRun run = runs.get(runPos); 
      b.append(run.getText(run.getTextPosition())); 
     }      
     String connectedRuns = b.toString(); 
     String replaced = connectedRuns.replace(find, repl); 

     // The first Run receives the replaced String of all connected Runs 
     XWPFRun partOne = runs.get(found.getBeginRun()); 
     partOne.setText(replaced, 0); 
     // Removing the text in the other Runs. 
     for (int runPos = found.getBeginRun()+1; runPos <= found.getEndRun(); runPos++) { 
      XWPFRun partNext = runs.get(runPos); 
      partNext.setText("", 0); 
     }       
     } 
    }  
} 
return count;} 

第二種方法所做的是在文檔中查找某個關鍵字,並用String替換它。現在,它傳遞了一個ArrayList,但是隨着我嘗試不同的事情而改變。我能夠獲得表格的內存地址(通過將輸入更改爲XWPFTable),但是就我所能得到的那樣。我覺得我失去了一些愚蠢的東西,但我無法弄清楚。

+0

請參閱https://stackoverflow.com/questions/39572996/how-to-add-a-table-to-header-or-footer/39578388#39578388。方法:在文檔中找到您的段落。然後從該段落的'org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP'中獲取'org.apache.xmlbeans.XmlCursor'。然後插入由該光標定位的表格。 –

+0

非常感謝,我不知道我是如何在所有搜索中找不到該線程的! – ZombieShellback

回答

0

非常感謝Axel,我能夠得到理想的結果!我稍微改變了我的創建方法,並通過了一個空表來填充。然後,使用Axel建議的光標,它現在找到目標字符串(「%TABLE」),並用表格替換它,完全按照我的需要。

/* Replaces table */ 
private long replaceTable(XWPFDocument doc, ArrayList<String> qs, ArrayList<String> avgScores, ArrayList<String> favScores) { 
    XWPFTable table = null; 
    long count = 0; 
    for (XWPFParagraph paragraph : doc.getParagraphs()) { 
     List<XWPFRun> runs = paragraph.getRuns(); 
     String find = "%TABLE"; 
     TextSegement found = paragraph.searchText(find, new PositionInParagraph()); 
     if (found != null) { 
      count++; 
      if (found.getBeginRun() == found.getEndRun()) { 
      // whole search string is in one Run 
      XmlCursor cursor = paragraph.getCTP().newCursor(); 
      table = doc.insertNewTbl(cursor); 
      XWPFRun run = runs.get(found.getBeginRun()); 
      // Clear the "%TABLE" from doc 
      String runText = run.getText(run.getTextPosition()); 
      String replaced = runText.replace(find, ""); 
      run.setText(replaced, 0); 
      } else { 
      // The search string spans over more than one Run 
      StringBuilder b = new StringBuilder(); 
      for (int runPos = found.getBeginRun(); runPos <= found.getEndRun(); runPos++) { 
       XWPFRun run = runs.get(runPos); 
       b.append(run.getText(run.getTextPosition())); 
      }      
      String connectedRuns = b.toString(); 
      XmlCursor cursor = paragraph.getCTP().newCursor(); 
      table = doc.insertNewTbl(cursor); 
      String replaced = connectedRuns.replace(find, ""); // Clear search text 

      // The first Run receives the replaced String of all connected Runs 
      XWPFRun partOne = runs.get(found.getBeginRun()); 
      partOne.setText(replaced, 0); 
      // Removing the text in the other Runs. 
      for (int runPos = found.getBeginRun()+1; runPos <= found.getEndRun(); runPos++) { 
       XWPFRun partNext = runs.get(runPos); 
       partNext.setText("", 0); 
      } 
      } 
     }  
    } 
    fillTable(table, qs, avgScores, favScores); 
    return count; 
} 
相關問題