Javaはwordをhtmlに変換する方法の例【docとdocxフォーマット】を実現します。


本論文の実例は、Javaがwordをhtmlに変換する方法を実現することを述べている。皆さんに参考にしてあげます。具体的には以下の通りです。

 public static void main(String[] args) throws Exception {
 String filePath = "C:/Users/Administrator/Desktop/92          /";
 File file = new File(filePath);
 File[] files = file.listFiles();
 String name = null;
 for (File file2 : files) {
  Thread.sleep(500);
  name = file2.getName().substring(0, file2.getName().lastIndexOf("."));
  System.out.println(file2.getName());
  if (file2.getName().endsWith(".docx") || file2.getName().endsWith(".DOCX")) {
  CaseHtm.docx(filePath ,file2.getName(),name +".htm");
  }else{
  CaseHtm.dox(filePath ,file2.getName(),name +".htm");
  }
  
    }
 }
 /**
 *   docx
 * @param filePath
 * @param fileName
 * @param htmlName
 * @throws Exception
 */
 public static void docx(String filePath ,String fileName,String htmlName) throws Exception{
 final String file = filePath + fileName;
 File f = new File(file); 
 // )   word     XWPFDocument  
 InputStream in = new FileInputStream(f);
 XWPFDocument document = new XWPFDocument(in);
 // )    XHTML   (    IURIResolver          )
 File imageFolderFile = new File(filePath);
 XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));
 options.setExtractor(new FileImageExtractor(imageFolderFile));
 options.setIgnoreStylesIfUnused(false);
 options.setFragment(true);
 // )   XWPFDocument   XHTML
 OutputStream out = new FileOutputStream(new File(filePath + htmlName));
 XHTMLConverter.getInstance().convert(document, out, options);
 }
 /**
 *   doc
 * @param filePath
 * @param fileName
 * @param htmlName
 * @throws Exception
 */
 public static void dox(String filePath ,String fileName,String htmlName) throws Exception{
    final String file = filePath + fileName;
    InputStream input = new FileInputStream(new File(file));
    HWPFDocument wordDocument = new HWPFDocument(input);
    WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
    //  word  
    wordToHtmlConverter.processDocument(wordDocument);
    Document htmlDocument = wordToHtmlConverter.getDocument();
    File htmlFile = new File(filePath + htmlName);
    OutputStream outStream = new FileOutputStream(htmlFile);
    DOMSource domSource = new DOMSource(htmlDocument);
    StreamResult streamResult = new StreamResult(outStream);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer serializer = factory.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.METHOD, "html");
    serializer.transform(domSource, streamResult);
    outStream.close();
  }


<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>fr.opensagres.xdocreport.document</artifactId>
  <version>1.0.5</version>
</dependency>
<dependency> 
  <groupId>fr.opensagres.xdocreport</groupId> 
  <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId> 
  <version>1.0.5</version> 
</dependency>
  <dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.12</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-scratchpad</artifactId>
  <version>3.12</version>
</dependency>

javaアルゴリズムに関する詳細について興味がある読者は、当駅のテーマを見ることができます。「Javaファイルとディレクトリの操作テクニックのまとめ」、「Javaデータ構造とアルゴリズム教程」、「Java操作DOMノード技術のまとめ」、「Javaキャッシュ操作テクニックのまとめ
本論文で述べたように、皆さんのjavaプログラムの設計に役に立ちます。