Because of the project requirements, you need to export the data as an Excel table, and you can choose to export items that you can download. The SPRING+MYBATIS+SPRINGMVC framework used by the project uses Apache POI to export Excel. Poi specific use please own Baidu. Words not much to say, on the code.
Exportexcelutil Code
Package com.rixin.common.util;
Import Java.io.OutputStream;
Import Java.lang.reflect.Method;
Import Java.net.URLEncoder;
Import java.util.Collection;
Import Java.util.Iterator;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Org.apache.poi.hssf.usermodel.HSSFCellStyle;
Import Org.apache.poi.hssf.usermodel.HSSFRow;
Import Org.apache.poi.hssf.usermodel.HSSFSheet;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import Org.apache.poi.ss.usermodel.Font;
/** * POI Java EE Export Excel Tool class * * @author wjmisaboy@gmail.com * @see POI/public class Exportexcelutil {/** * * @param response * Request * @param filename * file name such as: "Student table" * @param excelheader * Excel table header array, holding "name #name" format string , "name" is the Excel header row, "name" is the Object field name * @param dataList * Data collection, must match the field name in the table header array, and conforms to the JavaBean specification * @return returns a hssfworkbook * @throws Exception */public static <T> Hssfworkbook Export (httpservletresponse response, String FileName, string[ ] Excelheader, collection<t> dataList) throws Exception {//Set request Response.setcontenttype ("Application/applica
Tion/vnd.ms-excel ");
Response.setheader ("Content-disposition", "attachment;filename=" + urlencoder.encode (filename + ". xls", "UTF-8"));
Create a workbook, corresponding to an Excel file Hssfworkbook wb = new Hssfworkbook ();
Set heading style Hssfcellstyle TitleStyle = Wb.createcellstyle (); Sets the cell border style titlestyle.setbordertop (Hssfcellstyle.border_thin);//top Border thin edge titlestyle.setborderbottom (
Hssfcellstyle.border_thin)//Bottom Border thin line titlestyle.setborderleft (Hssfcellstyle.border_thin);//left Border thin line Titlestyle.setborderright (Hssfcellstyle.border_thin)//Right Border thin edge//Set cell alignment titlestyle.setalignment ( Hssfcellstyle.align_center); Horizontally centered titlestyle.setverticalalignment (hssfcellstyle.vertical_center);
Vertical Center//Set font style Titlefont = Wb.createfont (); Titlefont.setfontheightinpoints ((short) 15); Font height titlefont.setfontname ("blackbody"); Font style titlestyle.setfont (Titlefont);
Add a sheet to the workbook, corresponding to the sheet hssfsheet sheet = wb.createsheet (fileName) in the Excel file;
Title Array string[] Titlearray = new String[excelheader.length];
Field array Group string[] Fieldarray = new String[excelheader.length]; for (int i = 0; i < excelheader.length i++) {string[] Temparray = Excelheader[i].split ("#");//temporary array split # Titlear
Ray[i] = temparray[0];
Fieldarray[i] = temparray[1]; ////Add header row to sheet hssfrow row = sheet.createrow ((int) 0);//number of rows starting from 0 Hssfcell Sequencecell = Row.createcell (0);//Cell
The column starts with 0 and the first column adds ordinal sequencecell.setcellvalue ("ordinal");
Sequencecell.setcellstyle (TitleStyle); Sheet.autosizecolumn (0);//Auto Set width//Assign value for header row (int i = 0; i < titlearray.length; i++) {Hssfcell Titlecell = R
Ow.createcell (i + 1);//No. 0 is occupied by serial number, so +1 titlecell.setcellvalue (Titlearray[i]) is required;
Titlecell.setcellstyle (TitleStyle); Sheet.autosizecolumn (i + 1);//No. 0 bit is occupied by ordinal number, so +1}//Data styles are required because the headings and data styles need to be set separately otherwise they will cover hssfcellstyle Datastyle = Wb.create
CellStyle (); Set the data Border Datastyle.setborderbottom (Hssfcellstyle.border_thin);
Datastyle.setbordertop (Hssfcellstyle.border_thin);
Datastyle.setborderleft (Hssfcellstyle.border_thin);
Datastyle.setborderright (Hssfcellstyle.border_thin); Sets the center style datastyle.setalignment (Hssfcellstyle.align_center); Horizontally centered datastyle.setverticalalignment (hssfcellstyle.vertical_center);
Vertical Center//Set Data font Datafont = Wb.createfont (); Datafont.setfontheightinpoints ((short) 12); The font height datafont.setfontname ("song Body");
Font Datastyle.setfont (Datafont);
Traversing collection data, generating data rows iterator<t> it = Datalist.iterator ();
int index = 0;
while (It.hasnext ()) {index++;//No. 0 bit is occupied so +1 row = Sheet.createrow (index);
Assign a value to an ordinal Hssfcell Sequencecellvalue = Row.createcell (0);//The ordinal value is always the No. 0 column sequencecellvalue.setcellvalue (index);
Sequencecellvalue.setcellstyle (Datastyle);
Sheet.autosizecolumn (0);
T t = (t) it.next (); Using reflection, the corresponding GetXXX () method is dynamically invoked to get the property value for (int i = 0; i < Fiel) according to the array group of the field passed over.Darray.length;
i++) {Hssfcell Datacell = Row.createcell (i + 1);
Datacell.setcellstyle (Datastyle);
Sheet.autosizecolumn (i + 1);
String fieldName = Fieldarray[i]; String getmethodname = "Get" + fieldname.substring (0, 1). toUpperCase () + fieldname.substring (1);//obtain corresponding GetXXX () method Clas s<? Extends object> tcls = T.getclass ()//The generic is object and all object Subclass Method GetMethod = Tcls.getmethod (Getmethodname, new Class[] {})//Get the corresponding method by means of method Object value = Getmethod.invoke (t, new object[] {});//dynamic caller, Get property value if (value!= null)
{Datacell.setcellvalue (value.tostring ());//Assign value to current column}} OutputStream outputstream = Response.getoutputstream ();//Open stream Wb.write (outputstream);//Hssfworkbook Write stream Wb.close (
//Hssfworkbook close Outputstream.flush ();//Refresh Stream outputstream.close ();//close stream return to WB; }//Xssfcellstyle.align_center center align//xssfcellstyle.align_left left///Xssfcellstyle.align_right right//XSSFCellStyle . Align//xssfcellstyle.v on Vertical_topErtical_center//Xssfcellstyle.vertical_bottom align//cellstyle.border_double bilateral line//Cellstyle.border_thin thin edge/ /Cellstyle.border_medium medium edge//cellstyle.border_dashed dotted line//Cellstyle.border_hair dot dotted line//Cellstyle.border_thi
CK Thick Sideline}
Controller code
@RequestMapping ("/exportproject.do") public
void Exportproject (httpservletresponse response, String export) {
string[] Excelheader = Export.split (",");
list<project> projectlist = projectservice.getprojects (null);
try {
Exportexcelutil.export (response, "Project table", Excelheader, projectlist);
} catch (Exception e) {
E.printstacktrace ();
}
JSP part Code
...
<div style= "margin-top:5px" class= "Am-input-group am-u-sm-4" > <span class= "Am-input-group-label
" ><input type= "checkbox"
value= "project name #name" name= "Export" checked= "checked" ></span> <input
type= "text" value= "project name" disabled= "Disabled" >
</div>
...
The figure below
The above is the entire content of this article, I hope to help you learn.