docx命令运行Java_使用Java将DOC文件转换为DOCX

7 个答案:

答案 0 :(得分:3)

// Open a document.

Document doc = new Document("input.doc");

// Save document.

doc.save("output.docx");

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

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

答案 1 :(得分:2)

或者使用它:

XWPFDocument docx = new XWPFDocument(OPCPackage.openOrCreate(new File("hello.docx")));

XWPFWordExtractor wx = new XWPFWordExtractor(docx);

String text = wx.getText();

System.out.println("text = "+text);

答案 2 :(得分:2)

查看JODConverter以查看是否符合条款。我没有亲自使用它。

答案 3 :(得分:0)

将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);

答案 4 :(得分:0)

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.OutputStream;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfWriter;

import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hwpf.extractor.WordExtractor;

import org.apache.poi.hwpf.usermodel.Range;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class TestCon {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

POIFSFileSystem fs = null;

Document document = new Document();

try {

System.out.println("Starting the test");

fs = new POIFSFileSystem(new FileInputStream("C:/Users/312845/Desktop/a.doc"));

HWPFDocument doc = new HWPFDocument(fs);

WordExtractor we = new WordExtractor(doc);

OutputStream file = new FileOutputStream(new File("C:/Users/312845/Desktop/test.docx"));

System.out.println("Document testing completed");

} catch (Exception e) {

System.out.println("Exception during test");

e.printStackTrace();

} finally {

// close the document

document.close();

}

}

}

答案 5 :(得分:0)

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下工作)

答案 6 :(得分:0)

使用罐子jodconverter-core-4.2.2.jar和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);

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值