Android读取word文件

  之前通过第三方jar包tm-extractors-0.4.jar,实现了一个简单的读取word文件的demo,但是只能读取word的文本数据,无法读取图片、表格数据。

  最近参考网上的例子http://blog.csdn.net/xiaoxiaobian3310903/article/details/6598500 终于能够读取word里面的图片,表格等内容。在android系统上读取Word格式文档基于的开源包--POI(The Java API For Microsoft Documents)。两个jar包 poi-3.7-20101029.jar和 poi-scratchpad-3.7-20101029.jar

  大致实现的过程是这样的:

在目录中选择要打开的word文件(.doc);

将Word格式文档中的内容读取出来,判断当前读取的段落是文本、图片、还是表格,然后加上对应的HTML标签,生成一个html文件;

然后通过一个WebView对生成的html文件进行显示。

首先是SD卡根目录选择文件

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Browse extends Activity {
    private ListView listV = null;
    private List<File> list = null;
    private int a[] = {R.drawable.doc,R.drawable.dir};
    private ArrayList<HashMap<String, Object>> recordItem;

    private File presentFile;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.browse);
        listV = (ListView)findViewById(R.id.list);
        list_files();
    }
    private void list_files(){
        //得到SD卡根目录
        File path = android.os.Environment.getExternalStorageDirectory();
        presentFile = path;
        File[] file = path.listFiles();
        fill(file);
    }
    private void fill(File[] file){
        SimpleAdapter adapter = null;
        recordItem = new ArrayList<HashMap<String, Object>>();
        list = new ArrayList<File>();
        for(File f: file){
            if(Invalid(f) == 1){
                list.add(f);
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("picture", a[0]);
                map.put("name", f.getName());
                recordItem.add(map);
            }
            if(Invalid(f) == 0){
                list.add(f);
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("picture", a[1]);
                map.put("name", f.getName());
                recordItem.add(map);
            }
        }

        adapter = new SimpleAdapter(this, recordItem, R.layout.item, new String[]{"picture", "name"}, new int[]{R.id.picture, R.id.text});
        listV.setAdapter(adapter);
        listV.setAdapter(adapter);
        listV.setOnItemClickListener(new Clicker());
    }

    private int Invalid(File f){
        if(f.isDirectory()){
            return 0;
        }
        else{
            String filename = f.getName().toLowerCase();
            if(filename.endsWith(".doc")){
                return 1;
            }
            return 2;
        }
    }
    private class Clicker implements AdapterView.OnItemClickListener {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            // TODO Auto-generated method stub
            Intent i = new Intent();
            String nameStr = null;
            i.setClass(Browse.this, ViewFile.class);
            Bundle bundle = new Bundle();
            File file = list.get(arg2);
            presentFile = file;
            if(file.isDirectory()){
                File[] files = file.listFiles();
                fill(files);

            }
            else{
                nameStr = file.getAbsolutePath();
                bundle.putString("name", nameStr);
                i.putExtras(bundle);
                startActivity(i);
                finish();
            }
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(presentFile.isDirectory()){
                if(presentFile.equals(android.os.Environment.getExternalStorageDirectory())){
                    this.finish();
                }
                else{
                    presentFile = presentFile.getParentFile();
                    File file = presentFile;
                    File[] files = file.listFiles();
                    fill(files);
                }
            }
            if(presentFile.isFile()){
                if(presentFile.getParentFile().isDirectory()){
                    presentFile = presentFile.getParentFile();
                    File file = presentFile;
                    File[] files = file.listFiles();
                    fill(files);
                }
            }
        }
        return false;
    }

}

然后,选择word文档,在WebView上显示读取的html文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

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 android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by Administrator on 2016/4/11.
 */
public class ViewFile extends Activity {

    private String nameStr = null;

    private Range range = null;
    private HWPFDocument hwpf = null;

    private String htmlPath;
    private String picturePath;

    private WebView view;

    private List pictures;

