The example in this article describes how JSP generates Word documents, Excel documents, and PDF documents. Share to everyone for your reference, specific as follows:
In Web-oa system, document management seems to be indispensable, sometimes need to query some data from the database in some format output, and in the form of Word documents, and sometimes many word documents saved to the database in the Blob field of a table, The server then presents the picture file saved in the Blob field to the user. Search through the internet found little about this kind of article, now sorted up for your reference.
1 generating Word documents directly on client side
Generating a Word document on a JSP page is very simple, just change the contenttype= "text/html" to Contenttype= "Application/msword;" charset=gb2312 "can be, the code is as follows:
Copy Code code as follows:
<%@ page contenttype= "Application/msword; charset=gb2312 "%>
You can make the contents of the original page appear in Word by setting.
If you need to download a Word document, simply add the following code to the JSP page:
<%
response.setheader ("Content-disposition", "Attachment;filename=filename.doc");
%>
In Filename.doc, filename is the file name of the Word document to download, which can be customized by <%=docName%> from line, as follows
<%
response.setheader ("Content-disposition", "Attachment;filename=<%=docname%>.doc");
%>
This provides a hint of information for the user to choose from.
Tip: If a programmer needs to build a Word document in a format that he or she has designed beforehand in Word, you can copy the word format and paste it into FrontPage, with the HTML code pasted into the JSP page.
2 The Word entity in the database exists on the client output
Only Word document entities in the BLOB field in the client output Oracle are discussed here. The class Getblobbean is invoked, which provides the ability to remove the blob from Oracle, with the following code:
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 HttpS
Ervlet {//Set output content type, this setting is very important, otherwise the client browser does not recognize the output content, cause 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 Voi D doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {Response.setco
Ntenttype (Content_Type);
Perform (request,response); public void Perform (HttpServletRequest request, httpservletresponse response) {try{//The function is to remove the BLOB entity from the Oracle Cry GetBlob Bean Getblob=new GetBlobbean ();
OutputStream SOS = Response.getoutputstream ();
Getblob.connfunction ();
Oracle.sql.BLOB Blob=getblob.getblob ("Cehui");
Output 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 generating Excel documents directly on the client side
<%@ page contenttype= "application/vnd.ms-excel; charset=gb2312 "%>
<%
response.setheader (" Content-disposition "," Attachment;filename=20050304.xls " );
%>
4 Generate PDF documents directly on client side
Need to download JAR package: The following code is tested under JDK1.4 RESIN2.16
Itext Bag Http://mesh.dl.sourceforge.net/sourceforge/itext/itext-1.3.5.jar
Font Pack Http://itext.sourceforge.net/downloads/iTextAsian.jar
JSP generated to the client IE side directly open
IE_PDF.JSP:
<%@
page import= "java.io.*,java.awt.color,com.lowagie.text.*,com.lowagie.text.pdf.*"%>
<%
Response.setcontenttype ("Application/pdf");
Document document = new document ();
Bytearrayoutputstream buffer = new Bytearrayoutputstream ();
PDFWriter writer=pdfwriter.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]);
%>
No download is generated on the server side.
SERVER_PDF.JSP:
<%@ page import = "com.lowagie.text.*,com.lowagie.text.pdf.*, java.io.*"%>
<%
String filename = "d:// Test//111111.pdf ";
Document document = new document (PAGESIZE.A4);
Servletoutputstream out1 = Response.getoutputstream ();
try{
PDFWriter writer = pdfwriter.getinstance (document, new FileOutputStream (filename));
Document.open ();
Document.add (New Paragraph ("Hello World Chinese Support");
Document.close ();
}
catch (Exception e) {}
%>
Using Itext to set the font of text, how to show Chinese to Chinese programmers is the most critical issue. Luckily Itext has a special package to set up Asian country fonts You can download this package from Http://itext.sourceforge.net/downloads/iTextAsian.jar. Then put it directly into your classpath. How do I set the font?
Basefont Bfchinese = Basefont.createfont ("Stsong-light", "unigb-ucs2-h", basefont.not_embedded);
Font Fontchinese = new Font (Bfchinese, font.normal);
In the above code to set the display of Chinese fonts, you can only use the following code to package Chinese into the PDF
String title = "I Like to drink coffee";
Paragraph t = new Paragraph (title, Fontchinese);
Doc.add (t);
I hope this article will help you with JSP program design.