Recently on the Internet to see a Java to manipulate the open source of Excel, WebLogic on a trial, feel very good, hereby recommend to you.
First go to http://www.andykhan.com/jexcelapi/index.html to download the latest Jexcelapi and put Jxl.jar in your classpath.
Write a JavaBean, use JEXCELAPI to dynamically generate Excel documents, I write here a simplest, schematic. Complex you may have to query the database or something.
test.java///////////////////////////////////////////
Package com.jagie.test;
Import java.io.*;
Import jxl.*;
Import jxl.write.*;
Import jxl.format.*;
Import java.util.*;
Import Java.awt.Color;
Public class test{
public static void Writeexcel (OutputStream os) throws Exception {
Jxl.write.WritableWorkboo K WWB = Workbook.createworkbook (OS);
Jxl.write.WritableSheet ws = Wwb.createsheet ("TestSheet1", 0);
Jxl.write.Label LABELC = new Jxl.write.Label (0, 0, "I love China");
Ws.addcell (LABELC);
Jxl.write.WritableFont WFC = new Jxl.write.WritableFont (writablefont.arial,20, Writablefont.bold, False,
Underlinestyle.no_underline, Jxl.format.Colour.GREEN);
Jxl.write.WritableCellFormat WCFFC = new Jxl.write.WritableCellFormat (WFC);
Wcffc.setbackground (Jxl.format.Colour.RED);
LABELC = new Jxl.write.Label (6, 0, "China Loves Me", WCFFC);
Ws.addcell (LABELC);
//write to Exel worksheet
Wwb.write ();
Closes the Excel workbook Object
Wwb.close ();
It's a good idea to write a main method to test whether or not your class is written.
public static void Main (string[] args) throws exception{
File F=new file ("Kk.xls");
F.createnewfile ();
Writeexcel (new FileOutputStream (f));
}
}
Write a JSP that uses test this javabean to output an Excel document.
test_excel.jsp//////////////////////////
<% @page import= "Com.jagie.test.Test"%>
<%
Response.reset ();
Response.setcontenttype ("application/vnd.ms-excel");
Test.writeexcel (Response.getoutputstream ());
%>
This is done, you use IE access test_excel.jsp can open the dynamically generated Excel documents in IE. It's not a bit garbled.
Some people may ask: Response.reset (); Can not this sentence, my suggestion is sure to write, unless you are able to ensure that there is nothing else in the response buffer.
Others may ask: I add <% to the beginning of the JSP @page contenttype= "APPLICATION/VND.MS-EXCEL;CHARSET=GBK"%> this sentence, remove Response.setcontenttype (" Application/vnd.ms-excel "); The answer to this question is very simple, is to see the JSP server compiled JSP generated Java code, if so, my welogic7 compiled test_ The schematic code for the Java file generated after excel.jsp is this:
public void _jspservice (Javax.servlet.http.HttpServletRequest request,
Javax.servlet.http.HttpServletResponse response) throws Java.io.IOException,
javax.servlet.ServletException {
Declare and set well-known variables:
Javax.servlet.ServletConfig config = Getservletconfig ();
Javax.servlet.ServletContext application = Config.getservletcontext ();
Javax.servlet.jsp.tagext.Tag _activetag = null;
Variables for TAG Extension protocol
Object page = this;
Javax.servlet.jsp.JspWriter out;
Javax.servlet.jsp.PageContext PageContext =
Javax.servlet.jsp.JspFactory.getDefaultFactory (). Getpagecontext (This,
Request, response, NULL, True, 8192, true);
Response.setheader ("Content-type", "application/vnd.ms-excel; CHARSET=GBK ");
out = Pagecontext.getout ();
JspWriter _originalout = out;
Javax.servlet.http.HttpSession session = Request.getsession (true);
try {//error page try block
Response.setcontenttype ("APPLICATION/VND.MS-EXCEL;CHARSET=GBK");
Out.print ("\r\n\r\n\r\n\r\n");
Out.print ("\ r \ n");
[/test_excel.jsp; Line:6]
Response.reset (); [/test_excel.jsp; Line:7]
Response.setcontenttype ("application/vnd.ms-excel");
[/test_excel.jsp; Line:8]
Test.writeexcel (Response.getoutputstream ()); [/test_excel.jsp; Line:9]
catch (Throwable __ee) {
while (out!= null && out!= _originalout) out = Pagecontext.popbody ();
((Weblogic.servlet.jsp.PageContextImpl) PageContext). Handlepageexception ((throwable) __ee);
}
Before final close brace ...
}
It is evident that the shielding Response.setcontenttype ("application/vnd.ms-excel"), after the Test.writeexcel (Response.getoutputstream ()); Response.reset (); Response contenttype is not set after the correct type, of course, the output is garbled. And the correct output of Excel JSP compiled the source code is this:
public void _jspservice (Javax.servlet.http.HttpServletRequest request,
Javax.servlet.http.HttpServletResponse response) throws Java.io.IOException,
Javax.servlet.ServletException
{
Declare and set well-known variables:
Javax.servlet.ServletConfig config = Getservletconfig ();
Javax.servlet.ServletContext application = Config.getservletcontext ();
Javax.servlet.jsp.tagext.Tag _activetag = null;
Variables for TAG Extension protocol
Object page = this;
Javax.servlet.jsp.JspWriter out;
Javax.servlet.jsp.PageContext PageContext =
Javax.servlet.jsp.JspFactory.getDefaultFactory (). Getpagecontext (This, request, response, NULL, True, 8192, true);
out = Pagecontext.getout ();
JspWriter _originalout = out;
Javax.servlet.http.HttpSession session = Request.getsession (true);
try {//error page try block
Out.print ("\ r \ n");
[/test_excel.jsp; Line:2]
Response.reset (); [/test_excel.jsp; Line:3]
Response.setcontenttype ("application/vnd.ms-excel"); [/test_excel.jsp; Line:4]
Test.writeexcel (Response.getoutputstream ()); [/test_excel.jsp; Line:5]
catch (Throwable __ee) {
while (out!= null && out!= _originalout) out = Pagecontext.popbody ();
((Weblogic.servlet.jsp.PageContextImpl) PageContext). Handlepageexception ((throwable) __ee);
}
Before final close brace ...
}
You can see in Response.reset (); After that, Test.writeexcel (Response.getoutputstream ()); The response output was previously set correctly. So the output is normal.
Finally, I hope this article can enlighten you, if there is a mistake, please criticize me!