急,高手指教,用poi来解析.doc,docx,rtf,txt

无所事事的猫 2009-08-03 10:26:40
想实现解析简历功能,用poi来解析.doc,docx,rtf,txt。
但是要过滤出来一些信息,像name,tel,email,address这些信息.(简历全是英文的)
请问大家有什么好方法、好的思路吗??
都好长时间了,一直没有想出什么好方法来实现,请高手指教。,谢谢~
...全文
539 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
javaxyf 2012-09-07
  • 打赏
  • 举报
回复
请问,楼主,解析rtf ,获取rtf 内容成功了吗?
treedesert 2010-06-11
  • 打赏
  • 举报
回复
收藏下,遇到相同的问题,但文本格式这块怎么也得不到。
movingchina 2009-08-04
  • 打赏
  • 举报
回复
只能是一行一行读取,匹配字符串。
zhengpeiyong 2009-08-04
  • 打赏
  • 举报
回复
除了字符串匹配,想不到有什么简洁精确的办法了
meadking 2009-08-04
  • 打赏
  • 举报
回复
提取字符串:正则过滤,
或者按照格式提取...
老张-AI 2009-08-04
  • 打赏
  • 举报
回复
给楼主找的一个例子


POI是Apache的一个开源项目,可以到Apache网站下载相应的jar包文件,及其源文件。

POI提供了提取一些非TXT文本中文本内容的API,比如提取Word,Excel等,使用起来非常方便。

为了说明POI提起Word文件的方便和简单,通过提取一个Word文件的文本来,来了解POI API的功能。

假设在本地磁盘中存在一个Word文件

E:\POI\word\JBoss3.0 下配置和部署EJB简介.doc文件是具有格式的,内容如图所示:


下面看看提取它的内容是多么简单。

首先从Apache网站上下载POI的相关jar包。

新建一个测试类:

package org.shirdrn.word;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hwpf.extractor.WordExtractor;

