使用poi將word轉換為html,支持doc,docx,轉換後可以保持圖片、樣式。
1.導入
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.14</version> </dependency> <dependency> <groupId> org.apache.poi </groupId> <ArtifActId> poi-ooxml </artifactid> <版本> <版本> 3.14 </distrifency> </deverency> <dependency> <groupId> <groupId> fr.opensagres.xdocReport.xdocReport </groupId> </groupId> <groupId> org.apache.poi </groupId> <ARTIFACTID> poi-ooxml-schemas </artifactid> </artifactid> <版本> 3.14 </version> </dependency> <dependency> <groudency> <groupId> 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.domsource;導入javax.xml.transform.stream.streamresult;導入java.io.file;導入java.io.io.fileinputstream;導入java.io.io.fileoutputstream;導入java.io.OutputStreamWriter;公共類測試{// doc轉換為html void doctohtml()引發異常{字符串sourcefilename =“ c://doc//test.doc”;字符串targetFileName =“ c://html//test.html”;字符串ImagePath =“ C:// html // image //”; hwpfdocument worddocument = new hwpfdocument(new fileInputStream(sourceFileName));文檔= documentBuilderFactory.NewInstance()。 newDocumentBuilder()。 newDocument(); WordTohtmlConverter WordTohtmlConverter = new WordTohtmlConverter(document); //保存圖片,並返回圖片的相對路徑wordtohtmlConverter.setPicturesManager(((content,picturetype,name,width,height)) - > {trif(fileOutputstream out = new fileOutputStream(imagepathstr + name)){out.write(out.write(prons.write(content)) WordToHtmlConverter.ProcessDocument(WordDocument);文檔htmldocument = wordtohtmlConverter.getDocument(); domsource domsource = new domsource(htmldocument); StreamResult StreamResult = new StreamResult(new File(targetFileName)); TransformerFactory TF = TransformerFactory.NewInstance();變壓器序列化器= tf.newtransformer(); serializer.SetOutputProperty(outputKeys.Coding,“ UTF-8”); serializer.setOutputProperty(outputkeys.indent,“ yes”); serializer.setOutputProperty(outputKeys.Method,“ html”); serializer.transform(domsource,stream result); } // docx轉換為html public void docxtohtml()拋出異常{字符串sourcefileName =“ d://ac//00.docx”;字符串targetFileName =“ d://ac//test.html”;字符串ImagePath =“ d:// ac // image //”; outputStreamWriter outputStreamWriter = null;嘗試{xwpfdocument document = new xwpfdocument(new fileInputStream(sourceFileName)); xhtmloptions選項= xhtmloptions.create(); //存放圖片的文件夾options.setExtractor(new FileImageExtractor(new File(imagePathStr))); //html中圖片的路徑選項。 outputStreamWriter = new UppotStreamWriter(new FileOutputStream(targetFileName),“ UTF-8”); xhtmlConverter xhtmlConverter =(xhtmlConverter)xhtmlConverter.getInstance(); xhtmlConverter.convert(文檔,outputStreamWriter,options); }最後{if(outputStreamWriter!= null){utputStreamWriter.close(); }}}}演示地址:https://www.xiaoyun.studio/app/preview.html
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。 ,也希望大家多多支持武林網。