Docx4j
一开始打算使用docx4j来进行解析Word并生成Html。
但是生成的图片包含了emf
格式的图片,这是html所不支持的。
而且生成完成之后的Html样式皆为行内样式。
没有去仔细研读他的API(时间不充足呀....)。
导致生成的效果不是很理想。
LibreOffice
想起了当初使用过这个东西,再看一下效果如何把?
并且他生成的图片是jpg/png
格式的,完全可以被html渲染。
最关键使用还贼简单:
public static void main(String[] args)throws Exception {
/**
* D:\soft\LibreOffice_6.0.6\program\soffice:表示libreoffice安装路径
* D:\Desktop\DocCloud\testDir\hadoopInstall.doc:表示要转化的word文件
*/
String fileName= "C:\\Users\\Administrator\\Desktop\\test.docx";
//这里需要注意你的LibreOffice的安装路径哦
//采用了命令行调用的方式
String command = "C:\\Program Files\\LibreOffice\\program\\soffice --headless --invisible --convert-to html "+fileName;
/**
*workDir:表示转化之后的HTML文件保存的路径地址
*/
String workDir = "C:\\Users\\Administrator\\Desktop\\"+ UUID.randomUUID().toString()+"\\";
File file = new File(workDir);
//创建目录--因为是UUID所以不用判断目录一定不存在
file.mkdirs();
/**
* command:命令
* null:操作系统运行程序时通过envp 参数将系统环境变量传递给程序
* file:命令在那个路径下执行
*/
//返回过程对象--Process
Process exec = Runtime.getRuntime().exec(command,null,file);
//错误信息
InputStream errorStream = exec.getErrorStream();
//结果信息
InputStream inputStream = exec.getInputStream();
//IOUtils-直接将流转化成字符串
String error = IOUtils.toString(errorStream);
String result = IOUtils.toString(inputStream);
//打印信息
System.out.println(error);
System.out.println(result);
}
// ---------------------
// 作者:CSDN_小路
// 来源:CSDN
// 原文:https://blog.csdn.net/qq_38617531/article/details/83548785
// 版权声明:本文为博主原创文章,转载请附上博文链接!
对比图:
Docx4j相关代码可以在博客中找找哦
Q.E.D.