zoukankan      html  css  js  c++  java
  • java 读取word文件

    java读取word文档,获取文本内容,保留基本的换行格式。

    java用POI对word进行解析。所需jar包,用maven引入

    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.2-FINAL</version>
    </dependency>

    前端用webuploader上传控件,限制上传文件类型仅支持text和word.

    1  accept: {
    2             title: 'text',
    3             extensions: 'txt,docx',
    4             mimeTypes: 'text/plain,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    5         }

    后台MultipartFile接收文件,根据ContentType区分文件类型,区分解析获取文件内容。

    word解析:

    1 File uFile = new File("tempFile.docx");
    2 if(!uFile.exists()){
    3    uFile.createNewFile();
    4 }
    5 FileCopyUtils.copy(file.getBytes(), uFile);
    6 OPCPackage opcPackage = POIXMLDocument.openPackage("tempFile.docx");
    7 POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
    8 txt= extractor.getText();

    txt为word的文本内容

  • 相关阅读:
    pair
    非整除集合
    集合 set
    实现字通配符*
    vector
    矩阵及其初等变换
    求数组中连续子数组(最少有一个元素)的最大和。
    最长上升序列(Lis)
    st表求区间最大值
    [Noip2015] 信息传递
  • 原文地址:https://www.cnblogs.com/java-chanjuan/p/6625034.html
Copyright © 2011-2022 走看看