JSP dynamic output of Excel and Chinese garbled Solutions

Source: Internet
Author: User

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
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 view the JavaCodeIf 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 =
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 =
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 articleArticleI am inspired by you. If you have any mistakes, please criticize and correct them!

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.