1. First download the Poi-3.6-20091214.jar, as follows:
http://download.csdn.net/detail/evangel_z/3895051
2.student.java
Import Java.util.Date;
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 an excel in a simple format
*/
private static list<student> 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
{
The first step is to create a webbook that corresponds to an Excel file
Hssfworkbook wb = new Hssfworkbook ();
In the second step, add a sheet in WebBook that corresponds to the sheet in the Excel file
Hssfsheet sheet = wb.createsheet ("Student form One");
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 a cell, and set the value header to center the header
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:/students.xls");
Wb.write (Fout);
Fout.close ();
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}
Poi implements Java export Excel features