    private TableIterator tableIterator;

    private int presentPicture = 0;

    private int screenWidth;

    private FileOutputStream output;

    private File myFile;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.view);

        view = (WebView)findViewById(R.id.show);

        screenWidth = this.getWindowManager().getDefaultDisplay().getWidth() - 10;

        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        nameStr = bundle.getString("name");

        getRange();

        makeFile();

        readAndWrite();
//        view.loadUrl("content://com.android.htmlfileprovider/" + htmlPath);
//        view.loadUrl("http://m.oschina.net");

//        view.loadUrl("file:///mnt/sdcard/haha.html");
        view.loadUrl("file:///"+htmlPath);

        view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new WebViewClient());

        System.out.println("htmlPath" + htmlPath);
    }

    public boolean onCreateOptionsMenu(Menu menu){

        super.onCreateOptionsMenu(menu);
        menu.add(0, 0, 0, "关于文件").setIcon(this.getResources().getDrawable(R.drawable.importdb));
        menu.add(0, 1, 1, "关于作者").setIcon(this.getResources().getDrawable(R.drawable.exportdb));
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item){

        switch(item.getItemId()){
            case 0:
                showDialog(0);
                break;
            case 1:
                showDialog(1);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    protected Dialog onCreateDialog(int id){
        switch(id){
            case 0:
                return buildDialogProgram(ViewFile.this);
            case 1:
                return buildDialogAuthor(ViewFile.this);
        }
        return null;
    }

    private Dialog buildDialogAuthor(Context context){

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(this.getResources().getDrawable(R.drawable.dslab));
        builder.setTitle(this.getResources().getString(R.string.aboutauthor));
        builder.setMessage(this.getResources().getString(R.string.author));
        builder.setPositiveButton(this.getResources().getString(R.string.gotit), new DialogInterface.OnClickListener(){

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }

        });
        return builder.create();

    }

    private Dialog buildDialogProgram(Context context){

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(this.getResources().getString(R.string.aboutprogram));
        builder.setIcon(this.getResources().getDrawable(R.drawable.importdb));
        String programInfo = this.getResources().getString(R.string.word) + hwpf.characterLength() + "\n";
        programInfo = programInfo + this.getResources().getString(R.string.paragrap) + range.numParagraphs() + "\n";
        programInfo = programInfo + this.getResources().getString(R.string.pictures) + pictures.size() + "\n";

        builder.setMessage(programInfo);
        builder.setPositiveButton(this.getResources().getString(R.string.gotit), new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });

        return builder.create();
    }

    public void makeFile(){

        String sdStateString = android.os.Environment.getExternalStorageState();

        if(sdStateString.equals(android.os.Environment.MEDIA_MOUNTED)){
            try{
                File sdFile = android.os.Environment.getExternalStorageDirectory();

//                String path = sdFile.getAbsolutePath() + File.separator + "xiao";
                String path = sdFile.getAbsolutePath() ;
                String temp = path + File.separator + "my.html";

                File dirFile = new File(path);
                if(!dirFile.exists()){
                    dirFile.mkdir();
                }

                File myFile = new File(path + File.separator + "my.html");

                if(!myFile.exists()){
                    myFile.createNewFile();
                }

                htmlPath = myFile.getAbsolutePath();
            }
            catch(Exception e){

            }
        }
    }

    /*用来在sdcard上创建图片*/
    public void makePictureFile(){
        String sdString = android.os.Environment.getExternalStorageState();

        if(sdString.equals(android.os.Environment.MEDIA_MOUNTED)){
            try{
                File picFile = android.os.Environment.getExternalStorageDirectory();

                String picPath = picFile.getAbsolutePath() + File.separator + "xiao";

                File picDirFile = new File(picPath);

                if(!picDirFile.exists()){
                    picDirFile.mkdir();
                }
                File pictureFile = new File(picPath + File.separator + presentPicture + ".jpg");

                if(!pictureFile.exists()){
                    pictureFile.createNewFile();
                }

                picturePath = pictureFile.getAbsolutePath();

            }
            catch(Exception e){
                System.out.println("PictureFile Catch Exception");
            }
        }
    }

    public void onDestroy(){
        super.onDestroy();
    }
    /*读取word中的内容写到sdcard上的.html文件中*/
    public void readAndWrite(){

        try{
            myFile = new File(htmlPath);
            output = new FileOutputStream(myFile);
            String head = "<html><body>";
            String tagBegin = "<p>";
            String tagEnd = "</p>";

            output.write(head.getBytes());

            int numParagraphs = range.numParagraphs();

            for(int i = 0; i < numParagraphs; i++){
                Paragraph p = range.getParagraph(i);

                if(p.isInTable()){
                    int temp = i;
                    if(tableIterator.hasNext()){
                        String tableBegin = "<table style=\"border-collapse:collapse\" border=1 bordercolor=\"black\">";
                        String tableEnd = "</table>";
                        String rowBegin = "<tr>";
                        String rowEnd = "</tr>";
                        String colBegin = "<td>";
                        String colEnd = "</td>";

                        Table table = tableIterator.next();

                        output.write(tableBegin.getBytes());

                        int rows = table.numRows();

                        for( int r = 0; r < rows; r++){
                            output.write(rowBegin.getBytes());
                            TableRow row = table.getRow(r);
                            int cols = row.numCells();
                            int rowNumParagraphs = row.numParagraphs();
                            int colsNumParagraphs = 0;
                            for( int c = 0; c < cols; c++){
                                output.write(colBegin.getBytes());
                                TableCell cell = row.getCell(c);
                                int max = temp + cell.numParagraphs();
                                colsNumParagraphs = colsNumParagraphs + cell.numParagraphs();
                                for(int cp = temp; cp < max; cp++){
                                    Paragraph p1 = range.getParagraph(cp);
                                    output.write(tagBegin.getBytes());
                                    writeParagraphContent(p1);
                                    output.write(tagEnd.getBytes());
                                    temp++;
                                }
                                output.write(colEnd.getBytes());
                            }
                            int max1 = temp + rowNumParagraphs;
                            for(int m = temp + colsNumParagraphs; m < max1; m++){
                                Paragraph p2 = range.getParagraph(m);
                                temp++;
                            }
                            output.write(rowEnd.getBytes());
                        }
                        output.write(tableEnd.getBytes());
                    }
                    i = temp;
                }
                else{
                    output.write(tagBegin.getBytes());
                    writeParagraphContent(p);
                    output.write(tagEnd.getBytes());
                }
            }

            String end = "</body></html>";
            output.write(end.getBytes());
            output.close();
        }
        catch(Exception e){
            System.out.println("readAndWrite Exception");
        }
    }
    /*以段落的形式来往html文件中写内容*/
    public void writeParagraphContent(Paragraph paragraph){
        Paragraph p = paragraph;
        int pnumCharacterRuns = p.numCharacterRuns();

        for( int j = 0; j < pnumCharacterRuns; j++){

            CharacterRun run = p.getCharacterRun(j);

            if(run.getPicOffset() == 0 || run.getPicOffset() >= 1000){
                if(presentPicture < pictures.size()){
                    writePicture();
                }
            }
            else{
                try{
                    String text = run.text();
                    if(text.length() >= 2 && pnumCharacterRuns < 2){
                        output.write(text.getBytes());
                    }
                    else{
                        int size = run.getFontSize();
                        int color = run.getColor();
                        String fontSizeBegin = "<font size=\"" + decideSize(size) + "\">";
                        String fontColorBegin = "<font color=\"" + decideColor(color) + "\">";
                        String fontEnd = "</font>";
                        String boldBegin = "<b>";
                        String boldEnd = "</b>";
                        String islaBegin = "<i>";
                        String islaEnd = "</i>";

                        output.write(fontSizeBegin.getBytes());
                        output.write(fontColorBegin.getBytes());

                        if(run.isBold()){
                            output.write(boldBegin.getBytes());
                        }
                        if(run.isItalic()){
                            output.write(islaBegin.getBytes());
                        }

                        output.write(text.getBytes());

                        if(run.isBold()){
                            output.write(boldEnd.getBytes());
                        }
                        if(run.isItalic()){
                            output.write(islaEnd.getBytes());
                        }
                        output.write(fontEnd.getBytes());
                        output.write(fontEnd.getBytes());
                    }
                }
                catch(Exception e){
                    System.out.println("Write File Exception");
                }
            }
        }
    }
    /*将word中的图片写入到.jpg文件中*/
    public void writePicture(){
        Picture picture = (Picture)pictures.get(presentPicture);

        byte[] pictureBytes = picture.getContent();

        Bitmap bitmap = BitmapFactory.decodeByteArray(pictureBytes, 0, pictureBytes.length);

        makePictureFile();
        presentPicture++;

        File myPicture = new File(picturePath);

        try{

            FileOutputStream outputPicture = new FileOutputStream(myPicture);

            outputPicture.write(pictureBytes);

            outputPicture.close();
        }
        catch(Exception e){
            System.out.println("outputPicture Exception");
        }

        String imageString = "<img src=\"" + picturePath + "\"";

        if(bitmap.getWidth() > screenWidth){
            imageString = imageString + " " + "width=\"" + screenWidth + "\"";
        }
        imageString = imageString + ">";

        try{
            output.write(imageString.getBytes());
        }
        catch(Exception e){
            System.out.println("output Exception");
        }
    }
    /*处理word和html字体的转换*/
    public int decideSize(int size){

        if(size >= 1 && size <= 8){
            return 1;
        }
        if(size >= 9 && size <= 11){
            return 2;
        }
        if(size >= 12 && size <= 14){
            return 3;
        }
        if(size >= 15 && size <= 19){
            return 4;
        }
        if(size >= 20 && size <= 29){
            return 5;
        }
        if(size >= 30 && size <= 39){
            return 6;
        }
        if(size >= 40){
            return 7;
        }
        return 3;
    }
    /*处理word和html颜色的转换*/
    private String decideColor(int a){
        int color = a;
        switch(color){
            case 1:
                return "#000000";
            case 2:
                return "#0000FF";
            case 3:
            case 4:
                return "#00FF00";
            case 5:
            case 6:
                return "#FF0000";
            case 7:
                return "#FFFF00";
            case 8:
                return "#FFFFFF";
            case 9:
                return "#CCCCCC";
            case 10:
            case 11:
                return "#00FF00";
            case 12:
                return "#080808";
            case 13:
            case 14:
                return "#FFFF00";
            case 15:
                return "#CCCCCC";
            case 16:
                return "#080808";
            default:
                return "#000000";
        }
    }

    private void getRange(){
        FileInputStream in = null;
        POIFSFileSystem pfs = null;
        try{
            in = new FileInputStream(nameStr);
            pfs = new POIFSFileSystem(in);
            hwpf = new HWPFDocument(pfs);
        }
        catch(Exception e){

        }
        range = hwpf.getRange();

        pictures = hwpf.getPicturesTable().getAllPictures();

        tableIterator = new TableIterator(range);

    }
    /*处理点击返回按钮*/
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent();
            intent.setClass(ViewFile.this, Browse.class);
            startActivity(intent);
            this.finish();
        }
        return false;
    }
}

