Android使用ApachePOI组件读写Worddoc和docx文件【doc总结不错】

最近在项目中要生成Word的doc和docx文件,一番百度google以后,发现经过java语言实现的主流是Apache的POI组件。除了POI,这里还有另外一种实现,不过我没有去研究,有兴趣的同窗能够研究研究。html

关于POI能够访问Apache POI的官网获取详细的信息。java

进入主题!linux

因为项目中只是用到了doc和docx的组件,下面也只是介绍这两个组件的使用android

1、在Android Studio中如何用POI组件

从POI官网上看,貌似暂并不支持IntelliJ IDE,以下图,因此这里咱们采用直接下载jar包并导入项目的方式。apache

官网how to buildwindows

经过官网 ->Overview->Components,能够看到 d和docx文件分别对应着组件HWPFXWPF,而HWPF和XWPF则对应着poi-scratchpad和poi-ooxmlapi

文件类型 组件名 MavenId
doc HWPF poi-scratchpad
docx XWPF poi-ooxml

Components Mapapp


下载

进入Apache下载页面,选择最新版下载,以下。选择The latest beta release is Apache POI 3.16-beta2会跳转到poi-bin-3.16-beta2-20170202.tar.gz,而后点击poi-bin-3.16-beta2-20170202.tar.gz,选择镜像后便可成功下载。ui


linux系统选择.tar.gz
windows系统选择.zipthis

POI下载页面


解压

将下载后的压缩包解压,会获得如下文件。

文件(夹)名 做用
docs 文档(包括API文档和如何使用及版本信息)
lib doc功能实现依赖的包
ooxml-lib docx功能实现依赖的包
LICENSE  
NOTICE  
poi-3.16-beta2.jar The prerequisite poi-scratchpad-3.16-beta2.jar
poi-examples-3.16-beta2.jar 不明确
poi-excelant-3.16-beta2.jar excel功能实现
poi-ooxml-3.16-beta2.jar docx功能实现
poi-ooxml-schemas-3.16-beta2.jar The prerequisite of poi-ooxml-3.16-beta2.jar
poi-scratchpad-3.16-beta2.jar doc功能实现

解压后的poi包


导入

不熟悉怎么导入的同窗能够看看Android Studio导入jar包教程
一、doc
对于doc文件,须要将lib文件夹下jar包,poi-3.16-beta2.jar,poi-scratchpad-3.16-beta2.jar放入android项目libs目录下(lib文件夹下的junit-4.12.jar和log4j-1.2.17.jar不放个人项目也没出现异常,能少点是点)。

项目中的libs

二、docx
对于docx,须要导入lib文件夹下jar包,poi-3.16-beta2.jar,poi-ooxml-3.16-beta2.jar,poi-ooxml-schemas-3.16-beta2.jar和ooxml-lib下的包,因为一直我这一直出现Warning:Ingoring InnerClasses attribute for an anonymous inner class的错误,同时因为doc基本知足个人需求以及导入这么多jar致使apk体积增大,就没有去实现。
有兴趣的同窗能够研究研究。


2、实现doc文件的读写

Apache POI中的HWPF模块是专门用来读取和生成doc格式的文件。在HWPF中,咱们使用HWPFDocument来表示一个word doc文档。在看代码以前,有必要了解HWPFDocument中的几个概念:

名称 含义
Range 表示一个范围,这个范围能够是整个文档,也能够是里面的某个小节(Section),也能够是段落(Paragraph),还能够是拥有功能属性的一段文本(CharacterRun)
Section word文档的一个小节,一个word文档能够由多个小节构成。
Paragraph word文档的一个段落,一个小节能够由多个段落构成。
CharacterRun 具备相同属性的一段文本,一个段落能够由多个CharacterRun组成。
Table 一个表格。
TableRow 表格对应的行
TableCell 表格对应的单元格

注意 : Section、Paragraph、CharacterRun和Table都继承自Range。


读写前注意Apache POI 提供的HWPFDocument类只能读写规范的.doc文件,也就是说假如你使用修改 后缀名 的方式生成doc文件或者直接以命名的方式建立,将会出现错误“Your file appears not to be a valid OLE2 document”

Invalid header signature; read 0x7267617266202E31, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document

DOC读

读doc文件有两种方式
(a)经过WordExtractor读文件
(b)经过HWPFDocument读文件

在平常应用中,咱们从word文件里面读取信息的状况很是少见,更多的仍是把内容写入到word文件中。使用POI从word doc文件读取数据时主要有两种方式:经过WordExtractor读和经过HWPFDocument读。在WordExtractor内部进行信息读取时仍是经过HWPFDocument来获取的。

使用WordExtractor读

在使用WordExtractor读文件时咱们只能读到文件的文本内容和基于文档的一些属性,至于文档内容的属性等是没法读到的。若是要读到文档内容的属性则须要使用HWPFDocument来读取了。下面是使用WordExtractor读取文件的一个示例:

//经过WordExtractor读文件
public class WordExtractorTest {

