word 转 html

String htmlFileNameString= this .upfileFileName+ "_html.html" ;         //设置html文件的文件名
         //设置hmtl页面的保存路径
         String htmlFileSavePath=getRequest().getRealPath( "" )+ this .uploadCasePath+htmlFileNameString;
//      File htmlFile=new File(htmlFileSavePath);
         ActiveXComponent appComponent= null ;
         try {
             appComponent= new ActiveXComponent( "Word.Application" );
             appComponent.setProperty( "Visible" , new Variant( false ));
             Object   docs   =   appComponent.getProperty( "Documents" ).toDispatch();
             Object   doc   =   Dispatch.invoke((Dispatch)docs, "Open" ,Dispatch.Method, new Object[]{docFile, new Variant( false ), new Variant( true )}, new int [ 1 ]).toDispatch();
             //打开word文件
             Dispatch.invoke((Dispatch)doc, "SaveAs" ,Dispatch.Method, new Object[]{htmlFileSavePath, new Variant( 8 )}, new int [ 1 ]);
             //作为html格式保存到临时文件
             Variant   f   =   new   Variant( false );
             Dispatch.call((Dispatch)doc, "Close" ,f);
//          Dispatch.call((Dispatch)doc,"Delete",f);
             //flag   =   true;
             } catch (Exception   e){
                  e.printStackTrace();
             } finally {
                 appComponent.invoke( "Quit" , new Variant[]{});
             }
             System.out.println( "转化完毕! " );
//          return


描述:主要使用了jacob来实现,先去官网上下载最新的文件(主要用到jacob.jar和jacob.dll)两个东东,拿到上面两个文件以后,按照网上的说法需要将jacob.jar添加到工程中,这是必须的,还有的说要把上面两个文件放到很多地方才能用,为了保险起见,我把所有的都目录都放了一遍,以下目录是我放过的目录(我的可以用): 1.jdk安装目录中的 jre\bin(把jacob.dll放在这里)和jre\lib\ext(jacob.jar放在这里); 2.手动把jacob.jar添加到工程中(就是Eclipse中的buildPath啦),然后再手动copy到工程的web-info/lib目录下(好像只有web项目才需要这样做,我的是web,所以做了); 3.然后把facob.dll放到system32这个文件夹下面; 按照上面的配置以后我的就可以用了,否则可能会在执行 appComponent=new ActiveXComponent("Word.Application"); 这句时,没有任何动静或者报异常; 还有一个问题就是编译出来的html是以gb2312作为编码方式的,打开可能会乱码,java只要在web.xml中的jsp-config节点下配置这个: <jsp-property-group> <description>HTML Encoding</description> <display-name>HTML Encoding Config</display-name> <url-pattern>*.html</url-pattern> <el-ignored>true</el-ignored> <page-encoding>gbk</page-encoding> <scripting-invalid>true</scripting-invalid> </jsp-property-group> 就没什么问题了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值