JSP generate WORD Documents

Source: Internet
Author: User

JSP generate WORD Documents[Date: 2006-12-18]

Source: http://www.gs008.com/blog/blogview.asp? Logid = 324

In the web-OA system, document management seems indispensable. Sometimes some data needs to be queried from the database and output in a certain format and displayed as Word documents, sometimes Many Word documents are saved to the Blob field of a table in the database, and the server then presents the image file stored in the Blob field to the user. I found that there are very few such articles through searching on the Internet. I will sort them out for your reference.

1. Generate Word documents directly on the client
It is very easy to generate a Word document on the JSP page. You just need to change contenttype = "text/html" to contenttype = "application/MSWord; charset = gb2312". The Code is as follows:

<% @ Page contenttype = "application/MSWord; charset = gb2312" %>

Through settings, the content of the original page can be displayed in word.

To download a Word document, add the following code on the JSP page:

<%

Response. setheader ("content-disposition", "attachment?filename=filename.doc ");

%>

In filename.doc, filename is the name of the Word Document to be downloaded. It can be customized by <% = docname %> from the line, as shown below:

<%

Response. setheader ("content-disposition", "attachment; filename = <% = docname %>. Doc ");

%>

A prompt is provided for the user to select, as shown in

 

TIPS: If a programmer needs to follow the format designed on the word before generating a Word document, he can copy the Word format and paste it to the frontpage. Then, paste the HTML code to the JSP page.

2. output the word entity in the database on the client
Here we will only discuss how to output the Word document entity in the Blob field of Oracle by the client. The getblobbean class is called, which provides the function of retrieving blob from Oracle. The Code is as follows:

Package yourpackage;

Import javax. servlet .*;

Import javax. servlet. http .*;

Import java. Io .*;

Import java. util .*;

Import oracle. SQL .*;

Import beans. yourbeanpackage. getblobbean;

/**

* <P> title: </P>

* <P> Description: </P>

* <P> copyright: Copyright (c) 2004 </P>

* <P> company: </P>

* @ Author not attributable

* @ Version 1.0

*/

Public class getblobservlet1 extends httpservlet {

// Set the output content type. This setting is very important. Otherwise, the client browser cannot identify the output content, resulting in a pop-up download dialog box.

Private Static final string content_type = "application/MSWord; charset = gb2312 ";

// Initialize global variables

Public void Init () throws servletexception {

}

// Process the http get request

Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {

Response. setcontenttype (content_type );

Perform (request, response );

}

Public void perform (httpservletrequest request, httpservletresponse response ){

Try {

// This function extracts blob objects from Oracle crying

Getblobbean getblob = new getblobbean ();

Outputstream SOS = response. getoutputstream ();

Getblob. connfunction ();

Oracle. SQL. Blob blob = getblob. getblob ("cehui ");

// Output the Word Document

If (blob! = NULL ){

Inputstream Pi = blob. getbinarystream ();

Int blobsize = (INT) blob. Length ();

Byte [] blobbytes = new byte [blobsize];

Int bytesread = 0;

While (bytesread = pi. Read (blobbytes ))! =-1 ){

SOS. Write (blobbytes, 0, bytesread );

}

Pi. Close ();

SOS. Flush ();

SOS. Close ();

}

Getblob. dropconnfunction ();

} Catch (exception e ){

System. Out. println (E. tostring ());

}

}

// Clean up resources

Public void destroy (){

}

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.