   private final String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "test.doc");
   private static final String TAG = "WordExtractorTest";
   
   private void log(Object o) {
       Log.d(TAG, String.valueOf(o));
   }

   public void testReadByExtractor() throws Exception {
      InputStream is = new FileInputStream(PATH);
      WordExtractor extractor = new WordExtractor(is);
      //输出word文档全部的文本
      log(extractor.getText());
      log(extractor.getTextFromPieces());
      //输出页眉的内容
      log("页眉:" + extractor.getHeaderText());
      //输出页脚的内容
      log("页脚:" + extractor.getFooterText());
      //输出当前word文档的元数据信息,包括做者、文档的修改时间等。
      log(extractor.getMetadataTextExtractor().getText());
      //获取各个段落的文本
      String paraTexts[] = extractor.getParagraphText();
      for (int i=0; i<paraTexts.length; i++) {
         log("Paragraph " + (i+1) + " : " + paraTexts[i]);
      }
      //输出当前word的一些信息
      printInfo(extractor.getSummaryInformation());
      //输出当前word的一些信息
      this.printInfo(extractor.getDocSummaryInformation());
      this.closeStream(is);
   }
  
   /**
    * 输出SummaryInfomation
    * @param info
    */
   private void printInfo(SummaryInformation info) {
      //做者
      log(info.getAuthor());
      //字符统计
      log(info.getCharCount());
      //页数
      log(info.getPageCount());
      //标题
      log(info.getTitle());
      //主题
      log(info.getSubject());
   }
  
   /**
    * 输出DocumentSummaryInfomation
    * @param info
    */
   private void printInfo(DocumentSummaryInformation info) {
      //分类
      log(info.getCategory());
      //公司
      log(info.getCompany());
   }
  
   /**
    * 关闭输入流
    * @param is
    */
   private void closeStream(InputStream is) {
      if (is != null) {
         try {
            is.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

使用HWPFDocument读

HWPFDocument是当前Word文档的表明,它的功能比WordExtractor要强。经过它咱们能够读取文档中的表格、列表等,还能够对文档的内容进行新增、修改和删除操做。只是在进行完这些新增、修改和删除后相关信息是保存在HWPFDocument中的,也就是说咱们改变的是HWPFDocument,而不是磁盘上的文件。若是要使这些修改生效的话,咱们能够调用HWPFDocument的write方法把修改后的HWPFDocument输出到指定的输出流中。这能够是原文件的输出流,也能够是新文件的输出流(至关于另存为)或其它输出流。下面是一个经过HWPFDocument读文件的示例:

//使用HWPFDocument读文件
public class HWPFDocumentTest {
  
   private final String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "test.doc");
   private static final String TAG = "HWPFDocumentTest";
   
   private void log(Object o) {
       Log.d(TAG, String.valueOf(o));
   }

   public void testReadByDoc() throws Exception {
      InputStream is = new FileInputStream(PATH);
      HWPFDocument doc = new HWPFDocument(is);
      //输出书签信息
      this.printInfo(doc.getBookmarks());
      //输出文本
      log(doc.getDocumentText());
      Range range = doc.getRange();
      //读总体
      this.printInfo(range);
      //读表格
      this.readTable(range);
      //读列表
      this.readList(range);
      this.closeStream(is);
   }
  
   /**
    * 关闭输入流
    * @param is
    */
   private void closeStream(InputStream is) {
      if (is != null) {
         try {
            is.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
  
   /**
    * 输出书签信息
    * @param bookmarks
    */
   private void printInfo(Bookmarks bookmarks) {
      int count = bookmarks.getBookmarksCount();
      log("书签数量:" + count);
      Bookmark bookmark;
      for (int i=0; i<count; i++) {
         bookmark = bookmarks.getBookmark(i);
         log("书签" + (i+1) + "的名称是:" + bookmark.getName());
         log("开始位置:" + bookmark.getStart());
         log("结束位置:" + bookmark.getEnd());
      }
   }
  
   /**
    * 读表格
    * 每个回车符表明一个段落,因此对于表格而言,每个单元格至少包含一个段落,每行结束都是一个段落。
    * @param range
    */
   private void readTable(Range range) {
      //遍历range范围内的table。
      TableIterator tableIter = new TableIterator(range);
      Table table;
      TableRow row;
      TableCell cell;
      while (tableIter.hasNext()) {
         table = tableIter.next();
         int rowNum = table.numRows();
         for (int j=0; j<rowNum; j++) {
            row = table.getRow(j);
            int cellNum = row.numCells();
            for (int k=0; k<cellNum; k++) {
                cell = row.getCell(k);
                //输出单元格的文本
                log(cell.text().trim());
            }
         }
      }
   }
  
   /**
    * 读列表
    * @param range
    */
   private void readList(Range range) {
      int num = range.numParagraphs();
      Paragraph para;
      for (int i=0; i<num; i++) {
         para = range.getParagraph(i);
         if (para.isInList()) {
            log("list: " + para.text());
         }
      }
   }
  
   /**
    * 输出Range
    * @param range
    */
   private void printInfo(Range range) {
      //获取段落数
      int paraNum = range.numParagraphs();
      log(paraNum);
      for (int i=0; i<paraNum; i++) {
         log("段落" + (i+1) + ":" + range.getParagraph(i).text());
         if (i == (paraNum-1)) {
            this.insertInfo(range.getParagraph(i));
         }
      }
      int secNum = range.numSections();
      log(secNum);
      Section section;
      for (int i=0; i<secNum; i++) {
         section = range.getSection(i);
         log(section.getMarginLeft());
         log(section.getMarginRight());
         log(section.getMarginTop());
         log(section.getMarginBottom());
         log(section.getPageHeight());
         log(section.text());
      }
   }
  
   /**
    * 插入内容到Range,这里只会写到内存中
    * @param range
    */
   private void insertInfo(Range range) {
      range.insertAfter("Hello");
   }
}

DOC写

使用HWPFDocument写文件

在使用POI写word doc文件的时候咱们必需要先有一个doc文件才行,由于咱们在写doc文件的时候是经过HWPFDocument来写的,而HWPFDocument是要依附于一个doc文件的。因此一般的作法是咱们先在硬盘上准备好一个内容空白的doc文件,而后创建一个基于该空白文件的HWPFDocument。以后咱们就能够往HWPFDocument里面新增内容了,而后再把它写入到另一个doc文件中,这样就至关于咱们使用POI生成了word doc文件。

//写字符串进word
    InputStream is = new FileInputStream(PATH);
    HWPFDocument doc = new HWPFDocument(is);

    //获取Range
    Range range = doc.getRange();
    for(int i = 0; i < 100; i++) {
        if( i % 2 == 0 ) {
            range.insertAfter("Hello " + i + "\n");//在文件末尾插入String
        } else {
            range.insertBefore("      Bye " + i + "\n");//在文件头插入String
        }
    }
    //写到原文件中
    OutputStream os = new FileOutputStream(PATH);
    //写到另外一个文件中
    //OutputStream os = new FileOutputStream(其余路径);
    doc.write(os);
    this.closeStream(is);
    this.closeStream(os);

可是,在实际应用中,咱们在生成word文件的时候都是生成某一类文件,该类文件的格式是固定的,只是某些字段不同罢了。因此在实际应用中,咱们大可没必要将整个word文件的内容都经过HWPFDocument生成。而是先在磁盘上新建一个word文档,其内容就是咱们须要生成的word文件的内容,而后把里面一些属于变量的内容使用相似于“${paramName}”这样的方式代替。这样咱们在基于某些信息生成word文件的时候,只须要获取基于该word文件的HWPFDocument,而后调用Range的replaceText()方法把对应的变量替换为对应的值便可,以后再把当前的HWPFDocument写入到新的输出流中。这种方式在实际应用中用的比较多,由于它不但能够减小咱们的工做量,还可让文本的格式更加的清晰。下面咱们就来基于这种方式作一个示例。

假设咱们有个模板是这样的:

doc模板

 

以后咱们以该文件做为模板,利用相关数据把里面的变量进行替换,而后把替换后的文档输出到另外一个doc文件中。具体作法以下:

public class HWPFTemplateTest {
    /**
    * 用一个doc文档做为模板,而后替换其中的内容,再写入目标文档中。
    * @throws Exception
    */
    
     @Test
   public void testTemplateWrite() throws Exception {
      String templatePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "template.doc");

      String targetPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "target.doc";
      InputStream is = new FileInputStream(templatePath);
      HWPFDocument doc = new HWPFDocument(is);
      Range range = doc.getRange();
      //把range范围内的${reportDate}替换为当前的日期
      range.replaceText("${reportDate}", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
      range.replaceText("${appleAmt}", "100.00");
      range.replaceText("${bananaAmt}", "200.00");
      range.replaceText("${totalAmt}", "300.00");
      OutputStream os = new FileOutputStream(targetPath);
      //把doc输出到输出流中
      doc.write(os);
      this.closeStream(os);
      this.closeStream(is);
   }
  
   /**
    * 关闭输入流
    * @param is
    */
   private void closeStream(InputStream is) {
      if (is != null) {
         try {
            is.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
 
   /**
    * 关闭输出流
    * @param os
    */
   private void closeStream(OutputStream os) {
      if (os != null) {
         try {
            os.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

3、实现docx文件的读写

POI在读写word docx文件时是经过xwpf模块来进行的,其核心是XWPFDocument。一个XWPFDocument表明一个docx文档,其能够用来读docx文档,也能够用来写docx文档。XWPFDocument中主要包含下面这几种对象:

对象 含义
XWPFParagraph 表明一个段落
XWPFRun 表明具备相同属性的一段文本
XWPFTable 表明一个表格
XWPFTableRow 表格的一行
XWPFTableCell 表格对应的一个单元格

同时XWPFDocument能够直接new一个docx文件出来而不须要像HWPFDocument同样须要一个模板存在。

具体能够参考这位同窗写的POI读写docx文件


4、总结

欢迎你们提出建议和纠正本文可能存在的错误之处,感谢支持。

做者:猿湿Xoong 连接:https://www.jianshu.com/p/8d23b7f54b8e 来源:简书 简书著做权归做者全部,任何形式的转载都请联系做者得到受权并注明出处。

相关文章
相关标签/搜索