我尝试使用WordToHtmlConverter
将.doc转换为HTML,但效果非常好。
但是当我试图将.docx转换为HTML时,我就陷入了困境。
我尝试了什么:
我使用以下代码将.docx转换为HTML:
我尝试过的代码:How to use Tika's XWPFWordExtractorDecorator class?
InputStream input = TikaInputStream.get(new File("C:\\Users\\Downloads\\filename.docx"));
Parser parser = new AutoDetectParser();
StringWriter sw = new StringWriter();
SAXTransformerFactory factory = (SAXTransformerFactory)
SAXTransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.setResult(new StreamResult(sw));
try {
Metadata metadata = new Metadata();
parser.parse(input, handler, metadata, new ParseContext());
String xml = sw.toString();
System.out.print("tika : "+xml);
} finally {
input.close();
}
我得到的输出是,
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title/>
</head>
<body/>
</html>
感谢您的帮助,谢谢
答案 0 :(得分:8)
此代码对我来说可以将.docx转换为html:
您还可以查看链接:Link to code
//convert .docx to HTML string
InputStream in= new FileInputStream(new File(path));
XWPFDocument document = new XWPFDocument(in);
XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(new File("word/media")));
OutputStream out = new ByteArrayOutputStream();
XHTMLConverter.getInstance().convert(document, out, options);
String html=out.toString();
System.out.println(html);
答案 1 :(得分:0)
您可能想使用Mammoth docx到HTML库。它是一个库,用于通过在浏览器端将doc,docx文档转换为html来显示doc,docx文档,并且可以在后端进行处理。