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;
} }
三、把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();
}
} }
五、这个是把图片从word里读出来 生成一张图片 也就是个.jpg的图片。可是我读出来却无
java