笔者最近在开发一款文档阅读器,需要支持各种不同格式的文档。各种查找资料,也在网上看了各路大神的文章,今天写此文主要是为了留存笔记,同时也可以方便后来者能准确快速找到解决方案。
PS:本片内容只支持doc文档解析,其他文档类型还在摸索当中,后面将会继续更新。
笔者思路很简单:
1. 将doc文档解析成Html代码
2.通过WebView呈现
首先需要导入POI相关的jar包(红线标注):
然后直接上代码: package com.demo.x5.utils; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Picture; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Table; import org.apache.poi.hwpf.usermodel.TableCell; import org.apache.poi.hwpf.usermodel.TableIterator; import org.apache.poi.hwpf.usermodel.TableRow; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; public class DocUtil { private int mPresentPicture = 0; private List<Picture> mPictureList; private FileOutputStream output; private String mPicturePath; private int mScreenWidth; public DocUtil(int screenWidth) { this.mScreenWidth = screenWidth; } /** * 解析入口 * @param filepath doc文档绝对路径 * @param outputFilePath 输出html文件绝对路径 * */ public void docToHtml(String filepath, String outputFilePath) throws IOException { FileInputStream in = new FileInputStream(filepath); POIFSFileSystem pfs = new POIFSFileSystem(in); HWPFDocument hwpf = new HWPFDocument(pfs); Range range = hwpf.getRange(); mPictureList = hwpf.getPicturesTable().getAllPictures(); TableIterator tableIterator = new TableIterator(range); readAndWrite(range, tableIterator, outputFilePath); } /*读取word中的内容写到sdcard上的.html文件中*/ private void readAndWrite(Range range, TableIterator tableIterator, String outfile){ try{ File myFile = new File(outfile); if (myFile.exists()) { myFile.delete(); } myFile.createNewFile(); output = new FileOutputStream(myFile);