输入:docx文件
输出:html格式的字符串,类似于"
xxxxxxx"图片、文字、表格均可转换
public static String docx2Html(File docxFile){
String html = null;
String imagePath=docxFile.getAbsolutePath().replaceAll(docxFile.getName(),"")+"docxImage/";
File imageFile=new File(imagePath);
if(!docxFile.exists()){
System.out.println("文件不存在!");
}
try {
InputStream in=new FileInputStream(docxFile);
XWPFDocument document=new XWPFDocument(in);
//存储图片
XHTMLOptions options=XHTMLOptions.create();
//存放图片的文件夹
options.setIgnoreStylesIfUnused(false);
options.setFragment(true);
options.setImageManager(new Base64EmbedImgManager());
// 3) 将 XWPFDocument转换成XHTML
// OutputStream out = new FileOutputStream(new File(tempPath+"\a.html"));
// XHTMLConverter.getInstance().convert(document, out, options);
//也可以使用字符数组流获取解析的内容
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XHTMLConverter.getInstance().convert(document, baos, options);
html = new String(baos.toByteArray());
in.close();
baos.close();
} catch (IOException e) {
e.printStackTrace();
} catch (XWPFConverterException e) {
e.printStackTrace();
}
return html;
}



