Java는 poi를 사용하여 word를 html로 변환합니다.

5238 단어 poiwordhtml
poi를 사용하여 word를 html로 변환하고doc,docx를 지원하며 변환 후 그림, 스타일을 유지할 수 있습니다.
1. Maven 패키지 가져오기

<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> 
 <version>3.14</version> 
</dependency> 
<dependency> 
 <groupId>fr.opensagres.xdocreport</groupId> 
 <artifactId>xdocreport</artifactId> 
 <version>1.0.6</version> 
</dependency> 
<dependency> 
 <groupId>org.apache.poi</groupId> 
 <artifactId>poi-ooxml-schemas</artifactId> 
 <version>3.14</version> 
</dependency> 
<dependency> 
 <groupId>org.apache.poi</groupId> 
 <artifactId>ooxml-schemas</artifactId> 
 <version>1.3</version> 
</dependency> 
2. 코드 변환

import org.apache.poi.hwpf.HWPFDocument; 
import org.apache.poi.hwpf.converter.WordToHtmlConverter; 
import org.apache.poi.xwpf.converter.core.BasicURIResolver; 
import org.apache.poi.xwpf.converter.core.FileImageExtractor; 
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter; 
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions; 
import org.apache.poi.xwpf.usermodel.XWPFDocument; 
import org.w3c.dom.Document; 
 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.OutputKeys; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.OutputStreamWriter; 
 
public class Test { 
  // doc html 
  void docToHtml() throws Exception { 
    String sourceFileName = "C:\\doc\\test.doc"; 
    String targetFileName = "C:\\html\\test.html"; 
    String imagePathStr = "C:\\html\\image\\"; 
    HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(sourceFileName)); 
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 
    WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(document); 
    //  ,  
    wordToHtmlConverter.setPicturesManager((content, pictureType, name, width, height) -> { 
      try(FileOutputStream out = new FileOutputStream(imagePathStr + name)){ 
         out.write(content); 
      } catch (Exception e) { 
        e.printStackTrace(); 
      }  
      return "image/" + name; 
    }); 
    wordToHtmlConverter.processDocument(wordDocument); 
    Document htmlDocument = wordToHtmlConverter.getDocument(); 
    DOMSource domSource = new DOMSource(htmlDocument); 
    StreamResult streamResult = new StreamResult(new File(targetFileName)); 
 
    TransformerFactory tf = TransformerFactory.newInstance(); 
    Transformer serializer = tf.newTransformer(); 
    serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); 
    serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 
    serializer.setOutputProperty(OutputKeys.METHOD, "html"); 
    serializer.transform(domSource, streamResult); 
  } 
  // docx html 
  public void docxToHtml() throws Exception { 
    String sourceFileName = "D:\\ac\\00.docx"; 
    String targetFileName = "D:\\ac\\test.html"; 
    String imagePathStr = "D:\\ac\\image\\"; 
    OutputStreamWriter outputStreamWriter = null; 
    try { 
      XWPFDocument document = new XWPFDocument(new FileInputStream(sourceFileName)); 
      XHTMLOptions options = XHTMLOptions.create(); 
      //   
      options.setExtractor(new FileImageExtractor(new File(imagePathStr))); 
      // html  
      options.URIResolver(new BasicURIResolver("image")); 
      outputStreamWriter = new OutputStreamWriter(new FileOutputStream(targetFileName), "utf-8"); 
      XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getInstance(); 
      xhtmlConverter.convert(document, outputStreamWriter, options); 
    } finally { 
      if (outputStreamWriter != null) { 
        outputStreamWriter.close(); 
      } 
    } 
  } 
프레젠테이션 주소:https://www.xiaoyun.studio/app/preview.html
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기