Android实战开发篇 读取Word文档的 doc 与 docx 格式文本内容(全网最详细!!!)

该博客介绍了在Android中使用Apache POI库读取doc和docx文档的步骤,包括导入相关jar包、配置Android Studio、设置权限以及处理异常。详细讲解了读取操作,并提供了读取工具类的代码示例。
摘要由CSDN通过智能技术生成

一、内容读取所需jar包导入

异常崩溃点1以下异常均为jar包未导全

1、java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/xml/stream/XMLStreamReader;
在这里插入图片描述

2、RuntimeException异常 :无法正确读取文本内容
在这里插入图片描述

在这里插入图片描述
3、org.xml.sax.SAXNotRecognizedException: http://xml.org/sax/properties/declaration-handler

    // https://mvnrepository.com/artifact/com.android.support/multidex
    implementation group: 'com.android.support', name: 'multidex', version: '1.0.3'
    // https://mvnrepository.com/artifact/commons-codec/commons-codec
    implementation group: 'commons-codec', name: 'commons-codec', version: '1.14'
    // https://mvnrepository.com/artifact/org.apache.poi/poi
    implementation group: 'org.apache.poi', name: 'poi', version: '3.9'
    // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
    implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '3.9'
    // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas
    implementation group: 'org.apache.poi', name: 'poi-ooxml-schemas', version: '3.9'
    // https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad
    implementation group: 'org.apache.poi', name: 'poi-scratchpad', version: '3.9'
    // https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans
    implementation group: 'org.apache.xmlbeans', name: 'xmlbeans', version: '2.3.0'
    // https://mvnrepository.com/artifact/dom4j/dom4j
    implementation group: 'dom4j', name: 'dom4j', version: '1.6.1'
    // https://mvnrepository.com/artifact/stax/stax-api
    implementation group: 'stax', name: 'stax-api', version: '1.0.1'

二、Android studio - jar 配置

第一项:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
       // 镜像 : 
       // maven { url 'https://maven.aliyun.com/repository/public' }
       // maven { url 'https://maven.aliyun.com/repository/central' }
    }

第二项:config.gradle / build.gradle (app)

defaultConfig {
    ...
    multiDexEnabled true
}

