从 Docx 文件中读取表格数据 [英] Read the Tables data from the Docx files

查看:38
本文介绍了从 Docx 文件中读取表格数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read the Word Docx file and its reading fine with apache POI, but now I have a problem in reading the Docx file which has the table inside the file Kinldy anyone please help me how to read the data from the tables inside the Document. Kinldy find the screenshot of the document which I want to read with java.

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

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文档表格中提到的数据进入输出.

This was the Method I was using for returning the data in the pages, but not getting the data marked in yellow, but only those data are getting in output which are not mentioned inside the table in word document.

推荐答案

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(" ");
			}
		}
		
	}

}

这是正确答案.

这篇关于从 Docx 文件中读取表格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