• java读取word内容


    暂时只写读取word内容的方法。

    依赖的jar:

    poi-3.9-20121203.jar
    poi-ooxml-3.9-20121203.jar
    xmlbeans-2.3.0.jar

    package com.word;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    import org.apache.poi.POIXMLDocument;
    import org.apache.poi.POIXMLTextExtractor;
    import org.apache.poi.hwpf.extractor.WordExtractor;
    import org.apache.poi.openxml4j.opc.OPCPackage;
    import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
    
    /** 
     * @author QiaoJiafei 
     * @version 创建时间:2016年2月22日 上午11:30:04 
     * 类说明 
     */
    public class OptionWord {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
        }
        
        public static StringBuffer readWord(String path) {
            String s = "";
            try {
                if(path.endsWith(".doc")) {
                    InputStream is = new FileInputStream(new File(path));
                    WordExtractor ex = new WordExtractor(is);
                    s = ex.getText();
                }else if (path.endsWith("docx")) {
                    OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                    POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                    s = extractor.getText();
                }else {
                    System.out.println("传入的word文件不正确:"+path);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            StringBuffer bf = new StringBuffer(s);
            return bf;
        }
    
    }
  • 相关阅读:
    gitlab详解
    Centos7源码安装MySQL5.7
    git常用语句
    jenkins的Pipeline代码流水线管理
    jenkins配置Maven的私有仓库Nexus
    jenkins按角色授权
    jenkins集群节点构建maven(几乎是坑最多的)
    jenkins按版本发布maven项目
    jenkins发布java项目
    实现拷贝函数(strcpy)
  • 原文地址:https://www.cnblogs.com/qiaoyeye/p/5206626.html
Copyright © 2020-2023  润新知