Convert DOC file to DOCX with Java

前端 未结 7 2003
天涯浪人
天涯浪人 2021-02-20 12:43

I need to use DOCX files (actually the XML contained in them) in a Java software I\'m currently developing, but some people in my company still use the DOC format.

Do yo

7条回答
  •  旧巷少年郎
    2021-02-20 13:38

    Use newer versions of jars jodconverter-core-4.2.2.jar and jodconverter-local-4.2.2.jar

    String inputFile = "*.doc";
    String outputFile = "*.docx";
    
    LocalOfficeManager localOfficeManager = LocalOfficeManager.builder()
                .install()
                .officeHome(getDefaultOfficeHome()) //your path to openoffice
                .build();
    
      try {
          localOfficeManager.start();
          final DocumentFormat format
                  = DocumentFormat.builder()
                          .from(DefaultDocumentFormatRegistry.DOCX)
                          .build();
    
          LocalConverter
                  .make()
                  .convert(new FileInputStream(new File(inputFile)))
                  .as(DefaultDocumentFormatRegistry.getFormatByMediaType("application/msword"))
                  .to(new File(outputFile))
                  .as(format)
                  .execute();
    
      } catch (OfficeException ex) {
          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
      } catch (FileNotFoundException ex) {
          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
      } finally {
          OfficeUtils.stopQuietly(localOfficeManager);
      }
    

提交回复
热议问题