zoukankan      html  css  js  c++  java
  • Java阅读word程序说明文件

    完成office文件操作可以帮助apache.poi包(我用poi-3.10-FINAL),导入对应的jar包(最好所有导入)

    以下的程序演示了一些操作word的过程,具体的函数功能能够查看此包的官方API

    import java.io.*;
    import org.apache.poi.POIXMLDocument;
    import org.apache.poi.hwpf.HWPFDocument;
    import org.apache.poi.hwpf.extractor.*;
    import org.apache.poi.hwpf.usermodel.Range;
    //xwpf专门加强处理Word2007 .docx 格式
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    
    public class WordReader {
    
    	WordExtractor wordExtractor;
    
    	public static void main(String[] args) {
    		System.out.println("该word文档(docx格式)总页数例如以下:");
    		new WordReader().getPageCount("F:\数据挖掘及其应用论文格式.docx");
    		
    		System.out.println("
    获取整个word文本内容:");
    		System.out.println(new WordReader().getTextFromWord("F:\word2003.doc"));
    		
    		System.out.println("按段获取文本内容:");
    		System.out.println(new WordReader().getTextByParagraph("F:\word2003.doc"));
    	}
    
    	// 统计word文件总页数(仅docx格式的有效!) doc格式也有对应的方法,可是因为doc本身的问题,导致获取的页数总是错误的。
    	public void getPageCount(String filePath) {
    		XWPFDocument docx;
    		try {
    			docx = new XWPFDocument(POIXMLDocument.openPackage(filePath));
    			int pages = docx.getProperties().getExtendedProperties()
    					.getUnderlyingProperties().getPages();// 总页数
    			int wordCount = docx.getProperties().getExtendedProperties()
    					.getUnderlyingProperties().getCharacters();// 忽略空格的总字符数
    			// 另外还有getCharactersWithSpaces()方法获取带空格的总字数。
    			System.out.println("Total pages=" + pages +"页; "+ " Total wordCount=" + wordCount);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	// 获取word文档中全部文本的方法(仅对doc文件有效)
    	public String getTextFromWord(String filePath) {
    		String res = null;
    		File file = new File(filePath);
    		try {
    			FileInputStream fis = new FileInputStream(file);
    			wordExtractor = new WordExtractor(fis);
    			// 获取全部文本
    			res = wordExtractor.getText();
    			fis.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return res;
    	}
    
    	// 按段获取文本(仅对doc文件有效)
    	public String getTextByParagraph(String filePath) {
    		String res = null;
    		FileInputStream fis;
    		try {
    			fis = new FileInputStream(filePath);
    			wordExtractor = new WordExtractor(fis);
    			// 获取段文本
    			String[] strArray = wordExtractor.getParagraphText();
    			for (int i = 0; i < strArray.length; i++) {
    				System.out.println("第 " + (i+1)+" 段
    "+strArray[i]);
    			}
    
    			// 这个构造函数从InputStream中载入Word文档
    			HWPFDocument doc = new HWPFDocument(
    					(InputStream) new FileInputStream(filePath));
    			// 这个类为HWPF对象模型,对文档范围段操作
    			Range range = doc.getRange();
    			int num = range.numParagraphs();
    			System.out.println("该文档共" + num + "段");//空行也算一段
    			System.out.println("获取第"+num+"段内容例如以下:
    "+range.getParagraph(num-1).text());
    			fis.close();
    
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return res;
    	}
    }
    


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    Windows下NodeJS环境搭建
    大前端是什么?
    TeamCity+Rancher+Docker实现.Net Core项目DevOps(目前成本最小的DevOps实践)
    2019春运抢票终极攻略,让你躺着也能抢到票回家!
    ASP.NET CORE 2.0 发布到IIS,IIS如何设置环境变量来区分生产环境和测试环境
    使用第三方容器服务,自动化部署.Net Core
    记React+.NetCore API实现动态列导出
    6.前端基于react,后端基于.net core2.0的开发之路(6) 服务端渲染(SSR)
    5.前端基于react,后端基于.net core2.0的开发之路(5) 配置node层,session设置、获取,请求拦截
    4.前端基于react,后端基于.net core2.0的开发之路(4) 前端打包,编译,路由,模型,服务
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4711542.html
Copyright © 2011-2022 走看看