POI 关于对 ms word的读写代码
关键字: poi
read
word:
write word
写操作的代码还是有些问题:打开 WORD时提示要选择字符类型
希望能改进!
当然这几个jar是少不了的
poi-2.5.1-final-20040804.jar
poi-contrib-2.5.1-final-20040804.jar
poi-scratchpad-2.5.1-final-20040804.jar
- publicclassWordExtractor{
- publicWordExtractor(){
- }
- publicStringextractText(InputStreamin)throwsIOException{
- ArrayListtext=newArrayList();
- POIFSFileSystemfsys=newPOIFSFileSystem(in);
- DocumentEntryheaderProps=(DocumentEntry)fsys.getRoot().getEntry("WordDocument");
- DocumentInputStreamdin=fsys.createDocumentInputStream("WordDocument");
- byte[]header=newbyte[headerProps.getSize()];
- din.read(header);
- din.close();
- //Prendeleinformazionidall'headerdeldocumento
- intinfo=LittleEndian.getShort(header,0xa);
- booleanuseTable1=(info&0x200)!=0;
- //booleanuseTable1=true;
- //Prendeinformazionidallapiecetable
- intcomplexOffset=LittleEndian.getInt(header,0x1a2);
- //intcomplexOffset=LittleEndian.getInt(header);
- StringtableName=null;
- if(useTable1){
- tableName="1Table";
- }else{
- tableName="0Table";
- }
- DocumentEntrytable=(DocumentEntry)fsys.getRoot().getEntry(tableName);
- byte[]tableStream=newbyte[table.getSize()];
- din=fsys.createDocumentInputStream(tableName);
- din.read(tableStream);
- din.close();
- din=null;
- fsys=null;
- table=null;
- headerProps=null;
- intmultiple=findText(tableStream,complexOffset,text);
- StringBuffersb=newStringBuffer();
- intsize=text.size();
- tableStream=null;
- for(intx=0;x<size;x++){
- WordTextPiecenextPiece=(WordTextPiece)text.get(x);
- intstart=nextPiece.getStart();
- intlength=nextPiece.getLength();
- booleanunicode=nextPiece.usesUnicode();
- StringtoStr=null;
- if(unicode){
- toStr=newString(header,start,length*multiple,"UTF-16LE");
- }else{
- toStr=newString(header,start,length,"ISO-8859-1");
- }
- sb.append(toStr).append("");
- }
- returnsb.toString();
- }
- privatestaticintfindText(byte[]tableStream,intcomplexOffset,ArrayListtext)
- throwsIOException{
- //actualtext
- intpos=complexOffset;
- intmultiple=2;
- //skipsthroughtheprmsbeforewereachthepiecetable.Thesecontaindata
- //foractualfastsavedfiles
- while(tableStream[pos]==1){
- pos++;
- intskip=LittleEndian.getShort(tableStream,pos);
- pos+=2+skip;
- }
- if(tableStream[pos]!=2){
- thrownewIOException("corruptedWordfile");
- }else{
- //parseoutthetextpieces
- intpieceTableSize=LittleEndian.getInt(tableStream,++pos);
- pos+=4;
- intpieces=(pieceTableSize-4)/12;
- for(intx=0;x<pieces;x++){
- intfilePos=
- LittleEndian.getInt(tableStream,pos+((pieces+1)*4)+(x*8)+2);
- booleanunicode=false;
- if((filePos&0x40000000)==0){
- unicode=true;
- }else{
- unicode=false;
- multiple=1;
- filePos&=~(0x40000000);//givesmeFCindocstream
- filePos/=2;
- }
- inttotLength=
- LittleEndian.getInt(tableStream,pos+(x+1)*4)
- -LittleEndian.getInt(tableStream,pos+(x*4));
- WordTextPiecepiece=newWordTextPiece(filePos,totLength,unicode);
- text.add(piece);
- }
- }
- returnmultiple;
- }
- publicstaticvoidmain(String[]args){
- WordExtractorw=newWordExtractor();
- POIFSFileSystemps=newPOIFSFileSystem();
- try{
- Filefile=newFile("C:\\test.doc");
- InputStreamin=newFileInputStream(file);
- Strings=w.extractText(in);
- System.out.println(s);
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- }
- classWordTextPiece{
- privateint_fcStart;
- privateboolean_usesUnicode;
- privateint_length;
- publicWordTextPiece(intstart,intlength,booleanunicode){
- _usesUnicode=unicode;
- _length=length;
- _fcStart=start;
- }
- publicbooleanusesUnicode(){
- return_usesUnicode;
- }
- publicintgetStart(){
- return_fcStart;
- }
- publicintgetLength(){
- return_length;
- }
- }
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();
// Prende le informazioni dall'header del documento
int info = LittleEndian.getShort(header, 0xa);
boolean useTable1 = (info & 0x200) != 0;
//boolean useTable1 = true;
// Prende informazioni dalla piece table
int complexOffset = LittleEndian.getInt(header, 0x1a2);
//int complexOffset = LittleEndian.getInt(header);
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("C:\\test.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;
}
}
write word
- publicbooleanwriteWordFile(Stringpath,Stringcontent){
- booleanw=false;
- try{
- //byteb[]=content.getBytes("ISO-8859-1");
- byteb[]=content.getBytes();
- ByteArrayInputStreambais=newByteArrayInputStream(b);
- POIFSFileSystemfs=newPOIFSFileSystem();
- DirectoryEntrydirectory=fs.getRoot();
- DocumentEntryde=directory.createDocument("WordDocument",bais);
- FileOutputStreamostream=newFileOutputStream(path);
- fs.writeFilesystem(ostream);
- bais.close();
- ostream.close();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- returnw;
- }
public boolean writeWordFile(String path, String content) {
boolean w = false;
try {
// byte b[] = content.getBytes("ISO-8859-1");
byte b[] = content.getBytes();
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时提示要选择字符类型
希望能改进!
当然这几个jar是少不了的
poi-2.5.1-final-20040804.jar
poi-contrib-2.5.1-final-20040804.jar
poi-scratchpad-2.5.1-final-20040804.jar
- 09:21
- 浏览 (12760)
- 论坛浏览 (14400)
- 评论 (11)
- 分类: 心得
- 收藏
- 相关推荐
本文提供了使用Java POI库读取和写入Word文档的示例代码。读取部分通过解析复杂表格和文本片段来提取文档内容,而写入部分则展示了如何将字符串内容保存为Word文件。尽管存在一些限制,如对快速保存的Word文件支持不足及字符集选择问题,但对于基本的Word文档处理任务来说已经足够。


6213

被折叠的 条评论
为什么被折叠?




评论
不过也问题比较多,不能区分单元格内内容的格式,或错误判断
我看 POI对 word支持不太够啊
poi-contrib-2.5.1-final-20040804.jar
poi-scratchpad-2.5.1-final-20040804.jar
能提供这几个包的下载吗?
原先我是使用nutch的 word文本提取,但是相当大部分的中文 word文档无法正确提取,到官方网站查看他们的解决方案,是这么说的“Document with 2-byte characters (that's how Chinese characters are probably stored) are not correctly handled by HWPF.”One more thing you need to consider: HWPF cannot handle "fast saved" Word files. If the documents you need to parse are "fast saved" this adds an extra level of complexity.
有点小问题,希望楼主有时间的时候帮忙大家修复一下,那就是有部分提取的文本前后有小方框的,我想应该是这些字符本不该被提取。