Java implementation Export Excel table POI

Source: Internet
Author: User

2. Student.java



ImportJava.util.Date;

PublicClassStudent
{
PrivateIntId
PrivateString name;
PrivateIntAge
PrivateDate birth;

PublicStudent ()
{
}

Public Student (int ID, String name,IntAge, Date birth)
{
This.id =Id
THIS.name =Name
This.age =Age
This.birth =Birth
}

PublicIntGetId ()
{
ReturnId
}

Publicvoid SetId (IntId
{
This.id =Id
}

PublicString GetName ()
{
ReturnName
}

PublicvoidSetName (String name)
{
THIS.name =Name
}

PublicIntGetage ()
{
ReturnAge
}

Publicvoid Setage (IntAge
{
This.age =Age
}

PublicDate Getbirth ()
{
ReturnBirth
}

PublicvoidSetbirth (Date birth)
{
This.birth =Birth
}

}
3. Createsimpleexceltodisk.java




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;

PublicClassCreatesimpleexceltodisk
{
/**
* @ Function: Manually build an excel in a simple format
*/
PrivateStatic list<student> getstudent ()ThrowsException
{
List List =NewArrayList ();
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);

ReturnList
}

PublicStaticvoid Main (string[] args)ThrowsException
{
//The first step is to create a webbook that corresponds to an Excel file
Hssfworkbook WB =NewHssfworkbook ();
//In the second step, add a sheet in WebBook that corresponds to the sheet in the Excel file
Hssfsheet sheet = wb.createsheet ("Student table 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 (); 
       } 
   } 

Java implementation Export Excel table POI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.