1. First download the Poi-3.6-20091214.jar, as follows:
http://download.csdn.net/detail/evangel_z/3895051
2.student.java
public class Student
{
private int id;
private String name;
private int age;
Private Date birth;
Public Student ()
{
}
public Student (int ID, String name, Int. age, Date birth)
{
This.id = ID;
THIS.name = name;
This.age = age;
This.birth = birth;
}
public int getId ()
{
return ID;
}
public void setId (int id)
{
This.id = ID;
}
Public String GetName ()
{
return name;
}
public void SetName (String name)
{
THIS.name = name;
}
public int Getage ()
{
return age;
}
public void Setage (int.)
{
This.age = age;
}
Public Date Getbirth ()
{
return birth;
}
public void Setbirth (Date birth)
{
This.birth = birth;
}
}
3.createsimpleexceltodisk.java
Import Java.io.FileOutputStream; Import Java.text.SimpleDateFormat; Import java.util.ArrayList; Import java.util.List; 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; public class Createsimpleexceltodisk {/** * @ Function: Manually build a simple format of Excel */private static List<stud Ent> getstudent () throws Exception {List List = new ArrayList (); SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd"); Student user1 = new Student (1, "Zhang San", +, Df.parse ("1997-03-12")); Student user2 = new Student (2, "John Doe", +, Df.parse ("1996-08-12")); Student User3 = new Student (3, "Harry", +, Df.parse ("1985-11-12")); List.add (user1); List.add (User2); List.add (USER3); return list; public static void Main (string[] args) throws Exception {//First step, create a webbook, corresponding to an Excel file Hssfworkbook wb = new Hssfworkbook (); In the second step, add a sheet in WebBook, corresponding to the sheet hssfsheet sheet = wb.createsheet ("Student Form One") in the Excel file; In the third step, add the No. 0 row of the table header to the sheet, noting that the old version of POI has a limit on the number of rows in Excel short Hssfrow row = Sheet.createrow ((int) 0); Fourth step, create the cell, and set the value header to set the table header centered Hssfcellstyle style = Wb.createcellstyle (); Style.setalignment (Hssfcellstyle.align_center); Create a center format Hssfcell cell = Row.createcell ((short) 0); Cell.setcellvalue ("School Number"); Cell.setcellstyle (style); Cell = Row.createcell ((short) 1); Cell.setcellvalue ("name"); Cell.setcellstyle (style); Cell = Row.createcell ((short) 2); Cell.setcellvalue ("Age"); Cell.setcellstyle (style); Cell = Row.createcell ((short) 3); Cell.setcellvalue ("Birthday"); cell.seTcellstyle (style); The fifth step, write the Entity data in the actual application of this data from the database, list list = Createsimpleexceltodisk.getstudent (); for (int i = 0; i < list.size (); i++) {row = Sheet.createrow ((int) i + 1); Student stu = (Student) list.get (i); Fourth step, create the cell and set the value Row.createcell ((short) 0). Setcellvalue ((double) Stu.getid ()); Row.createcell ((short) 1). Setcellvalue (Stu.getname ()); Row.createcell ((short) 2). Setcellvalue ((double) stu.getage ()); Cell = Row.createcell ((short) 3); Cell.setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd"). Format (stu. Getbirth ())); }//Sixth, save the file to the specified location try {fileoutputstream fout = new FileOutputStream ("E:/stud Ents.xls "); Wb.write (Fout); Fout.close (); } catch (Exception e) {e.printstacktrace (); } } }
Java POI Export Excel table