Dynamically output the EXCEL document and the solution of Chinese garbled problems with JSP

xiaoxiao2021-03-06  132

Dynamically output the EXCEL document and the solution of Chinese garbled problems with JSP

Author: Chen Wanfei

I recently saw a Java to manipulate the Open Source with Java, I tried it on WebLogic, I feel very good, here I recommend it.

First go to http://www.andykhan.com/jexcelapi/index.html to download the latest JEXCELAPI, put JXL.jar in your classpath. Write a JavaBean, use Jexcelapi to dynamically generate an Excel document, I write a simplest, schematic. Complicated you may want to query the database what. //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.WRITABLEWORK 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.WRITABECECELLFORMAT WCFFC = New JXL.WRITE.WRITABECECELLFORMAT (WFC);

Wcffc.setBackground; JXL.FORMAT.COLOUR.RED;

Labelc = new jxl.write.label (6, 0, "China love me", WCFFC);

WS.Addcell (labelc);

// Write an EXEL worksheet

Wwb.write ();

// Close Excel work thin object

WWB.CLOSE ();

}

/ / It is best to write a such main method to test whether 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 to use 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 very big, you can use IE to access Test_Excel.jsp to open dynamically generated Excel documents in IE. Nothing is no.

Maybe someone will ask: response.reset (); can not do this, my suggestion must be written unless you can guarantee that there is no other thing in response's buffer. Some people may ask: I started at the beginning of JSP and <% @ Page ContentType = "Application / VND.MS-Excel; Charset = GBK"%> This sentence removes Response.SetContentType ("Application / VND.MS-Excel" ); Can you do it? Answer this question is very simple, just check the JSP server to build JAVA code after compiling JSP, if this is changed to this, the schematic code of the Java file generated after my Welogic7 compiles Test_Excel.jsp After:

Public void _jspservice (javax.servlet.http.httpservletRequest Request,

Javax.Servlet.http.httpservletResponse response) THROWS JAVA.IO.IEXCEPTION,

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.getut ();

Jspwriter_originalout = OUT;

Javax.Servlet.http.httpsession session = request.getations (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 ...

}

Obviously, shielding response.setContentType ("Application / Vnd.ms-Excel"); after Test.WriteExcel (response.getputstream ()); before, response.reset (); then did not set the correct type of Response ContentType, Of course, the output is garbled. The source code for the correct output of the EXCEL's JSP is like this:

Public void _jspservice (javax.servlet.http.httpservletRequest Request,

Javax.Servlet.http.httpservletResponse response) THROWS JAVA.IO.IEXCEPTION,

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.getut ();

Jspwriter_originalout = OUT;

Javax.Servlet.http.httpsession session = request.getations (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 ...

}

Everyone can see in response.reset (); after Test.WriteExcel (response.getoutPutStream ()); previously set up the output content of Response. Therefore, the output is normal.

Finally, I hope this article can inspire you. If there is any mistake, please criticize and correct!

Reference

Http://www.mail-archive.com/poi-user@jakarta.apache.org/msg02800.html

转载请注明原文地址:https://www.9cbs.com/read-127049.html

New Post(0)