Apache POI 操作Word部分

Apache POI
                      ----Word部分
                                



最近在研究Apache POI,不过只研究了如何做word的部分。网上对于Excel等的介绍也很多例子也很多,但是对于word缺少的可怜,导致我学的也很费劲,只能算是会了皮毛。但是整理了下例子,也方便以后大家看。最好能有高手把这个word部分的知识和例子多传到网上给大家分享。 
Apache POI是一个开源的利用Java读写Excel、WORD等微软OLE2组件文档的项目。最新的版本有很多改进,加入了对采用OOXML格式的 Office 2007支持,如xlsx、docx、pptx文档。以下是POI的几个重要组成部分,以及各组件的功能概述。
POIFS是该项目的最古老,最稳定的一部分。.这是格式化OLE 2复合文档为纯Java的接口。 它同时支持读写功能。所有的组件,最终都依赖于它的定义 HSSF: MS-Excel 97-2003(.xls),基于BIFF8格式的JAVA接口。 XSSF:MS-Excel 2007+(.xlsx),基于OOXML格式的JAVA接口。
HWPF: MS-Word 97-2003(.doc),基于BIFF8格式的JAVA接口。只支持.doc文件简单的操作,读写能力有限。本API为POI项目早期开发,很不幸的 是主要负责HWPF模块开发的工程师-"Ryan Ackley"已经离开Apache组织,现在该模块没有人维护、更新、完善。 XWPF:MS-Word 2007+(.docx),基于OOXML格式的JAVA接口。较HWPF功能完善。 
Word例子:
1读取一个word里的内容,只能读取纯文字,word里不能有图片表格等,否则图片和表格就会成为乱码。输出结果在后台显示 
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.IOException;
import org.apache.poi.hwpf.extractor.WordExtractor; 
//输出文字
public class world {
public static void main(String [] args){  FileInputStream file;  try {   file = new FileInputStream("d:\\a.doc");   WordExtractor extractor;    try {        extractor = new WordExtractor(file);    String st=extractor.getText();    System.out.println(st);    } catch (IOException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }



 


   } catch (FileNotFoundException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }   }    }
用另一种方式也可以输出文字
public class world2 {   public static void main(String[] args) throws Exception{   FileInputStream file;   try {    file=new FileInputStream("d:\\a.doc");
 
 
HWPFDocument hDocument= new HWPFDocument(file);
           Range rang= hDocument.getRange();            String string=rang.text();          
         System.out.println(string);   } catch (FileNotFoundException e) {    // TODO Auto-generated catch block    e.printStackTrace();
 
}
} }
2,把从一个word里读出来的内容写到另一个里,虽然可以写进去,但是只能把文字、数字、字母等写进去,图片和表格依旧不可以。而且写进去的文字只能是字符形式写进去,这样在打开word文档时就会有个转换器的问题。但是可以写进去,不知道怎么解决。 import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.io.HWPFOutputStream; import org.apache.poi.hwpf.usermodel.Range; public class wordWrite {  public static void main(String [] args){ 
        try {
//用了HWPFDocument API对于他的解释是:作为一个存储桶,我们把所有的word数据抛出到其中 我的理解就是把输入流读取的word数据都放到他这里了 以便调用方法使用
         HWPFDocument hDocument= new HWPFDocument(new
FileInputStream("d:\\a.doc"));
           Range range= hDocument.getRange();
          
 
          String st=range.text();//获得了word里的内容
              writeDoc("d:\\a.doc",st);//调用写入的方法            System.out.println(st);



 


         } catch (Exception e) {              e.printStackTrace();          }              }
 public static boolean writeDoc(String path, String string) {   
  boolean w = false;   try {    byte b[] = string.getBytes("utf-8");
   ByteArrayInputStream bais = new ByteArrayInputStream(b);    POIFSFileSystem fs = new POIFSFileSystem();    DirectoryEntry directory = fs.getRoot();
   DocumentEntry de = directory.createDocument("WordDocument", bais);
   FileOutputStream ostream = new FileOutputStream(path);    fs.writeFilesystem(ostream);    bais.close();    ostream.close();   } catch (IOException e) {
   e.printStackTrace();
  }
 
return w;
} }
3、把word文档里的某个内容替换成所需要的别的内容。也可以说是修改成自己想要的内容。但是还是仅限于修改文字。 public class word {   public static void main(String [] args){          try {
          HWPFDocument hDocument= new HWPFDocument(new FileInputStream("d:\\a.doc"));
 
          Range range= hDocument.getRange();
 
//在这里把word里的name替换成组织 当然word里要已经有了name这个单词
其实这个时候replaceText()方法并没有真正的替换了你原有word里的内容 你打开文档里面依旧显示name 但是你看后台输出的内容是已经修改了的 这就又需要把修改的内容写进word                    range.replaceText("name","组织1");            String st=range.text();               writeDoc("d:\\a.doc",st);
           System.out.println(st);          } catch (Exception e) {              e.printStackTrace(); 
        }



 


            
}
public static boolean writeDoc(String path, String string) { 
//这里的写入我换了个方式 不过类似 依旧是以字节的形式写入 还是会有什么转换器的错误
我研究好久没解决这个问题   boolean w = false;        try {  
         byte b[] = string.getBytes(); 
         FileOutputStream fs = new FileOutputStream(“d://b.doc”);          HWPFOutputStream hos = new HWPFOutputStream();          hos.write(b, 0, b.length);          hos.writeTo(fs);          hos.close();                   w=true;
     } catch (Exception e) {            e.printStackTrace();        }        return w;  }  
 
4,把整个word里的内容读出来 写入另一个里 这次无论是图片还是文字或者表格都可以写入,而且没有那个转换器的错误了。但是其实就相当于复制了一个word到另一个word里。却无法实现修改内容在写入啊 或者别的。 public class word2 {  public static void main(String[] args) {  
try {
 
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:\\bb.doc"));
            HWPFDocument hDocument = new HWPFDocument(fs);    Range range = hDocument.getRange();
      String st = range.text();
   FileOutputStream out = new FileOutputStream("d:\\cc.doc");       fs.writeFilesystem(out);    out.flush();    out.close();
   System.out.println(st);   } catch (Exception e) {
   e.printStackTrace();  
}
  } }
5、这个是把图片从word里读出来 生成一张图片 也就是个.jpg的图片。但是我读出来却无



 





 

 

转载于:https://my.oschina.net/u/1454838/blog/345030

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值