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

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

问题描述

我正在尝试读取Word Docx文件及其在apache POI中的读取效果,但是现在我在读取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.

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

Have to retrieve the Highlighted data from the doc.

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

}

这是正确的答案.

This is the Correct answer for this.

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

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