POI implement Java Export Excel function

Source: Internet
Author: User

1. First download Poi-3.6-20091214.jar, download the address 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 age)
{
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
{
/**
* @ Features: Manually build a simple format of Excel
*/
private static list<student> Getstudent () throws Exception
{
List List = new ArrayList ();
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd");
Student user1 = new Student (1, "John", Df.parse ("1997-03-12"));
Student user2 = new Student (2, "Dick", Df.parse ("1996-08-12"));
Student User3 = new Student (3, "Harry", Num, 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 to the WebBook, corresponding to the sheet in the Excel file
Hssfsheet sheet = wb.createsheet ("Student form One");
Step three, add the No. 0 row of the table header to the sheet, and note that the old version poi has a limited number of rows in Excel
Hssfrow row = Sheet.createrow ((int) 0);
Step fourth, create the cell, and set the value table 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);
The fifth step is to 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);
Step fourth, 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 ());
}
Step 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 ();
}
}
}

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.