JSP uses POI to build Excel and export the sample _jsp programming in the page

Source: Internet
Author: User

Java export Excel has two components to use, one is JXL, one is poi, I use the poi here. Export can be generated on the server file, and then download, you can also use the output stream directly in the Web page Pop-up dialog box prompts the user to save or download. The way files are generated can cause garbage files in the server to be implemented in a way that is not elegant, so here I am using the following way directly through the output stream.

1, modify the Web server conf/web.xml, add XML code

<mime-mapping> 
    <extension>xls</extension> 
    <mime-type>application/vnd.ms-excel </mime-type> 
 

If you do not add this, then the download in the Web page will become a JSP file

2, download.jsp documents

<%@ page contenttype= "application/vnd.ms-excel" language= "java" import= "java.util.*, Com.shangyu.action.WriteExcel "pageencoding=" GBK "%><% 
response.setheader (" Content-disposition "," Attachment;filename=test123.xls ");//Specify the file name of the download 
response.setcontenttype (" application/vnd.ms-excel ");  
Writeexcel we=new writeexcel (); 
We.getexcel ("111.xls", Response.getoutputstream ()); 
%> 

Be careful not to have HTML code, and in addition to <%%> the middle of the code, other places do not have spaces. Otherwise, when you export the file will appear in the background of the exception, although not affect the use of the program, to make it look uncomfortable

3, Writeexcel.java generation of Excel JavaBean, complex application please check the API

Package com.shangyu.action; 
 
Import java.io.*; 
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook; 
Import Org.apache.poi.hssf.usermodel.HSSFSheet; 
Import Org.apache.poi.hssf.usermodel.HSSFRow; 
Import Org.apache.poi.hssf.usermodel.HSSFCell; public class Writeexcel {public void Getexcel (String sheetname,outputstream output) {Hssfworkbook wb=new 
 Hssfworkbook (); 
 Hssfsheet Sheet1=wb.createsheet ("Sheet1"); 
 Hssfrow Row=sheet1.createrow ((short) 0); 
 Hssfcell Cell=row.createcell ((short) 0); 
  
 Cell.setcellvalue (1); 
 Row.createcell ((short) 1). Setcellvalue (2); 
 Row.createcell ((short) 2). Setcellvalue (3); 
  
  
 Row.createcell ((short) 3). Setcellvalue ("Chinese character"); 
 Row=sheet1.createrow ((short) 1); 
 Cell=row.createcell ((short) 0); 
  
 Cell.setcellvalue (1); 
 Row.createcell ((short) 1). Setcellvalue (2); 
 Row.createcell ((short) 2). Setcellvalue (3); 
  
 Row.createcell ((short) 3). Setcellvalue ("Chinese character"); 
  
 FileOutputStream fileout=new FileOutputStream ("Workbook.xls");  
 try {    Output.flush ();  
     Wb.write (output); 
 Output.close ();  
     catch (IOException e) {e.printstacktrace ();  
 System.out.println ("Output is closed"); 
 }  
 } 
}

With the above three steps, you should be able to directly generate an Excel file to download or save, which is quite useful in some information systems.

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.