京东6.18大促主会场领京享红包更优惠

 找回密码
 立即注册

QQ登录

只需一步,快速开始

将word转化为xml

2019-11-6 15:23| 发布者: zhaojun917| 查看: 619| 评论: 0

摘要: 刚到公司头儿就给我布置了任务,可以将word里的内容读取出来,持久化到数据库中。一开始觉得,用流读一下不就行了吗?后来发现没有这么简单。Word的.doc格式是不公开的,所以Java很难直接支持读.doc文件,网上的提供的信 ...
   刚到公司头儿就给我布置了任务,可以将word里的内容读取出来,持久化到数据库中。一开始觉得,用流读一下不就行了吗?后来发现没有这么简单。Word的.doc格式是不公开的,所以Java很难直接支持读.doc文件,网上的提供的信息也不多。有一个通过poi的组件来读取的,但是通过poi来读取excel的内容很方便,读取word实在是不给力了,具体原因apache官网也说了在poi中处理word模块的作者离开apache组织了(http://poi.apache.org/hwpf/index.html)。现在主要是面临三个问题:第一,支持中文;第二,保证word03和word07兼容;第三,对图片表格等格式的内容都能取读出来。请教高人后得知,打开word后可以直接另存为xml的格式,只要能得到xml的格式,就可以通过Dom4j解析得到内容。但是如何通过程序将word转化成xml呢? 
   网上查了一下,免费的主要有两种方法: 
   1,通过jacob来实现(http://danadler.com/jacob/).jacob是在java与微软的com组件之间的桥梁,通过使用jacob自带的dll动态链接库通过jni的方式实现了在sun java平台上的程序对com调用.但是这种方式未能解决word07转化为Xml的问题,所以只能弃之。 
   2,通过jodconveter来实现转化(http://www.artofsolving.com/opensource/jodconverter)。这种方式实现起来比较麻烦,操作有点繁琐,但是能解决全部上述问题。jobconveter团队的才智表示敬佩:通过启动OpenOffice.org的服务端口,实现程序操作opeanoffice实现文件的转换。 
   具体的实现方式在http://nopainnogain.iteye.com/blog/819432 此文中已有详细描述. 

   另外贴上我的代码: 
Java代码  收藏代码
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6.   
  7. import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;  
  8. import com.artofsolving.jodconverter.DocumentConverter;  
  9. import com.artofsolving.jodconverter.DocumentFormatRegistry;  
  10. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;  
  11. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;  
  12. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  
  13.   
  14.   
  15. public class test {  
  16.   
  17.     /** 
  18.      * @param args 
  19.      */  
  20.     public static void main(String[] args) throws Exception {  
  21.         // TODO Auto-generated method stub  
  22.         test t = new test();  
  23.         //File in = new File("d:\\mytest\\test1.pdf");  
  24.         //File out = new File("d:\\mytest\\test11.html");  
  25.         FileInputStream input = new FileInputStream("d:\\mytest\\test11.pdf");  
  26.         FileOutputStream output = new FileOutputStream("d:\\mytest\\test11.doc");  
  27.   
  28.         t.convert(input, output);  
  29.     }  
  30.       
  31.     public void convert(File input, File output) throws Exception  
  32.   
  33.     {  
  34.   
  35.         OpenOfficeConnection conn = new SocketOpenOfficeConnection("localhost"8100);  
  36.   
  37.         conn.connect();  
  38.   
  39.         DocumentConverter converter = new OpenOfficeDocumentConverter(conn);  
  40.   
  41.         converter.convert(input, output);  
  42.   
  43.         conn.disconnect();  
  44.   
  45.     }  
  46.       
  47.     public void convert(InputStream input, OutputStream output) throws Exception  
  48.   
  49.     {  
  50.   
  51.         OpenOfficeConnection conn = new SocketOpenOfficeConnection("localhost"8100);  
  52.   
  53.         conn.connect();  
  54.   
  55.         DocumentConverter converter = new OpenOfficeDocumentConverter(conn);  
  56.   
  57.         DocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();  
  58.   
  59.         converter.convert(input, registry.getFormatByFileExtension("pdf"), output, registry.getFormatByFileExtension("doc"));  
  60.   
  61.         conn.disconnect();  
  62.   
  63.     }  
  64. }  

附件里有jodconverter的zip包,可以在这里直接下了,openoffice的安装文件就不贴了,到官网下就行(http://www.openoffice.org/). 
关闭

站长推荐上一条 /6 下一条

QQ|手机版|小黑屋|梦想之都-俊月星空 ( 粤ICP备18056059号 )|网站地图

GMT+8, 2025-7-1 18:09 , Processed in 0.036387 second(s), 18 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2025 Discuz! Team.

返回顶部