Skip to content
Advertisement

Get value from auto-increment cell in Word

I have simple table in Word

enter image description here

The first column is auto-increment. How can I get the value from there?

XWPFDocument xdoc = new XWPFDocument(resource.getInputStream());
List<XWPFTable> tables = xdoc.getTables();
for (XWPFTable table : tables) {
  int i = 1;
  for (XWPFTableRow row : table.getRows()) {
    StringBuilder sb = new StringBuilder();
    for (XWPFTableCell cell : row.getTableCells()) {
      cell.getParagraphs()
          .forEach(xwpfParagraph -> sb.append(xwpfParagraph.getText())
              .append(" "));
    }
    log.info("ROW {}   : {}", String.format("%03d", i++), sb);
  }
}

The current code is working, but I can’t get the counter value

My Logs

13:04:04.035 [main] INFO  com.test.WordService - Word: Test.docx
13:04:04.293 [main] INFO  com.test.WordService - ROW 001   :  Jack London 
13:04:04.293 [main] INFO  com.test.WordService - ROW 002   :  James Hadley Chase 
13:04:04.293 [main] INFO  com.test.WordService - ROW 003   :  Agatha Mary Clarissa 

Advertisement

Answer

The reason you could not get the value of the first column is that his XWPFParagraph do not contain plain text like the other column. that’s why when you call getText, it doesn’t find anything. that’s what your first Row really contains.

Column 1 :

<xml-fragment>
    <w:pPr>
        <w:pStyle w:val="Paragraphedeliste"/>
        <w:numPr>
            <w:ilvl w:val="0"/>
            <w:numId w:val="1"/>
        </w:numPr>
    </w:pPr>
</xml-fragment>

Column 2 :

<xml-fragment>
    <w:r>
        <w:t>Jack London</w:t>
    </w:r>
</xml-fragment>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement