一、内容读取所需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文档依赖包";
}
}
}