title: Replacing text in .docx with a table, using Apache POI (Java)
body: <p>So, I'm trying to insert a table at a certain point in a .docx file using Apache POI (Vers. 3.15). I'm able to create it, using the code below. That throws the table that I need into the end of the document. (If this is superfluous, my apologies! Trying to give as much info as possible!)</p>
  
  <pre><code>    /* Creates table for the questions */
  private XWPFTable createMainTable(XWPFDocument doc, ArrayList&lt;String&gt; qs, ArrayList&lt;String&gt; avgScores, ArrayList&lt;String&gt; 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 &lt; 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;
  }
  </code></pre>
  
  <p>Now, in order to replace the rest of the text in the document, I use the following method: </p>
  
  <pre><code>    /* Replace text in the document */
  private long replaceText(ArrayList&lt;String&gt; comments, String targ, XWPFDocument doc) {
  long count = 0;
  for (XWPFParagraph paragraph : doc.getParagraphs()) {
    List&lt;XWPFRun&gt; 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 &lt;= 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 &lt;= found.getEndRun(); runPos++) {
            XWPFRun partNext = runs.get(runPos);
            partNext.setText("", 0);
          }                          
        }
      }     
  }
  return count;}  
  </code></pre>
  
  <p>What that 2nd method does is find a certain keyword in the document, and replace it with a String. Right now, it's passed an ArrayList, but that changes as I'm trying different things out. I was able to get the memory address of the table to print out (by changing the input to an XWPFTable), but that's as far as I was able to get. I feel like I'm missing something stupid, but I just can't figure it out.</p>