使用Java将DOC文件转换为DOCX

我需要在我正在开发的Java软件中使用DOCX文件(实际上是其中包含的XML),但我公司的一些人仍然使用DOC格式。

您知道是否有办法使用Java将DOC文件转换为DOCX格式? 我知道使用C#是可能的,但这不是一个选择

我用Google搜索,但没有出现……

谢谢

您可以尝试使用Aspose.Words for Java 。 它允许您加载DOC文件并将其另存为DOCX格式 。 代码非常简单,如下所示:

// Open a document. Document doc = new Document("input.doc"); // Save document. doc.save("output.docx"); 

请查看这在您的方案中是否有帮助。

披露:我在Aspose担任开发人员传播者。

检查JODConverter以查看它是否符合要求。 我没有亲自使用它。

要将DOC文件转换为HTML,请查看此内容( 在Java中以编程方式将Word文档转换为HTML )

使用此: http : //poi.apache.org/

或者用这个:

 XWPFDocument docx = new XWPFDocument(OPCPackage.openOrCreate(new File("hello.docx"))); XWPFWordExtractor wx = new XWPFWordExtractor(docx); String text = wx.getText(); System.out.println("text = "+text); 

JODConvertor通过网络协议调用OpenOffice / LibreOffice。 因此,它可以“在OpenOffice中执行任何操作”。 这包括转换格式。 但它只能与您运行的任何OpenOffice版本一样好。 我的一个文档中有一些艺术,并没有像我希望的那样转换它们。

根据针对v3的谷歌代码网站,不再支持JODConvertor。

要让JOD完成这项工作,你需要做一些事情

 private static void transformBinaryWordDocToDocX(File in, File out) { OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); DocumentFormat docx = converter.getFormatRegistry().getFormatByExtension("docx"); docx.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "MS Word 2007 XML")); converter.convert(in, out, docx); } private static void transformBinaryWordDocToW2003Xml(File in, File out) { OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);; DocumentFormat w2003xml = new DocumentFormat("Microsoft Word 2003 XML", "xml", "text/xml"); w2003xml.setInputFamily(DocumentFamily.TEXT); w2003xml.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "MS Word 2003 XML")); converter.convert(in, out, w2003xml); } private static OfficeManager officeManager; @BeforeClass public static void setupStatic() throws IOException { /*officeManager = new DefaultOfficeManagerConfiguration() .setOfficeHome("C:/Program Files/LibreOffice 3.6") .buildOfficeManager(); */ officeManager = new ExternalOfficeManagerConfiguration().setConnectOnStart(true).setPortNumber(8100).buildOfficeManager(); officeManager.start(); } @AfterClass public static void shutdownStatic() throws IOException { officeManager.stop(); } 

为了实现这一点,您需要将LibreOffice作为联网服务器运行(我无法让JODConvertor的’按需运行’部分在具有LO 3.6的Windows下工作)

我需要进行相同的转换,经过研究后发现Jodconvertor很有用,你可以从https://code.google.com/p/jodconverter/downloads/list下载jar

将jodconverter-core-3.0-beta-4-sources.jar文件添加到项目lib中

  //1) Create OfficeManger Object OfficeManager officeManager = new DefaultOfficeManagerConfiguration() .setOfficeHome(new File("/opt/libreoffice4.4")) .buildOfficeManager(); officeManager.start(); // 2) Create JODConverter converter OfficeDocumentConverter converter = new OfficeDocumentConverter( officeManager); // 3)Create DocumentFormat for docx DocumentFormat docx = converter.getFormatRegistry().getFormatByExtension("docx"); docx.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "MS Word 2007 XML")); //4)Call convert funtion in converter object converter.convert(new File("doc/AdvancedTable.doc"), new File( "docx/AdvancedTable.docx"), docx);