I recently saw an open source that uses java to manipulate excel on the Internet. I tried it on weblogic and thought it was good. I would like to recommend it to you.
First go to the terminal.
Write a javaBean file and use JExcelApi to dynamically generate an excel file. Here I will write a simple and schematic file. Complex, you may need to query the database or something.
/// // Test. java /////////////////////////////////////// ////
Package com. jarie. 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. WritableWorkbook 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, "", wcfFC );
Ws. addCell (labelC );
// Write the Exel Worksheet
Wwb. write ();
// Close the Excel worksheet object
Wwb. close ();
}
// It is best to write such a main method to test whether your class has been written.
Public static void main (String [] args) throws Exception {
File f = new File ("kk.xls ");
F. createNewFile ();
WriteExcel (new FileOutputStream (f ));
}
}
Write a jsp file to use the Test javabean to output the excel file.
/// // Test_excel.jsp //////////// //////////////
<% @ Page import = "com. jarie. test. Test" %>
<%
Response. reset ();
Response. setContentType ("application/vnd. ms-excel ");
Test. writeExcel (response. getOutputStream ());
%>
In this way, you can use ie to access test_excel.jsp to open the dynamically generated excel file in ie. No garbled characters.
Someone may ask: response. reset (); can you leave this sentence alone? I suggest writing it unless you can ensure that there is nothing in the buffer of response.
Someone may ask: I add <% @ page contentType = "application/vnd. ms-excel; charset = GBK "%> to remove response. setContentType ("application/vnd. ms-excel "); OK? To answer this question, it is easy to check the java code generated after jsp is compiled by the jsp server. If so, the schematic code of the java file generated after my welogic7 compiles test_excel.jsp is as follows:
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. getdefafactory Factory (). 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 ");
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...
}
Obviously, blocking response. setContentType ("application/vnd. ms-excel "); then, in the Test. writeExcel (response. getOutputStream (); before, response. the correct type of response contenttype is not set after reset (); of course, the output is garbled. The compiled source code of the jsp correct output of excel is as follows:
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. getdefafactory Factory (). 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 that the output content of response is set correctly before Test. writeExcel (response. getOutputStream (); after response. reset. So the output is normal.
Finally, I hope this article will inspire you. If you have any mistakes, please criticize and correct them!