Link to home
Start Free TrialLog in
Avatar of Lalibela
Lalibela

asked on

Word deocument MS SQL SERVER 2000 and JSP

Hello,

I am have seen some application that does the following and I wonder if this is possible. I have a series of word documents/templates that will be used for coresspondance purpose. The thing is some of the fields in the template will be filled from the database(MS SQL SERVER 2000). Just like mail merge. What my question is will jsp populate the word document by fetching the required data from the DB ?

Thank you in advance for your reponses,
Lalibela
:14:15
Avatar of petmagdy
petmagdy
Flag of Canada image

Servlets can do this, the summary steps are:

1- read the word doument from database as byte[]
2- set content type as following      
response.setContentType("application/msword:);

3- stream the content out:

byte[] wordContent = getTheWordcontentFromDataBase();

      ServletOutputStream outStream;
      outStream = response.getOutputStream();
      outStream.write(wordContent);

Avatar of TimYates
You are probably best reading the document in in blocks and writing these blocks out to the browser, rather than reading the whole thing into e byte buffer (as it may be larger than the size of an int)

BufferedInputStream bis = null ;
BufferedOutputStream bos = null ;
try
{
    bis = new BufferedInputStream( new FileInputStream( filename ) ) ;
    byte[] bfr = new byte[ 4000 ] ;
    response.setContentType( "application/msword" );
    response.setHeader( "Content-Disposition", "attachment; filename=" + filename ) ;
    bos = new BufferedOutputStream( response.getOutputStream() ) ;
    int cnt = 0 ;
    while( ( cnt = bis.read( bfr, 0, bfr.length ) ) > 0 )
    {
        bos.write( bfr, 0, cnt ) ;
    }
}
catch( IOException ex )
{
    ex.printStackTrace() ;
}
finally
{
    try { if( bis != null ) bis.close() ; } catch( IOException ex ) {}
}
Also, this may help:

http://www.lowagie.com/iText/

it is a library allowing you to create PDFs on the fly...

To create DOC files this may help:

http://radio.javaranch.com/channel/val/2004/10/16/1097909528000.html
Avatar of Lalibela
Lalibela

ASKER

Hello,

Thank you for your responses. But my problem is not reading a word document stored in DB. I have the word document in web root application. What I want is to fill the template fields wtih data feteched from the DB. Just like mail merge in word.

Thanks,
:14:15
Hello,

Here is a solution I found... Jakarta velocity. If there is anybody in need of the same problem of mine you can use jakarta velocity and populate the database fields to hash map. If you need a unicode just set the content typt to utf-8.


Lalibela
:14:15
ASKER CERTIFIED SOLUTION
Avatar of PAQ_Man
PAQ_Man
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial