Java Implementation Excel table export

Source: Internet
Author: User

Java implementation Export Excel table POI

1. First download the Poi-3.6-20091214.jar, as follows:

http://download.csdn.net/detail/evangel_z/3895051

2. Student.javaImportjava.util.Date;  Public classStudent {Private intID; PrivateString name; Private intAge ; PrivateDate Birth;  PublicStudent () {} PublicStudent (intID, String name,intAge , Date birth) {           This. ID =ID;  This. Name =name;  This. Age =Age ;  This. Birth =birth; }         Public intgetId () {returnID; }         Public voidSetId (intID) { This. ID =ID; }         PublicString GetName () {returnname; }         Public voidsetName (String name) { This. Name =name; }         Public intGetage () {returnAge ; }         Public voidSetage (intAge ) {           This. Age =Age ; }         PublicDate Getbirth () {returnbirth; }         Public voidSetbirth (Date birth) { This. Birth =birth; }    }  3. Createsimpleexceltodisk.javaImportJava.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;  Public classCreatesimpleexceltodisk {/*** @ Function: Manually build an excel in a simple format*/      Private StaticList<student> getstudent ()throwsException {List List=NewArrayList (); SimpleDateFormat DF=NewSimpleDateFormat ("Yyyy-mm-dd"); Student User1=NewStudent (1, "Zhang San", Df.parse ("1997-03-12"))); Student User2=NewStudent (2, "John Doe", +, Df.parse ("1996-08-12")); Student User3=NewStudent (3, "Harry", Df.parse ("1985-11-12")));          List.add (user1);          List.add (User2);            List.add (USER3); returnlist; }         Public Static voidMain (string[] args)throwsException {//The first step is to create a webbook that corresponds to an Excel fileHssfworkbook WB =NewHssfworkbook (); //In the second step, add a sheet in WebBook that corresponds to the sheet in the Excel fileHssfsheet 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 shortHssfrow row = Sheet.createrow ((int) 0); //Fourth step, create a cell, and set the value header to center the headerHssfcellstyle style =Wb.createcellstyle (); Style.setalignment (Hssfcellstyle.align_center); //Create a center formatHssfcell 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(inti = 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 valueRow.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 (NewSimpleDateFormat ("Yyyy-mm-dd"). Format (stu. Getbirth ())); }          //Sixth, save the file to the specified location        Try{fileoutputstream Fout=NewFileOutputStream ("E:/students.xls");              Wb.write (Fout);          Fout.close (); }          Catch(Exception e) {e.printstacktrace (); }      }  }  

Java Implementation Excel table export

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.