office在线预览功能

在比对项目中,有个预览office的功能,试了很多种方法,

如,用流的方法读取word,excel里的数据显示在页面上,对于简单的文本是没有问题的,但是对于有表格,字体大小 ,图片,格式的读取会有很大的失真的,

所以就放弃了,

用pageOffice插件,因为是收费的,所以不考虑了

最后用的是jodconverter  + openoffice   ,这个把office的文件转为html文件的,用了感觉还可以

步骤:

1,系统要先装openoffice

2,启动openoffice的转换功能

/opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &  


3.在java代码里写相应的转换方法

  public  void updateConvertFile(String docFile, String filepath) { 
    //把后缀换成html
    int index  =  docFile.lastIndexOf(".");
    String docFile2  =  docFile.substring(0, index);
    // 创建保存html的文件  放在原目录下
    File htmlFile = new File(SystemManageKey.FILE_PATH_KNOWLEDGE+filepath + docFile2 + ".html");  
    docFile = SystemManageKey.FILE_PATH_COMPAUDIT+filepath + docFile;
    // 创建Openoffice连接  
    OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);  
    try {  
        // 连接  
        con.connect();  
    } catch (ConnectException e) {  
    logger.info("转换开始:获取OpenOffice连接失败...");  
        e.printStackTrace();  
    }  
    // 创建转换器  
    DocumentConverter converter = new OpenOfficeDocumentConverter(con);
    try{
    logger.info("转换开始:文件路径html:"+htmlFile+"    文件名:"+docFile);
    // 转换文档问html  
        converter.convert(new File(docFile), htmlFile);  
    }catch(Exception e){
    logger.info("office 转为html 转换失败---");
    e.printStackTrace();
    }
    // 关闭openoffice连接  
    con.disconnect();  
    }  

4.把生成的html文件放在服务本地,在tomcat里配置个虚拟目录,点击时引用到html 相应文件就欧了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值