JSP Word documents, Excel documents, PDF documents

Source: Internet
Author: User

========================================================== =============================== Original blog, reprinted please declare the source of Electronic coffee (original id blue rock) ========================================================== ================================

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 (){

}

}

3. generate an Excel document directly on the client

<% @ Page contenttype = "application/vnd. MS-Excel; charset = gb2312" %>

<%

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

%>

<HTML>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

<Title> Generate </title>

</Head>

<Body>

Generate example excel. Copy the code to the web page from the HTML tab, and print the JSP page as needed.

</Body>

</Html>

4. Generate a PDF file directly on the client

Download the jar package: the following code passes the test in jdk1.4 resin2.16

Itext pack http://mesh.dl.sourceforge.net/sourceforge/itext/itext-1.3.5.jar

Character bag http://itext.sourceforge.net/downloads/iTextAsian.jar

JSP is generated on the client ie side and opened directly

Ie_mirror.jsp

<% @

Page import = "Java. Io. *, java. AWT. Color, Com. lowagie. Text. *, com.lowagie.text}. *" %>

<%

Response. setcontenttype ("application/pdf ");

Document document = new document ();

Bytearrayoutputstream buffer = new bytearrayoutputstream ();

Response writer = Response writer. getinstance (document, buffer );

Document. open ();

Document. Add (new paragraph ("Hello World "));

Document. Close ();

Dataoutput output = new dataoutputstream (response. getoutputstream ());

Byte [] bytes = buffer. tobytearray ();

Response. setcontentlength (bytes. Length );

For (INT I = 0; I <bytes. length; I ++) {output. writebyte (Bytes [I]);}

%>

-------------------------------

Generate a file on the server without downloading it.

Server_assist.jsp

<% @ Page import = "com. lowagie. Text. *, com.lowagie.text=. *, java. Io. *" %>

<%

String filename = "D: // test // 111111.20 ";

Document document = new document (pagesize. A4 );

Servletoutputstream out1 = response. getoutputstream ();

Try {

Using writer = Using writer. getinstance (document, new fileoutputstream (filename ));

Document. open ();

Document. Add (new paragraph ("Hello World Chinese "));

Document. Close ();

}

Catch (exception e ){}

%>

You can use itext to set the font of text, which is the most important question for Chinese programmers. Fortunately, there is a specialized package in itext to set the fonts for Asian countries. You can download this package from http://itext.sourceforge.net/downloads/itextasian.jar. Then you can put it directly in your classpath. How to set the font?

Basefont bfchinese = basefont. createfont ("stsong-light", "UniGB-UCS2-H", basefont. not_embedded );

Font fontchinese = new font (bfchinese, 12, Font. Normal );

The above Code sets the display of Chinese fonts. You only need to use the following code to include Chinese characters in the PDF.

String title = "I love coffee ";

Paragraph T = new paragraph (title, fontchinese );

Doc. Add (t );

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.