Solutions to Jsp output of excel documents and Chinese garbled characters

Source: Internet
Author: User

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.

 
 
  1. package com.jagie.test
  2. import java.io. 
  3. import jxl.
  4. import jxl.write.
  5. import jxl.format.
  6. import java.util.
  7. import java.awt.Color
  8. public class Test 

Write a JSP file to use the Test javabean to output the excel file.

 
 
  1. <%@page import="com.jagie.test.Test" %> 
  2. <%  
  3. response.reset();  
  4. response.setContentType("application/vnd.ms-excel");  
  5. Test.writeExcel(response.getOutputStream());  
  6. %> 

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:

 
 
  1. public void _jspService(javax.servlet.http.HttpServletRequest
    request,   
  2. javax.servlet.http.HttpServletResponse response) throws 
    java.io.IOException,   
  3. javax.servlet.ServletException  catch (Throwable __ee)   
  4.  
  5.  
  6.         //before final close brace...  
  7.     } 

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:

 
 
  1. public void _jspService(javax.servlet.http.HttpServletRequest request,   
  2. javax.servlet.http.HttpServletResponse response) throws 
    java.io.IOException,  
  3. javax.servlet.ServletException   
  4.      catch (Throwable __ee)   
  5.  
  6.  
  7.         //before final close brace...  
  8.     } 

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.

  1. JSP custom tags are explained in detail from the beginning to the end.
  2. How to configure and use fckeditor in JSP Environment
  3. Storage and display of images in databases Based on JSP
  4. Methods for constructing JSP and Javabean development and release Environments
  5. Examples of JSP + JavaBean + Servlet Working Principles

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.