- 浏览: 104194 次
- 性别:
- 来自: 深圳
最新评论
-
harry_2013:
太感谢楼主了。。。。
一步一步创建一个结构清晰的Web Dynpro -
昔日舞曲:
下 了 ,谢谢
Sap NetWeaver 架构分析 -
昔日舞曲:
今天刚培训这个
多谢楼主
一步一步创建一个结构清晰的Web Dynpro -
yezeguo:
原来是在处理表达式,我以为是在做计算器
java 计算器 的实现 -
Surmount_ego:
最近估计会涉及到EP所以先学习下
一步一步创建一个结构清晰的Web Dynpro
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
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
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
评论
13 楼
liujun999999
2007-09-18
收藏,谢谢啦
12 楼
cleanboxer
2007-06-18
开发poi word 的那个哥们老早就不干了,好像就职于商业的公司,apache还招募人参与呢,2006的事, poi处理word 太弱,还得用vs6.0 c++, java的开源的那个word有更详细的文档,不过没研究过,处理excel还算凑合,
不过也问题比较多,不能区分单元格内内容的格式,或错误判断
不过也问题比较多,不能区分单元格内内容的格式,或错误判断
11 楼
jlusdy
2007-06-15
问个问题,写work java哪个开源包比较好
我看POI对word支持不太够啊
我看POI对word支持不太够啊
10 楼
lfhaha
2007-04-13
收藏
9 楼
andyandyandy
2007-04-02
写word的少见,收了
8 楼
strongkill
2007-04-02
http://jakarta.apache.org/poi/index.html
7 楼
dy.f
2007-03-15
poi-2.5.1-final-20040804.jar
poi-contrib-2.5.1-final-20040804.jar
poi-scratchpad-2.5.1-final-20040804.jar
能提供这几个包的下载吗?
poi-contrib-2.5.1-final-20040804.jar
poi-scratchpad-2.5.1-final-20040804.jar
能提供这几个包的下载吗?
6 楼
transist
2007-03-06
感谢楼主,这个word extractor能够比较好的支持中文。
原先我是使用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.
有点小问题,希望楼主有时间的时候帮忙大家修复一下,那就是有部分提取的文本前后有小方框的,我想应该是这些字符本不该被提取。
原先我是使用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.
有点小问题,希望楼主有时间的时候帮忙大家修复一下,那就是有部分提取的文本前后有小方框的,我想应该是这些字符本不该被提取。
5 楼
sprite
2007-02-07
希望楼主把表情符号关掉 重新编辑一下 让大家能欣赏到正确的代码
4 楼
fish922
2007-02-07
写word的代码不对!
3 楼
minimu
2006-09-14
收藏下
2 楼
抛出异常的爱
2006-09-14
代码里应该关了表情符号吧
1 楼
java虫
2006-09-14
不错,收藏一下。论坛里这方面帖子不多
发表评论
-
rpmbuild signature public key
2009-02-27 10:22 1420生成 RPM-GPG-KEY-test [root@ ~]# ... -
kickstart linux installation 自动linux安装制作
2009-02-17 11:47 1676自动linux安装制作 关健是ks.cfg这个文件 insta ... -
yum and rpmbuild
2008-12-26 10:16 22201 yum 是一种rpm高级管理包工具 它有效的维护了包所有d ... -
公司注消 我的合同怎么办?
2008-11-19 21:23 1092之前公司是在深圳注册,并且我的合同是和公司深圳分公司签的 然后 ... -
linux mount problem
2008-05-20 11:16 1190when mount the sysfile, it appe ... -
lusence 搜索引挚 另一个选择 MG4J
2008-03-21 10:00 3600MG4J 是另一个搜索engine 与lusence 主要区别 ... -
sybase procedures
2008-03-06 16:47 2045List of procedures MODIFIED SY ... -
redhat 4 上的oracle,sybase,db2 安装 恢复
2008-03-06 14:43 1556这几个数据库安装的时候,注意把环境变量设置起来 它们有相应的角 ... -
linux 下的 command
2008-02-20 17:08 987uname -a top iostat rpm cp ... -
linux 下的cvs , telent 配置
2008-02-20 17:07 1494各自的配置文件: #/etc/xinetd.d/cvs # ... -
双 linux 启动
2008-02-20 17:05 1479记录一下 简单的分区 /dev/hda1 redhat ... -
买房子全过程
2007-05-06 13:41 1326选房子 多种渠道 1 网上找 2 报纸上 3 到熟悉的小区直 ... -
architecture
2007-04-06 09:43 37第一种 1. Design screen mockups 2. ... -
backbase validate datagrid's data
2007-01-17 18:37 1476在提交之前验证datagrid的数据可以手动来做。请参照如下代 ... -
backbase How to dynamically hide or display some columns
2007-01-17 18:33 1774How to dynamically hide or disp ... -
backbase Read-time data refresh for listgrid
2007-01-17 18:29 1982Read-time data refresh for list ... -
backbase How to keep the order for every column after drag
2007-01-17 18:26 1736How to keep the order for ev ... -
backbase
2007-01-17 09:50 1601backbase 是一个企业级的AJAX框架 它分成二部分 j ...
相关推荐
使用POI读写Word doc文件 Apache poi的hwpf模块是专门用来对word doc文件进行读写操作的。在hwpf里面我们使用HWPFDocument来表示一个word doc文档
poi实现对word读写操作源码,内包含jar包,还有相关的代码
poi实现对word读写操作实例源码Java源码
java读写Excel,POI.JAR,Word内容读取
poi-tl解析Word文档,包含表格类型的也可以
poi读写word模板/JAVA生成word包
使用poi将多个word合并,本方法经过测试,目前只支持多个doc格式合并成一个doc,如果是docx格式生成后打不开
资源描述了poi对于word的操作,poi合并word文档,poi设置word页码,内含maven依赖文档和完整代码,是一个完整工程,打开即可用
poi读写excel,poi总结; poi读写excel,poi总结 poi读写excel,poi总结
这是一个POI读取word 2003 和 word 2007的例子 是一个Eclipse工程 下载后直接导入工程 运行 src 目录下的 Test java 类即可 这个 rar 包中包含着 POI 读取word 2003 和 word 2007 所需要的 jar 包 也有需要读取的 ...
poi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成word
poi读取word代码,可以通过此代码实现对word中的文本、图片、表格的读取。
java 使用POI合并两个word文档.docx
Apache poi 根据word模板生成word报表 替换 循环列表 图片,代码调试过了,修改相应的图片位置,word模板路径即可运行
java使用poi操作.doc word模板替换,循环插入表格
一个使用Apache的poi使用word模板循环输出表格行并导出的例子
通过POI将图片插入word文档中,写文字就简单了自己摸索
android使用POI操作word docx文档,根据 【***】这种标识规则,替换掉docx文档中的内容
java使用poi,根据模板,生成word文档.
poi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excel