An example of jsp generating Excel from POI and exporting it in a page

  • 2021-11-29 08:09:42
  • OfStack

Exporting Excel in java has two components available, one is jxl, one is POI, and I'm using POI here. Export can generate files on the server and then download them, or you can use the output stream to pop up a dialog box directly in the web page to prompt the user to save or download them. The way of generating files will lead to junk files in the server, which is not very elegant, so I use the way of directly passing the output stream here.

1. Modify CONF/web. xml of WEB server and add Xml code


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

If you don't add this, it will become an JSP file when you download it in the web page

2. download. jsp file


<%@ 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 to 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 except for < % % > In the middle of the code, do not have spaces in other places. Otherwise, an exception will appear in the background when exporting the file, although it will not affect the use of the program, and it will look uncomfortable at that time

3. WriteExcel. java generates JavaBean of Excel. Please see API for complex applications


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 ");  
 }  
 } 
} 

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


Related articles: