从Docx文件中读取Tables数据

时间:2017-05-17 05:30:07

标签: java apache-poi

我正在尝试使用apache POI阅读Word Docx文件及其读取,但现在我在阅读文件中包含文件的Docx文件时遇到问题 Kinldy有人请帮助我如何从文档内的表格中读取数据。 Kinldy找到了我想用java阅读的文档的截图。 enter image description here

必须从文档中检索突出显示的数据。

public static void readDocxFile(String fileName){
        try {
            File file = new File(fileName);
            FileInputStream fis = new FileInputStream(file.getAbsolutePath());
            XWPFDocument document = new XWPFDocument(fis);

            List<XWPFParagraph> paragraphs = document.getParagraphs();

            System.out.println("Total Number of Paragraphs:: "+paragraphs.size());
            for (int i = 0; i < paragraphs.size(); i++) {
                System.out.println(paragraphs.get(i).getParagraphText());
            }
            fis.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我用来返回页面中数据的方法,但没有将数据标记为黄色,但只有那些数据输入到word文档表格中未提及的输出中。

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
public class ReadTableWord {
 static String temp = "";
static String cellValue;
	public static void main(String[] args) throws IOException {
		
		
		File file = new File("D:/Test111/BRD-+machine-usage+updation.docx");
		FileInputStream fis = new FileInputStream(file);
		XWPFDocument doc = new XWPFDocument(fis);
		List<XWPFTable> tables = doc.getTables();
	
		for (XWPFTable table : tables) {
			for (XWPFTableRow row : table.getRows()) {
				for (XWPFTableCell cell : row.getTableCells()) {
					System.out.println(cell.getText());
					String sFieldValue = cell.getText();
					if (sFieldValue.matches("Whatever you want to match with the string") || sFieldValue.matches("Approved")) {
						System.out.println("The match as per the Document is True");
					}
//					System.out.println("\t");
				}
				System.out.println(" ");
			}
		}
		
	}

}
&#13;
&#13;
&#13;

这是对此的正确答案。