Find the table in the word and write in this table using java

I have a document with text that can have n number of tables. The table is identified by the name of the table, which is written in the 1st cell as the header. Now I need to find a table with the name of the table and write it in the cell of this table. I tried using apache-poi for the same, but couldn't figure out how to use it for my purpose. Please refer to the attached screenshot if I cannot explain what the document looks like.

thank as seen in screenshot name of tables are S1 and S2

    String fileName = "E:\\a1.doc";  

    if (args.length > 0) {  
        fileName = args[0];  
    }  

    InputStream fis = new FileInputStream(fileName);  
    POIFSFileSystem fs = new POIFSFileSystem(fis);  
    HWPFDocument doc = new HWPFDocument(fs);  

    Range range = doc.getRange(); 
    for (int i=0; i<range.numParagraphs(); i++){ 
       Paragraph tablePar = range.getParagraph(i);

        if (tablePar.isInTable()) {  
            Table table = range.getTable(tablePar);  
            for (int rowIdx=0; rowIdx<table.numRows(); rowIdx++) {  

                for (int colIdx=0; colIdx<row.numCells(); colIdx++) {  
                    TableCell cell = row.getCell(colIdx);  
                    System.out.println("column="+cell.getParagraph(0).text());  
                }  
            }  
        }  
    } 

this is what i tried but it only reads the 1st table.

+5
source share
2 answers

, POI Apache - . , , , , . Word - , , () , .

+2

. . TableIterator , , .

, .

    InputStream fis = new FileInputStream(fileName);  
    POIFSFileSystem fs = new POIFSFileSystem(fis);  
    HWPFDocument doc = new HWPFDocument(fs);  

    Range range = doc.getRange();
    TableIterator itr = new TableIterator(range);
    while(itr.hasNext()){
        Table table = itr.next();
        for(int rowIndex = 0; rowIndex < table.numRows(); rowIndex++){
            TableRow row = table.getRow(rowIndex);
            for(int colIndex = 0; colIndex < row.numCells(); colIndex++){
                TableCell cell = row.getCell(colIndex);
                System.out.println(cell.getParagraph(0).text());
            }
        }
    }
+2

All Articles