public class MyWordExtractor {

public static void main(String[] args) {
File file = new File("E:\\POI\\word\\JBoss3.0 下配置和部署EJB简介.doc");
try {
FileInputStream fis = new FileInputStream(file);
WordExtractor wordExtractor = new WordExtractor(fis);
System.out.println("【 使用getText()方法提取的Word文件的内容如下所示:】");
System.out.println(wordExtractor.getText());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

提取Word文件的文本内容,打印到控制台上,如下所示:

使用WordExtractor类的getTextFromPieces()方法提取:

wordExtractor.getTextFromPieces();

结果和上面是一样的。

WordExtractor类还有一个可以提取Word文件的各个段落的方法getParagraphText(),返回一个String[]数组,数组中每个元素为一个段的文本内容。

这里,对Word文件中换行也看成是一个段,测试如下:

package org.shirdrn.word;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hwpf.extractor.WordExtractor;

public class MyWordExtractor {

public static void main(String[] args) {
File file = new File("E:\\POI\\word\\JBoss3.0 下配置和部署EJB简介.doc");
try {
FileInputStream fis = new FileInputStream(file);
WordExtractor wordExtractor = new WordExtractor(fis);
System.out.println("【 使用getText()方法提取的Word文件的内容如下所示:】");
String[] paragraph = wordExtractor.getParagraphText();
System.out.println("该Word文件共有"+paragraph.length+"段。");
for(int i=0;i<paragraph.length;i++){
System.out.println("< 第 "+(i+1)+" 段的内容为 >");
System.out.println(paragraph[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

提取Word文件的文本内容,打印到控制台上,如下所示:

从上面的Word文件可以看出,最后一行是Word文件的一个换行符,使用WordExtractor提取时,也把它默认成为一个段,因为一个段结束后应该有一个回车换行符。

如果有多个Word文件,而且放在不同的目录下,要提取它们的文本内容,可以实现一个递归的函数,通过深度遍历,为每一个Word文件进行提取。

如果需要,可以将提取到的Word文件的文本内容输出到本地磁盘中,比如以txt记事本的根式保存。

从上面可以看出,提取Word文件的文本内容,实际上是将Word文件的格式去掉了,获取到文本的内容。
czp3158 2009-08-04
  • 打赏
  • 举报
回复
我也期待着有能看到相关的介绍!
bigpretty 2009-08-04
  • 打赏
  • 举报
回复
我没有用过poi,但是我在做一个解析程序,其实很简单,就是对你现在的文挡进行全文浏览,然后用正则表达式对其匹配,匹配出你需要的内容,然后存储就可以了.希望可以帮到lz!
无所事事的猫 2009-08-04
  • 打赏
  • 举报
回复
谢谢,我正在试朋友们给我提供的方法。非常感谢
无所事事的猫 2009-08-03
  • 打赏
  • 举报
回复
谢谢1楼的热情帮助。不过我这一步也已经实现了。现在的问题就是过滤,从里面得到那些信息。
简历的模板也有好多种,不是固定的。很头疼
yqsshr 2009-08-03
  • 打赏
  • 举报
回复
解析word很简单,我给楼主贴一个,就是不知道怎么过滤

package com.psp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.LittleEndian;

public class WordExtractor {
public WordExtractor() {
}

public String extractText(InputStream in) throws IOException {
ArrayList text = new ArrayList();
POIFSFileSystem fsys = new POIFSFileSystem(in);

DocumentEntry headerProps = (DocumentEntry) fsys.getRoot().getEntry("WordDocument");
DocumentInputStream din = fsys.createDocumentInputStream("WordDocument");
byte[] header = new byte[headerProps.getSize()];

din.read(header);
din.close();
int info = LittleEndian.getShort(header, 0xa);

boolean useTable1 = (info & 0x200) != 0;

int complexOffset = LittleEndian.getInt(header, 0x1a2);

String tableName = null;
if (useTable1) {
tableName = "1Table";
} else {
tableName = "0Table";
}

DocumentEntry table = (DocumentEntry) fsys.getRoot().getEntry(tableName);
byte[] tableStream = new byte[table.getSize()];

din = fsys.createDocumentInputStream(tableName);

din.read(tableStream);
din.close();

din = null;
fsys = null;
table = null;
headerProps = null;

int multiple = findText(tableStream, complexOffset, text);

StringBuffer sb = new StringBuffer();
int size = text.size();
tableStream = null;

for (int x = 0; x < size; x++) {

WordTextPiece nextPiece = (WordTextPiece) text.get(x);
int start = nextPiece.getStart();
int length = nextPiece.getLength();

boolean unicode = nextPiece.usesUnicode();
String toStr = null;
if (unicode) {
toStr = new String(header, start, length * multiple, "UTF-16LE");
} else {
toStr = new String(header, start, length, "ISO-8859-1");
}
sb.append(toStr).append(" ");

}
return sb.toString();
}

private static int findText(byte[] tableStream, int complexOffset, ArrayList text)
throws IOException {
//actual text
int pos = complexOffset;
int multiple = 2;
//skips through the prms before we reach the piece table. These contain data
//for actual fast saved files
while (tableStream[pos] == 1) {
pos++;
int skip = LittleEndian.getShort(tableStream, pos);
pos += 2 + skip;
}
if (tableStream[pos] != 2) {
throw new IOException("corrupted Word file");
} else {
//parse out the text pieces
int pieceTableSize = LittleEndian.getInt(tableStream, ++pos);
pos += 4;
int pieces = (pieceTableSize - 4) / 12;
for (int x = 0; x < pieces; x++) {
int filePos =
LittleEndian.getInt(tableStream, pos + ((pieces + 1) * 4) + (x * 8) + 2);
boolean unicode = false;
if ((filePos & 0x40000000) == 0) {
unicode = true;
} else {
unicode = false;
multiple = 1;
filePos &= ~(0x40000000); //gives me FC in doc stream
filePos /= 2;
}
int totLength =
LittleEndian.getInt(tableStream, pos + (x + 1) * 4)
- LittleEndian.getInt(tableStream, pos + (x * 4));

WordTextPiece piece = new WordTextPiece(filePos, totLength, unicode);
text.add(piece);

}

}
return multiple;
}
public static void main(String[] args){
WordExtractor w = new WordExtractor();
//POIFSFileSystem ps = new POIFSFileSystem();
try{

File file = new File("E:\\资料\\相关问题和论文.doc");

InputStream in = new FileInputStream(file);
String s = w.extractText(in);
System.out.println(s);


}catch(Exception e){
e.printStackTrace();
}

}

}
class WordTextPiece {
private int _fcStart;
private boolean _usesUnicode;
private int _length;

public WordTextPiece(int start, int length, boolean unicode) {
_usesUnicode = unicode;
_length = length;
_fcStart = start;
}
public boolean usesUnicode() {
return _usesUnicode;
}

public int getStart() {
return _fcStart;
}
public int getLength() {
return _length;
}

}

俺不太懂,帮楼主顶一下。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