Skip to content

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()) {
          .forEach(xwpfParagraph -> sb.append(xwpfParagraph.getText())
              .append(" "));
    }"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 


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 :

        <w:pStyle w:val="Paragraphedeliste"/>
            <w:ilvl w:val="0"/>
            <w:numId w:val="1"/>

Column 2 :

        <w:t>Jack London</w:t>