基于Android的Word在线预览

来源:互联网 发布:诺诺助手软件电话 编辑:程序博客网 时间:2024/05/21 07:07

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

经过一些查找工作,终于找到了可以在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文件的内容。

判断当前段落是表格、图片还是一段文字的代码:

[java] view plain copy
  1. public void writeParagraphContent(Paragraph paragraph){  
  2.     Paragraph p = paragraph;  
  3.     int pnumCharacterRuns = p.numCharacterRuns();  
  4.           
  5.     forint j = 0; j < pnumCharacterRuns; j++){  
  6.       
  7.         CharacterRun run = p.getCharacterRun(j);  
  8.               
  9.         if(run.getPicOffset() == 0 || run.getPicOffset() >= 1000){  
  10.             if(presentPicture < pictures.size()){  
  11.                 writePicture();  
  12.             }  
  13.         }  
  14.         else{  
  15.             try{  
  16.                 String text = run.text();  
  17.                 if(text.length() >= 2 && pnumCharacterRuns < 2){  
  18.                     output.write(text.getBytes());  
  19.                 }  
  20.                 else{  
  21.                             int size = run.getFontSize();  
  22.                         int color = run.getColor();  
  23.                         String fontSizeBegin = "<font size=\"" + decideSize(size) + "\">";  
  24.                         String fontColorBegin = "<font color=\"" + decideColor(color) + "\">";  
  25.                         String fontEnd = "</font>";  
  26.                         String boldBegin = "<b>";  
  27.                         String boldEnd = "</b>";  
  28.                         String islaBegin = "<i>";  
  29.                         String islaEnd = "</i>";  
  30.       
  31.                         output.write(fontSizeBegin.getBytes());  
  32.                         output.write(fontColorBegin.getBytes());  
  33.                           
  34.                         if(run.isBold()){  
  35.                             output.write(boldBegin.getBytes());  
  36.                         }  
  37.                         if(run.isItalic()){  
  38.                             output.write(islaBegin.getBytes());  
  39.                         }  
  40.                           
  41.                         output.write(text.getBytes());  
  42.                           
  43.                         if(run.isBold()){  
  44.                             output.write(boldEnd.getBytes());  
  45.                         }  
  46.                         if(run.isItalic()){  
  47.                             output.write(islaEnd.getBytes());  
  48.                         }  
  49.                         output.write(fontEnd.getBytes());  
  50.                         output.write(fontEnd.getBytes());  
  51.                     }  
  52.             }  
  53.             catch(Exception e){  
  54.                 System.out.println("Write File Exception");  
  55.             }  
  56.         }  
  57.     }  
  58. }  

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

[java] view plain copy
  1. public void writePicture(){  
  2.     Picture picture = (Picture)pictures.get(presentPicture);  
  3.           
  4.     byte[] pictureBytes = picture.getContent();  
  5.           
  6.     Bitmap bitmap = BitmapFactory.decodeByteArray(pictureBytes, 0, pictureBytes.length);  
  7.           
  8.     makePictureFile();  
  9.     presentPicture++;  
  10.           
  11.     File myPicture = new File(picturePath);  
  12.           
  13.     try{  
  14.           
  15.         FileOutputStream outputPicture = new FileOutputStream(myPicture);  
  16.           
  17.         outputPicture.write(pictureBytes);  
  18.           
  19.         outputPicture.close();  
  20.     }  
  21.     catch(Exception e){  
  22.         System.out.println("outputPicture Exception");  
  23.     }  
  24.       
  25.     String imageString = "<img src=\"" + picturePath + "\"";  
  26.           
  27.     if(bitmap.getWidth() > screenWidth){  
  28.         imageString = imageString + " " + "width=\"" + screenWidth + "\"";  
  29.     }  
  30.     imageString = imageString + ">";  
  31.           
  32.     try{  
  33.         output.write(imageString.getBytes());  
  34.     }  
  35.     catch(Exception e){  
  36.         System.out.println("output Exception");  
  37.     }  
  38. }  

运行效果截图:

读取图片和文字:



读取表格和文字:


源代码及测试Word文档打包:

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

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


0 0
原创粉丝点击