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.