1. Import the relevant JAR package first
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
- {
- //First step, create a webbook, corresponding to an Excel file
- Hssfworkbook WB = new Hssfworkbook ();
- ///second step, add a sheet in WebBook, corresponding to the sheet in the Excel file
- Hssfsheet sheet = wb.createsheet ("student form One");
- ///step three, add the header line No. 0 to the sheet, and note that the older 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 center the table 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);
- //Fifth, write the data from the database in the actual application of the physical data,
- 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 a 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 ();
- }
- }
- }
Java implementation Export Excel table POI