最后别忘了添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> </uses-permission>

  

时间: 2024-10-12 19:05:28

Android读取word文件的相关文章

Java读取word文件,字体,颜色

在Android读取Word文件时,在网上查看时可以用tm-extractors,但好像没有提到怎么读取Word文档中字体的颜色,字体,上下标等相关的属性.但由于需要,要把doc文档中的内容(字体,下划线,颜色等)读取应用到android中(不包括图片和图表). 后面采用的是poi三方jar包(原包太大,可以从源代码里自己抽取有用的一些代码减少包的大小). 我的想法是:把doc中的内容解析出来后,加上html对应的标签,在android中通过Html.fromHtml在TextView中进行显示

[转] C#实现在Sql Server中存储和读取Word文件

出处 C#实现在Sql Server中存储和读取Word文件 要实现在Sql Server中实现将文件读写Word文件,需要在要存取的表中添加Image类型的列,示例表结构为: CREATE TABLE CONTRACTS ( ID VARCHAR (50), CONTRACT_FILE IMAGE ); 要将Word文件存储到数据库的CONTRACT_FILE字段中,需要将文件转换为byte数组,具体代码如下: /// 将文件转换为byte数组 /// <summary> /// 将文件转换

Java读取word文件的程序演示

完成对office文件的操作可以借助apache.poi包(我用的poi-3.10-FINAL),导入相应的jar包(最好全部导入) 下面的程序演示了一些操作word的过程,详细的函数功能可以查看此包的官方API import java.io.*; import org.apache.poi.POIXMLDocument; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.extractor.*; import

