Java Export Excel (simple case)

Source: Internet
Author: User

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;
}

...............

}

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;

Import com.sun.entity.Student;

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 ();
}
}
}

Jar Package Used

Poi-3.8.jar
Poi-examples-3.8.jar
Poi-excelant-3.8.jar
Poi-ooxml-3.8.jar
Poi-ooxml-schemas-3.8.jar
Poi-scratchpad-3.8.jar

Java Export Excel (simple case)

Related Article

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.