0

我在一个名为 Mirth 的 java 应用程序中工作,我需要读取一个保存的 word 文档,该文档以 Microsoft word 二进制数据格式保存在数据库表中。目前我可以从我的 java 应用程序中的列中检索数据,但我需要将其转换为可读文本或 XML 或 HTML 格式。

在线查看有一个名为 Aspose.words 的 java 库,但我找不到任何可以读取此二进制数据并将其转换为可读内容的方法。以前有没有人使用过 Aspose.words 来完成这样的任务,或者有没有人有替代解决方案

4

1 回答 1

0

从数据库加载文档

如果 Word 文档位于数据库表中,则可以使用 ByteArrayInputStream 加载该文档。请参阅http://www.aspose.com/docs/display/wordsjava/How+to++Load+and+Save+a+Document+to+Database了解如何将 Word 文档保存和读取到/的文章从数据库。我已经从那里复制了相关代码。

public static Document readFromDatabase(String fileName) throws Exception
{
    // Create the SQL command.
    String commandString = "SELECT * FROM Documents WHERE FileName='" + fileName + "'";

    // Retrieve the results from the database.
    ResultSet resultSet = executeQuery(commandString);

    // Check there was a matching record found from the database and throw an exception if no record was found.
    if(!resultSet.isBeforeFirst())
        throw new IllegalArgumentException(MessageFormat.format("Could not find any record matching the document \"{0}\" in the database.", fileName));

    // Move to the first record.
    resultSet.next();

    // The document is stored in byte form in the FileContent column.
    // Retrieve these bytes of the first matching record to a new buffer.
    byte[] buffer = resultSet.getBytes("FileContent");

    // Wrap the bytes from the buffer into a new ByteArrayInputStream object.
    ByteArrayInputStream newStream = new ByteArrayInputStream(buffer);

    // Read the document from the input stream.
    Document doc = new Document(newStream);

    // Return the retrieved document.
    return doc;

}

阅读文本

加载文件后,您可以使用 DOM 读取它的段落、表格、图像等,请参阅http://www.aspose.com/docs/display/wordsjava/Programming+with+Documents上的相关文档。

但是,如果您只想从文档中获取所有文本,您可以通过调用 toString() 方法轻松完成,如下所示

System.out.println(doc.toString(SaveFormat.TEXT));

我与 Aspose 一起担任开发人员宣传员。

于 2015-04-30T05:38:02.123 回答