Android读取Assets文件夹中的内容

Android读取Assets文件夹中的内容 assets文件夹是android程序中存放相关外部文件的一个目录,Android官方提供了相应的方法去访问该文件夹中的内容,故此我们并不需要进行相关的路径判断等代码操作,直接调用相关方法打开文件并得到一个字节输入流(InputStream):然后通过相应的字符编码方式读取字节解码为字符输入流(InputStreamReader):再通过BufferReader对字符输入流读取文本并将字符存入缓冲区以便能提供字符.数组和线段的高效读取:最后我们就能逐

通过POI读取word文件

POI读取word只能读取内容不能读取格式 tm-extractors-0.4.jar import java.io.FileInputStream; import org.textmining.text.extraction.WordExtractor; public class Test { public static void main(String[] args) { try { FileInputStream in = new FileInputStream("d:\\a.doc&qu

用python读取word文件里的表格信息【华为云技术分享】

在企查查查询企业信息的时候,得到了一些word文件,里面有些控股企业的数据放在表格里,需要我们将其提取出来. word文件看起来很复杂,不方便进行结构化.实际上,一个word文档中大概有这么几种类型的内容:paragraph(段落),table(表格),character(字符).我现在要解析的word文档中,基本都是段落和表格,本文主要来讲一下如何从word中解析出表格,并将表格信息进行结构化. 要想使用python解析word文件,我们可以使用包docx,首先我们需要安装它. 安装完成后,我

android 读取txt文件内容

Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹,放置在raw文件夹下的内容会被原样打包,而不会被编译成二进制文件,并且可以通过R文件进行很方便地访问. 比如我们可以将更新信息.版权信息等放到txt文件中,然后放到raw文件中,然后很方便地进行访问. 在raw中放入一个a.txt文件,然后就可以在Activity中使用getResources().openRawResource(R.raw.a);方法获取一个此文件的InputStream类,而后就可以很方便

arcgis for android 读取shp文件中文乱码解决方法

设置注册表默认字符,即可解决中文乱码问题. 'dbfDefault' 设置方法1.开始--运行,输入”Regedit“,打开注册表.2.如是用的是 10.x 版本 ArcGIS Desktop,定位到”My Computer\HKEY_CURRENT_USER\Software\ESRI\Desktop 10.x' (如果是9.3.1之前的版本,定位到 'My Computer\HKEY_CURRENT_USER\Software\ESRI').3.创建项 'Common',接着创建 'Code

Python中办公软件(读取word文件和读出保存别文件)

#读word文件import win32comimport win32com.client def readWordFile(path): #调用系统word功能,可以出来doc和docx两种文件 mw=win32com.client.Dispatch("Word.Application") #打开word文件 doc=mw.Documents.Open(path) for paragraphsin in doc.Paragraphs:#循环取出内容 line=paragraphsin