1. Export the list collection type data file
Packagecom.mi.entity;Importjava.util.Date; Public classStudent {Private intID; PrivateString name; Private intAge ; PrivateDate Birth; PublicStudent (intID, String name,intAge , Date birth) { Super(); This. ID =ID; This. Name =name; This. Age =Age ; This. Birth =birth; }}
Generate Excel File code
PackageCom.mi.util;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFCellStyle;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;Importcom.mi.entity.Student; Public classCreatesimpleexceltodisk {/*** Manually create a list containing student *@return * @throwsException*/ Private StaticList<student> getstudent ()throwsException {List<Student> list =NewArraylist<>(); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd"); Student User1=NewStudent (1, "Zhang San", Sdf.parse ("1997-03-12"))); Student User2=NewStudent (2, "John Doe", +, Sdf.parse ("1996-08-12")); Student User3=NewStudent (3, "Harry", Sdf.parse ("1985-11-12"))); List.add (user1); List.add (User2); List.add (USER3); returnlist; } Public Static voidMain (string[] args)throwsException {//The first step is to create a WebBook file that corresponds to an Excel fileHssfworkbook WB =NewHssfworkbook (); //Second, add a sheet workbook in Excel with the name of the workbook, not the default;Hssfsheet sheet = wb.createsheet ("Student table 1"); //third, do the sheet add the table No. 0 line, note that the old version of the POI on the number of lines of Excel is limited shortHssfrow row = Sheet.createrow ((int) 0); //Fourth, create a cell header to set the table header centeredHssfcellstyle style =Wb.createcellstyle (); Style.setalignment (hssfcellstyle.align_center);//Create a center format//To create a cell for specific data, consider using the cell as a common objectHssfcell cell = Row.createcell ((int) 0); Cell.setcellvalue ("School Number"); Cell= Row.createcell ((int) 1); Cell.setcellvalue (Name); Cell= Row.createcell ((int) 2); Cell.setcellvalue (Age); Cell= Row.createcell ((int) 3); Cell.setcellvalue (Birthday); //Fifth, the data written in the actual application of the Entity data should be obtained from the databaselist<student> list =createsimpleexceltodisk.getstudent (); for(intI=0;i<list.size (); i++){ //each time you create a new row and insert a list of data objects in a new row, it's a bit cumbersome, maybe a better encapsulation method, left to look at laterrow = Sheet.createrow ((int) i+1); Row.createcell ((int) 0). Setcellvalue (List.get (i). GetId ()); Row.createcell ((int) 1). Setcellvalue (List.get (i). GetName ()); Row.createcell ((int) 2). Setcellvalue (List.get (i). Getage ()); Row.createcell ((int) 3). Setcellvalue (NewSimpleDateFormat ("Yyyy-mm-dd"). Format (List.get (i). Getbirth ())); } //Sixth, save the file to the specified locationFileOutputStream Fout =NewFileOutputStream ("D:/student.xls"); Wb.write (Fout); Fout.close (); }}
Effect:
Note: Download the Poi-3.6-20091214.jar first, as follows:
http://download.csdn.net/detail/evangel_z/3895051
Java generates Excel export file