• 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;
    	}
    }
    


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

  • 相关阅读:
    jdbc学习一半的代码
    实验室的毕业照
    IOS_地图_定位_天气预报_Block回调_单例
    POJ -1062 昂贵的聘礼(前向星 && SPFA)
    【监控】Nagios-NRPE脚本返回值
    cocos2d-x-3.0 Windos 新建项目(coco2d-x 学习笔记一)
    托付与事件
    Netty源代码学习——EventLoopGroup原理:NioEventLoopGroup分析
    vs2013 error c4996: 'fopen': This function or variable may be unsafe
    Customize User Interfaces and Pass User Input to Installer Classes
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4711542.html
Copyright © 2020-2023  润新知