Hi,
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,
Nandu
Hi,
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,
Nandu
Hi
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();
while((len=dbDocStream.read(buf))>0)
tmpStream.write(buf,0,len);
dbDocStream.close();
// Get document bytes
byte[] docBytes = tmpStream.toByteArray();
tmpStream.close();
// 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.moveToDocumentEnd();
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());
}
connection.close();
}
/**
* Method returns all entries from Documents table
*/
public static ResultSet getDocumentsData(Connection connection)
{
try
{
// 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)
{
System.out.println(e);
}
return null;
}
/**
* Method updates entry in Documents table
*/
public static void updateDocumentData(Connection connection, int id, String fileName, byte[] docStream)
{
try
{
// 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
stmt.executeUpdate();
connection.commit();
}
catch(Exception e)
{
System.out.println(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
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Compose connection string.
String connectionString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" +
"DBQ=DocDB.mdb;UID=Admin";
// DSN-less DB connection.
Connection connection = DriverManager.getConnection(connectionString);
return connection;
}
}
Hope this helps.
Best regards.