Save the query list content to an Excel table and save it to the appropriate disk

Source: Internet
Author: User

1, first import the corresponding jar package

2, a small demo test "entity class + Test class: How to save Excel"

Student entity class

public class student{

private int id;

private String name;

Private String Email;

Private Date birth;

The corresponding set, get method

There are also constructors (arguments, non-parametric)

···············

}

Test class

public class test{

public static list<student> Getstudent () throws parseexception{

List List = new ArrayList ();

SimpleDateFormat SF = new SimpleDateFormat ("Yyyy-mm-dd");//Conversion time format

Student stu = new Student (1, "haha", "[email protected]", Sf.parse ("1992-09-20"));

·······

List.add (Stu);

return list;

}

Specific implementation of the program to export Excel tables

public static void Main (string[] args) {

The first step is to create a workbook that corresponds to an Excel file

Hssfworkbook wb = new Hssfworkbook ();

The second step is to create a sheet in workbook

Hssfsheet sheet = Wb.createsheet ();

Step three, create a table header in sheet

Hssfrow row = sheet.createrow (0);//or ((int) 0);

Fourth step, create a cell style, cell

Hssfcellstyle style = Wb.createcellstyle ();

Style.setalignment (hssfcellstyle.align_general);//Cell style

Hssfcell cell = Row.createcell (0);

Cell.setcellvalue ("School Number");
Cell.setcellstyle (style);
Cell = Row.createcell (1);
Cell.setcellvalue ("name");
Cell.setcellstyle (style);
Cell = Row.createcell (2);

····· First line of the table "wardrobe"

Fifth step, write the Entity data

List List = Test.getstudent ();

for () {

row = Sheet.createrow (i+1);
Student stu = (Student) list.get (i);
Set the value of a cell
Row.createcell (0). Setcellvalue (Stu.getid ());
Row.createcell (1). Setcellvalue (Stu.getname ());

Cell = Row.createcell (3);//Further conversion of the time format
Cell.setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd"). Format (Stu.getbirth ()));

}

The sixth step is to create a good file output
try {
FileOutputStream fout = new FileOutputStream ("D:/student.xls");
Wb.write (Fout);
Fout.close ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

}

The program completes, runs, will see in the D disk directory under the Stu.xls such a form;

Save the query list content to an Excel table and save it to the appropriate disk

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.