使用 poi 将 word 转换为 html, 支持 doc, docx, 转换后可以保持图片、样式。
1. 导入 maven 包
<การพึ่งพา> <roupId> org.apache.poi </groupid> <ratifactid> poi </artifactid> <version> 3.14 </version> </การพึ่งพา> <การพึ่งพา> <roupid> org.apache.poi </groupid> <ratifactid> <roupId> org.apache.poi </groupid> <ratifactid> poi-ooxml </artifactid> <version> 3.14 </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา> <roupid> fr.opensagres.xdocreport </groupid> <RoupID> org.apache.poi </groupId> <ratifactid> poi-ooxml-schemas </artifactid> <sersion> 3.14 </version> </การพึ่งพา> <การพึ่งพา> <roupid> org.apache.poi </groupid>
2. 转换代码
นำเข้า org.apache.poi.hwpf.hwpfdocument; นำเข้า org.apache.poi.hwpf.converter.wordtohtmlconverter; นำเข้า org.apache.poi.xwpf.converter.core.basicuriresolver; นำเข้า org.apache.poi.xwpf.converter.core.fileimageExtractor; นำเข้า org.apache.poi.xwpf.converter.xhtml.xhtmlconverter; นำเข้า org.apache.poi.xwpf.converter.xhtml.xhtmloptions; นำเข้า org.apache.poi.xwpf.usermodel.xwpfdocument; นำเข้า org.w3c.dom.document; นำเข้า Javax.xml.parsers.documentBuilderFactory; นำเข้า javax.xml.transform.outputkeys; นำเข้า javax.xml.transform.transformer; นำเข้า javax.xml.transform.transformerfactory; นำเข้า javax.xml.transform.dom.domsource; นำเข้า javax.xml.transform.stream.streamresult; นำเข้า Java.io.File; นำเข้า Java.io.FileInputStream; นำเข้า Java.io.FileOutputStream; นำเข้า Java.io.OutputStreamWriter; การทดสอบคลาสสาธารณะ {// doc 转换为 html โมฆะ doctohtml () พ่นข้อยกเว้น {สตริง sourcefilename = "c: //doc//test.doc"; String targetFilename = "C: //html//test.html"; string imagepathstr = "c: // html // image //"; hwpfdocument wordDocument = ใหม่ hwpfDocument (ใหม่ FileInputStream (SourceFilename)); เอกสารเอกสาร = DocumentBuilderFactory.NewInstance (). NewDocumentBuilder (). NewDocument (); wordtohtmlconverter wordtohtmlconverter = new wordtohtmlconverter (เอกสาร); // 保存图片, 并返回图片的相对路径 wordtohtmlconverter.setPicturesManager ((เนื้อหา, pictureType, ชื่อ, ความกว้าง, ความสูง) -> {ลอง (fileOutputStream out = new FileOutputStream (imagePathstr + name)) {out.write (เนื้อหา); WordToHtmlConverter.processDocument (WordDocument); เอกสาร htmldocument = wordtohtmlconverter.getDocument (); domsource domsource = new domsource (htmldocument); streamResult streamResult = streamResult ใหม่ (ไฟล์ใหม่ (TargetFilename)); TransformerFactory TF = TransformerFactory.NewInstance (); Transformer serializer = tf.newTransformer (); serializer.setOutputProperty (outputKeys.encoding, "UTF-8"); serializer.setOutputProperty (outputKeys.indent, "ใช่"); serializer.setOutputProperty (outputKeys.method, "html"); serializer.transform (Domsource, Streamresult); } // docx 转换为 html โมฆะสาธารณะ docxtohtml () พ่นข้อยกเว้น {สตริง sourceFilename = "d: //ac//00.docx"; สตริง targetFileName = "d: //ac//test.html"; string imagePathStr = "d: // ac // image //"; OutputStreamWriter OutputStreamWriter = null; ลอง {xwpfDocument document = ใหม่ XWPFDocument (ใหม่ FileInputStream (SourceFilename)); ตัวเลือก xhtmloptions = xhtmloptions.create (); // 存放图片的文件夹ตัวเลือก Setextractor (FileimageExtractor ใหม่ (ไฟล์ใหม่ (imagepathstr))); // html 中图片的路径ตัวเลือก uriresolver (ใหม่ Basicuriresolver ("image")); OutputStreamWriter = new OutputStreamWriter (ใหม่ fileOutputStream (targetFileName), "UTF-8"); XHTMLCONVERTER XHTMLCONVERTER = (XHTMLCONVERTER) XHTMLCONVERTER.GETINSTANCE (); XHTMLConverter.Convert (เอกสาร, OutputStreamWriter, ตัวเลือก); } ในที่สุด {ถ้า (outputStreamWriter! = null) {outputStreamWriter.close (); -演示地址: https://www.xiaoyun.studio/app/preview.html
以上就是本文的全部内容, 希望对大家的学习有所帮助, 也希望大家多多支持武林网。