Java在word定位表格,获取内容(通过POI)

 可以直接获取文档中所有table

        List<XWPFTable> xwpfTables = doc.getTables();
        for (XWPFTable xwpfTable : xwpfTables){
        
            List<XWPFTableRow> rows = ((XWPFTable) bodyElement).getRows();
            for (XWPFTableRow xwpfTableRow:rows){
                System.out.println("-----------------------------------------------");
                List<XWPFParagraph> paragraphs = xwpfTableRow.getCell(0).getParagraphs();
                for (XWPFParagraph xwpfParagraph : paragraphs){
                    Map<String, Object> paragraphsInfo = contentUnitService.getParagraphsInfo(doc, xwpfParagraph);
                    System.out.println(paragraphsInfo);
                }
            }
        }
但是,这样无法知道在文档中的位置,所以代码如下
Iterator<IBodyElement> bodyElementsIterator = doc.getBodyElementsIterator();
        while (bodyElementsIterator.hasNext()) {
            IBodyElement bodyElement = bodyElementsIterator.next();

            // 根据元素类型执行相应操作
            if (bodyElement instanceof XWPFParagraph) {

            } else if (bodyElement instanceof XWPFTable) {
                List<XWPFTableRow> rows = ((XWPFTable) bodyElement).getRows();
                for (XWPFTableRow xwpfTableRow:rows){
                System.out.println("-----------------------------------------------");
                List<XWPFParagraph> paragraphs = xwpfTableRow.getCell(0).getParagraphs();
                    for (XWPFParagraph xwpfParagraph : paragraphs){
                        Map<String, Object> paragraphsInfo = contentUnitService.getParagraphsInfo(doc, xwpfParagraph);
                        System.out.println(paragraphsInfo);
                    }
                }
            }
        }