• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

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
0
Lalibela
Asked:
Lalibela
1 Solution
 
petmagdyCommented:
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);

0
 
TimYatesCommented:
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 ) {}
}
0
 
TimYatesCommented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LalibelaAuthor Commented:
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
0
 
LalibelaAuthor Commented:
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
0
 
PAQ_ManCommented:
PAQed with points refunded (500)

PAQ_Man
Community Support Moderator
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now