基于Android的Word文档阅读器

转载自 http://blog.csdn.net/xiaoxiaobian3310903/article/details/6598500

      随着android系统的发展,android已经得到广泛的认可,作为一名普通的大学生,真的希望自己能在android系统上做一个可以让大家使用、方便大家工作的一个软件,最后决定做一个基于andriod的Word格式阅读器。

        经过一些查找工作,终于找到了可以在android系统上使用的用于读取Word格式文档的开源包--POI(The Java API For Microsoft Documents)。下载地址:http://poi.apache.org/

       POI是Apache的一个子项目,其目的是提供对基于OOXML(Microsoft Office Open XML)和OLE2(Object Linking and Embedding)的各种文档操作的Java APIs包。该项目分为几个组件,其中包括一个叫做HWPF的组件,它只能操作Word文件。这就是我将要使用的组件。HWPF的全称是Horrible Word Processor Format。翻译成中文是“可怕的文档处理格式”,利用HWPF,开发者可以用纯Java代码实现在Android系统是读取Word文档。HWPF组件是POI项目中用来实现Word文档读取的一个重要组件,以下是该组件中几个重要的类:

(1)Range:是所有HWPF对象模型的核心类,Word文档中字符的所有属性都是继承这个类得到的。

(2)HWPFDocument:文件类。任何形式的Word文档的最终表现形式都是对该对象进行一些属性的定制。

(3)Paragraph:是Word文档中基本的组成部分,每个文档都被划分成一个一个的段落,所有的段落最终组成一个Word文档。

(4)Picture:Word文档中嵌入的每张图片都是由Picture对象来表示的,它包括了图片的大小、内容等一系列属性。

(5)Table:Word文档中嵌入的每张表格都是有Table对象来表示的,它包括了表格中每行的TableRow对象和行数等属性。

       其他的类文件对以上核心的类进行功能补充,最终完成了Word 文档的读取。

       由于Word格式文档中有图片、表格和字符串,为了能在android系统上全部进行显示,选择使用WebView进行显示。首先将Word格式文档中的内容读取出来,加上对应的HTML标签,然后写入HTML文件中,最后直接使用WebView进行读取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");  
                }  
            }  
        }  
    }  

在SDCARD上创建一个图片的代码:

    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文档打包:

http://download.csdn.net/source/3432624

本文系“暑期大学生博客大赛-2011 Android成长篇“参赛文章


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值