• java读取word内容


    package word;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;

    import java.io.IOException;


    import org.apache.poi.POIXMLDocument;
    import org.apache.poi.POIXMLTextExtractor;
    import org.apache.poi.hwpf.HWPFDocument;
    import org.apache.poi.hwpf.usermodel.Range;
    import org.apache.poi.openxml4j.opc.OPCPackage;
    import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;

    public class JavaWord {


    /**
    * @Title: main
    * @Description:
    * @param:
    * @return void 
    * @user: wangzg
    * @Date:2014-7-3
    * @throws
    */
    public static void main(String[] args) {

    readWord2003("doc\wzg.doc");

    //readWord2007("doc\wzg1.docx");
    }



    /**

    * @Title: readWord2003
    * @Description:
    * @param:
    * @return String 
    * @user: wangzg
    * @Date:2014-7-4
    * @throws
    */
    public static String readWord2003(String filePath) {     
            FileInputStream fis;
            HWPFDocument doc;
            String text = null;
    try {
    File f = new File(filePath);
    fis = new FileInputStream(f);
    doc = new HWPFDocument(fis);
    Range rang = doc.getRange();     
    text = rang.text();  


           System.out.println(text);
           
           fis.close();     
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }        
            return text;     
        } 

    /**

    * @Title: readWord2007
    * @Description:
    * @param:
    * @return String 
    * @user: wangzg
    * @Date:2014-7-4
    * @throws
    */
    public static String readWord2007(String filePath){

    String text = null;
    try {
               OPCPackage oPCPackage = POIXMLDocument.openPackage(filePath);
               XWPFDocument xwpf = new XWPFDocument(oPCPackage);
               POIXMLTextExtractor ex = new XWPFWordExtractor(xwpf);
               text = ex.getText();
               System.out.println(text);
               oPCPackage.close();
           } catch (FileNotFoundException e) {
               e.printStackTrace();  
           } catch (IOException e) {  
               e.printStackTrace(); 
           } 
    return text;
    }

    }


    详情:请查看poi官网——http://poi.apache.org/


  • 相关阅读:
    16、springboot——错误处理原理+定制错误页面(1)
    15、springboot——CRUD-跳转到修改员工页面+员工修改和删除实现 ⑥
    14、springboot——CRUD-跳转到添加员工页面+员工添加实现⑤
    13、springboot——CRUD-thymeleaf公共页面元素抽取④
    12、springboot——CRUD登录和拦截③
    11、springboot——CRUD国际化②
    10、springboot——CRUD导入静态资源以及设置默认访问首页①
    9、springmvc的自动配置
    8、模板引擎thymeleaf(百里香叶)
    7、对静态资源映射的规则
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6723027.html
Copyright © 2020-2023  润新知