在比对项目中,有个预览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 相应文件就欧了