[英]Convert DOC file to DOCX with Java
我需要在我目前正在開發的 Java 軟件中使用 DOCX 文件(實際上是其中包含的 XML),但我公司中的一些人仍然使用 DOC 格式。
你知道是否有一種方法可以使用 Java 將 DOC 文件轉換為 DOCX 格式? 我知道可以使用 C#,但這不是一個選項
我用谷歌搜索了它,但沒有任何結果......
謝謝
你可以試試Aspose.Words 的 Java 。 它允許您加載 DOC 文件並將其保存為 DOCX 格式。 代碼非常簡單,如下所示:
// Open a document.
Document doc = new Document("input.doc");
// Save document.
doc.save("output.docx");
請查看這是否對您的方案有幫助。
披露:我在 Aspose 擔任開發人員布道師。
查看JODConverter以查看它是否符合要求。 我個人沒有用過。
JODConvertor 通過網絡協議調用 OpenOffice/LibreOffice。 因此,它可以“做任何您在 OpenOffice 中可以做的事情”。 這包括轉換格式。 但它只與您運行的任何版本的 OpenOffice 一樣好。 我的一個文檔中有一些藝術作品,但它並沒有像我希望的那樣轉換它們。
JODConvertor 不再受支持,根據谷歌代碼 web site for v3.
要讓 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 文件添加到您的項目庫中
//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);
使用較新版本的 jars 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);
}
要將 DOC 文件轉換為 HTML,請查看此( 在 Java 中以編程方式將 Word doc 轉換為 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);
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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.