We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Export Document from database and writing into word document


I am trying to implement exporting document from database and creating new word document.
I need some idea, how it can be done in java.


Thanks for you request. I create sample code for you that demonstrate how to operate with documents stored in the DB. Please see the following code:

public class Main {
    public static void main(String[] args) throws Exception {
        // Get data from database.
        // In our case DB is a simple MS Access database.
        // but any tyle of Databases can be processed using the same techniques
        Connection connection = createConnection();
        ResultSet docRS = getDocumentsData(connection);
        // Fetch each row from the result set
        while (docRS.next()) {
            // Get the data from the row using the column name
            int id = docRS.getInt("ID");
            String fileName = docRS.getString("FileName");
            InputStream dbDocStream = docRS.getBinaryStream("FileContent");
            // This is needed because JdbcOdbcInputStream does not support available()
            // Write document to ByteArrayOutputStream
            byte buf[]=new byte[1024];
            int len;
            ByteArrayOutputStream tmpStream = new ByteArrayOutputStream();
            // Get document bytes
            byte[] docBytes = tmpStream.toByteArray();
            // Create input stream that supportes available()
            InputStream docStream = new ByteArrayInputStream(docBytes);
            // Now you can create Aspose.Words.Document from InputStream
            Document doc = new Document(docStream);
            // Here you can process your document using Aspose.Words API
            // You can perfor mail merge, change values of DocumentProperties etc
            // ........................................
            // just for example we add some text at end of the document
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.writeln("This is some text");
            // Now you can save your document to disk
            doc.save("C:\\Temp\\" + fileName);
            // Also you can update entry in your DB
            // first we should save the document into stream
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            doc.save(outStream, SaveFormat.DOC);
            // Conver outputStrem to inputStream
            InputStream inputDocStream = new ByteArrayInputStream(outStream.toByteArray());
            // Update DB enry
            updateDocumentData(connection, id, fileName, outStream.toByteArray());
     * Method returns all entries from Documents table
    public static ResultSet getDocumentsData(Connection connection)
            // Create database query
            String sqlString = "SELECT * FROM Documents";
            // Create statement
            // Connection connection = createConnection();
            Statement stmt = connection.createStatement();
            // Return ResultSet object
            return stmt.executeQuery(sqlString);
        catch(Exception e)
        return null;
     * Method updates entry in Documents table
    public static void updateDocumentData(Connection connection, int id, String fileName, byte[] docStream)
            // Create database query
            String sqlString = "UPDATE Documents SET FileName = ?, FileContent = ? WHERE ID = ?";
            // Create statement
            // Connection connection = createConnection();
            PreparedStatement stmt = connection.prepareStatement(sqlString);
            // Set values of parameters
            stmt.setString(1, fileName);
            stmt.setBytes(2, docStream);
            stmt.setInt(3, id);
            // Execute quesry
        catch(Exception e)
     * Utility function that creates a connection to the Database.
    private static Connection createConnection() throws Exception
        // Load a DB driver that is used by the demos
        // Compose connection string.
        String connectionString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" +
        // DSN-less DB connection.
        Connection connection = DriverManager.getConnection(connectionString);
        return connection;

Hope this helps.
Best regards.