第三项:文档读取有JDK版本限制,最好是更新至1.8(异常崩溃点2

android {
   .......
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

三、权限配置

<!-- 外部存储 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- 管理外部存储 -->
    <uses-permission
        android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />

异常点3: AndroidMainfest.xml 的 <application …
内添加 android:requestLegacyExternalStorage="true" 否则就会提示下列异常:在这里插入图片描述
在这里插入图片描述

四、读取doc 、 docx

import android.util.Log;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName : WordUtil.java
 * @Function : 文档读取工具类
 * @Description :  1、AndroidManifest - 权限
 *                  2、JDK 1.8
 *                  3、defaultConfig
 *                  4、全家桶依赖
 * @Idea :
 * {@link  }
 * @Encourage :Do everything you can right now, and then decide.
 * 全力以赴,历而后择。
 * @date : 2021/8/26
 */
public class WordUtil {

    /**
     * 阅读 Word 文档 - doc 格式文件
     *
     * @param filePath doc 文件路径
     * @return 仅文档内容
     */
    public static String readWordDoc(String filePath) {
        try {
            FileInputStream in = new FileInputStream(filePath);
            //PoiFs :管理整个文件系统生命周期
            POIFSFileSystem pfs = new POIFSFileSystem(in);
            //获取文档所有的数据结构 : 文档对象
            HWPFDocument hwpfDocument = new HWPFDocument(pfs);
            return hwpfDocument.getText().toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "请检查是否授予了权限 or Word文档依赖包";
        }
    }

    /**
     * 阅读 Word 文档 - docx 格式文件
     *
     * @param filePath docx 文件路径
     * @return 仅文档内容
     */
    public static String readWordDocx(String filePath) {
        try {
            InputStream is = new FileInputStream(filePath);
            XWPFDocument doc = new XWPFDocument(is);
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
            return extractor.getText();
        } catch (Exception e) {
            e.printStackTrace();
            return "请检查是否授予了权限 or Word文档依赖包";
        }
    }

}

五、其他

1、扫描文档
2、读取txt文档并解决转码问题
3、fileparse 文件解析工具

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
### 回答1: Java可以使用Apache POI库来读取Word文档,包括docdocx格式。具体步骤如下: 1. 导入POI库的依赖,可以使用Maven或手动下载jar包。 2. 创建一个File对象,指定要读取Word文档的路径。 3. 根据文档的格式,创建不同的XWPFDocument或HWPFDocument对象。 4. 使用XWPFDocument或HWPFDocument对象的方法来读取文档内容,如getText()方法。 5. 关闭文档对象和文件流。 示例代码: // 导入POI库的依赖 import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument; // 创建File对象 File file = new File("path/to/word/document.docx"); // 创建XWPFDocument或HWPFDocument对象 XWPFDocument docx = new XWPFDocument(new FileInputStream(file)); HWPFDocument doc = new HWPFDocument(new FileInputStream(file)); // 读取文档内容 String text = docx.getText(); String text = doc.getText(); // 关闭文档对象和文件流 docx.close(); doc.close(); ### 回答2: Java作为一个高级编程语言,被广泛用于各种应用开发中,包括处理Office文档。在处理Office文档中,常常会需要读取Word文档docdocx格式,因此,Java也提供了相应的API来实现这个功能。 对于doc格式Word文档,可以使用POI(Poor Obfuscation Implementation)API来读取,该API是Apache基金会提供的开源API之一,完全用Java实现。通过POI API,可以读取Word文档文本内容格式,例如字体、颜色、样式等。使用POI API读取Word文档需要使用到以下几个类: 1. HSSFWorkbook:代表一个Excel文档。 2. HSSFSheet:代表Excel文档中的一个Sheet。 3. HSSFRow:代表一行数据。 4. HSSFCell:代表一个单元格。 5. HSSFCellStyle:代表一个单元格样式。 6. HSSFFont:代表一个字体。 7. HSSFColor:代表一个颜色。 对于docx格式Word文档,Java也提供了相应的API,可以使用Apache POI或者其他第三方库,如OpenXML4J来读取。OpenXML4J是一个用于处理OOXML(Office Open XML)格式的Java库,提供了读写Word文档、Excel表格和PowerPoint演示文稿等功能。使用该库读取docx格式Word文档需要使用到以下类: 1. OPCPackage:代表一个OOXML包。 2. XWPFDocument:代表一个Word文档。 3. XWPFParagraph:代表一个段落。 4. XWPFRun:代表一个文本运行。 5. XWPFTable:代表一个表格。 6. XWPFTableRow:代表一个表格行。 7. XWPFTableCell:代表一个表格单元格。 读取docx格式Word文档相对于doc格式Word文档更加方便,因为docx格式是一种基于XML的格式,其内部结构比较清晰,易于解析。同时,与POI API相比,OpenXML4J库还提供了更多的高级功能,例如修改和创建Word文档、获取文档元数据等。 总之,Java有多种方式可以读取Word文档docdocx格式开发人员可以根据自己的需求选择合适的API。不过,在具体开发过程中,需要注意文件格式的判断和兼容性问题,以确保程序的稳定性和兼容性。 ### 回答3: Java读取Word文档docdocx的过程需要使用Apache POI Library。POI是一组Java开发人员广泛使用的开源API集合,用于处理Microsoft Office格式的文件,例如Word文档,Excel电子表格和PowerPoint演示文稿等。 首先需要在Java项目中引入POI library,可以在Maven或Gradle中添加依赖项,或者直接下载POI Jar文件导入到项目中。然后针对不同的Word文档格式,采用不同的方式进行读取。 对于doc格式Word文档,可以使用HWPFDocument类读取。以下是一个简单的代码示例: ``` File file = new File("example.doc"); FileInputStream fis = new FileInputStream(file.getAbsolutePath()); HWPFDocument document = new HWPFDocument(fis); Range range = document.getRange(); String text = range.text(); System.out.println(text); fis.close(); ``` 这个例子中,首先创建一个File对象来指定要读取Word文档,然后使用FileInputStream读取文件流。接着使用HWPFDocument类打开文件流并获取文档的Range范围,最后使用Range对象的text()方法获取文本内容。 而对于docx格式Word文档,需要使用XWPFDocument类读取。以下是一个简单的代码示例: ``` File file = new File("example.docx"); FileInputStream fis = new FileInputStream(file.getAbsolutePath()); XWPFDocument document = new XWPFDocument(fis); XWPFWordExtractor extractor = new XWPFWordExtractor(document); String text = extractor.getText(); System.out.println(text); fis.close(); ``` 这个例子中,同样首先创建一个File对象来指定要读取Word文档,然后使用FileInputStream读取文件流。接着使用XWPFDocument类打开文件流并创建XWPFWordExtractor对象,使用getText()方法获取文本内容。 总的来说,使用POI Library可以轻松地读取Word文档内容,不论是doc还是docx格式的文档。但需要注意的是,POI是用于读取和写入 Office 格式文件的API,并不是一个完整的文档编辑器,因此可能需要额外的处理才能获得符合需求的结果。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

其子昱舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值