poi操作word
2015-03-14 23:56
253 查看
poi操作word
1、环境支持1.1 添加poi支持:包下载地址http://www.apache.org/dyn/closer.cgi/poi/release/
1.2 POI对Excel文件的读取操作比较方便,POI还提供对Word的DOC格式文件的读取。但在它的发行版本中没有发布对Word支持的模块,需要另外下载一个POI的扩展的Jar包。下载地址为http://www.ibiblio.org/maven2/org/textmining/tm-extractors/0.4/ 下载extractors-0.4_zip这个文件
2、提取Doc文件内容
public static String readDoc(String doc) throws Exception
{
// 创建输入流读取DOC文件
FileInputStream in = new FileInputStream(new File(doc));
WordExtractor extractor = null;
String text = null;
// 创建WordExtractor
extractor = new WordExtractor();
// 对DOC文件进行提取
text = extractor.extractText(in);
return text;
}
public static void main(String[] args) {
try{
String text = WordReader.readDoc("c:/test.doc");
System.out.println(text);
}catch(Exception e){
e.printStackTrace();
}
}
3、写入Doc文档
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class WordWriter {
public static boolean writeDoc(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;
}
public static void main(String[] args) throws Exception{
String wr=WordReader.readDoc("D:\\test.doc");
boolean b = writeDoc("D:\\result.doc",wr);
}
}
相关文章推荐
- 利用POI对MS Excel进行读写(可以设置写入文件的样式),利用tm-extractor对Word进行读取操作并将信息用(log4j)日志信息显示出
- poi操作word,能控制字体和颜色
- Java POI 如何操作word 格式(基础篇)
- poi操作word(一)
- Java操作Word文档(POI)
- POI-操作Word和Excel的纯Java解决方案
- poi操作word,简单写docx
- java poi操作word 2003 2007
- 利用poi操作word文档(针对docx格式)
- POI XWPF 操作word并操作表格
- POI操作WORD
- (5) POI操作word,能控制字体和颜色
- .NET版的POI NPOI(.NET Excel、WORD操作)
- 使用POI操作Excel和Word
- java 操作word 使用POI3.7
- 利用poi操作word文档(针对docx格式)
- 使用POI的HWPF操作word(未解决中文乱码问题)
- java用poi实现对word读取和修改操作
- poi操作word文档总结
- poi操作word