Java教程

java读取word文档

本文主要是介绍java读取word文档,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

通用的读取方法:
读取 doc

private static String contextOfDoc(File file){
		String str = "";
        try {
            FileInputStream fis = new FileInputStream(file);
            HWPFDocument doc = new HWPFDocument(fis);
            str = doc.getDocumentText();
            doc.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
	}

读取 docx

FileInputStream fis = null;
        XWPFDocument xdoc = null;
        XWPFWordExtractor extractor = null;
        try{
            if (suffix.endsWith(".docx")) {
                fis = new FileInputStream(file);
                xdoc = new XWPFDocument(fis);
                extractor = new XWPFWordExtractor(xdoc);
                wordText = extractor.getText();

            }
        } catch (IOException e) {
            log.error("getWordContent error", e);
        }
        finally {
            try {
                if (extractor != null){
                    extractor.close();
                }
            } catch (IOException e) {
                log.error("close stream failed", e);
            }
            CloseUtil.closeStream(fis);
        }
        //将整个文档数据字符串拆分成行数据,删除两头空格,并删除空行
        String[] lineArr = wordText.split("\r\n|\n\n|\n");
        for (String line : lineArr) {
            if (StringUtils.isNotEmpty(line.trim())) {
                lineList.add(line.trim());
            }
        }

                    
这篇关于java读取word文档的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!