convert doc to docx

由于项目需要,收集了各种word2003转换word2007方法,在此提供给大家。

1)You may try Aspose.Words for Java. It allows you to load a DOC file and save it as DOCX format. The code is very simple as shown below:

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

Please see if this helps in your scenario.

Disclosure: I work as developer evangelist at Aspose.

2)To convert DOC file to HTML look at this (Convert Word doc to HTML programmatically in Java)

Use this: http://poi.apache.org/

Or use this :

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

 

3)JODConvertor calls OpenOffice/LibreOffice via a network protocol. It can therefore 'do anything you can do in OpenOffice'. This includes converting formats. But it only does as good a job as whatever version of OpenOffice you are running. I have some art in one of my docs, and it doesn't convert them as I hoped.

JODConvertor is no longer supported, according to the google code web site for v3.

To get JOD to do the job you need to do something like

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();
}

For this to work you need to be running LibreOffice as a networked server ( I could not get the 'run on demand' part of JODConvertor to work under windows with LO 3.6 very well )

4)
 

I needed the same conversion ,after researching a lot found Jodconvertor can be useful in it , you can download the jar from https://code.google.com/p/jodconverter/downloads/list

Add jodconverter-core-3.0-beta-4-sources.jar file to your project 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);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
To convert a DOC file to DOCX format using Apache POI library, you can follow these steps: 1. Add the Apache POI dependency to your project. You can do this by adding the necessary JAR files to your project's build path or by using a dependency management tool like Maven or Gradle. 2. Use the following code snippet to perform the conversion: ```java import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.WordToConverter; import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class DocToDocxConverter { public static void main(String[] args) { try { // Load the DOC file InputStream inputStream = new FileInputStream("input.doc"); HWPFDocument document = new HWPFDocument(inputStream); // Create an empty output DOCX file OutputStream outputStream = new FileOutputStream("output.docx"); XWPFDocument convertedDocument = new XWPFDocument(); // Convert the DOC file to DOCX format WordToConverter converter = new WordToConverter(convertedDocument); converter.processDocument(document); // Save the converted document to the output file convertedDocument.write(outputStream); // Close the streams outputStream.close(); inputStream.close(); System.out.println("Conversion completed successfully."); } catch (Exception e) { e.printStackTrace(); } } } ``` Make sure to replace "input.doc" with the path to your input DOC file and "output.docx" with the desired path for the output DOCX file. 3. Run the code, and it will convert the DOC file to DOCX format and save it as "output.docx" in the specified location. Please note that this code is based on Apache POI version 5.x, which supports the conversion of DOC to DOCX. If you are using an older version of Apache POI, you might need to use different classes or methods for the conversion.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郡麟天下

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值