1, first import the corresponding jar package
2, a small demo test "entity class + Test class: How to save Excel"
Student entity class
public class student{
private int id;
private String name;
Private String Email;
Private Date birth;
The corresponding set, get method
There are also constructors (arguments, non-parametric)
···············
}
Test class
public class test{
public static list<student> Getstudent () throws parseexception{
List List = new ArrayList ();
SimpleDateFormat SF = new SimpleDateFormat ("Yyyy-mm-dd");//Conversion time format
Student stu = new Student (1, "haha", "[email protected]", Sf.parse ("1992-09-20"));
·······
List.add (Stu);
return list;
}
Specific implementation of the program to export Excel tables
public static void Main (string[] args) {
The first step is to create a workbook that corresponds to an Excel file
Hssfworkbook wb = new Hssfworkbook ();
The second step is to create a sheet in workbook
Hssfsheet sheet = Wb.createsheet ();
Step three, create a table header in sheet
Hssfrow row = sheet.createrow (0);//or ((int) 0);
Fourth step, create a cell style, cell
Hssfcellstyle style = Wb.createcellstyle ();
Style.setalignment (hssfcellstyle.align_general);//Cell style
Hssfcell cell = Row.createcell (0);
Cell.setcellvalue ("School Number");
Cell.setcellstyle (style);
Cell = Row.createcell (1);
Cell.setcellvalue ("name");
Cell.setcellstyle (style);
Cell = Row.createcell (2);
····· First line of the table "wardrobe"
Fifth step, write the Entity data
List List = Test.getstudent ();
for () {
row = Sheet.createrow (i+1);
Student stu = (Student) list.get (i);
Set the value of a cell
Row.createcell (0). Setcellvalue (Stu.getid ());
Row.createcell (1). Setcellvalue (Stu.getname ());
Cell = Row.createcell (3);//Further conversion of the time format
Cell.setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd"). Format (Stu.getbirth ()));
}
The sixth step is to create a good file output
try {
FileOutputStream fout = new FileOutputStream ("D:/student.xls");
Wb.write (Fout);
Fout.close ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
The program completes, runs, will see in the D disk directory under the Stu.xls such a form;
Save the query list content to an Excel table and save it to the appropriate disk