Use the poi component to write data to an Excel file
Last Update:2018-12-05
Source: Internet
Author: User
Code: Package com. Way. Poi. writetoexcel; import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. arrays;
Import java. util. List; import org. Apache. Poi. hssf. usermodel. hssfcell;
Import org. Apache. Poi. hssf. usermodel. hssfrow;
Import org. Apache. Poi. hssf. usermodel. hssfsheet;
Import org. Apache. Poi. hssf. usermodel. hssfworkbook; import com. Way. Poi. Bean. Student ;/**
*
* @ Author fenzaiway
*
* Simulate the data, write the data into excel,
*
*/
Public class generateexcel {
Private Static string xlspath = "D: \ student.xls ";
Private Static list <student> studentlist = NULL;
Private Static student [] students = new student [4];
/**
* Static block initialization data
*/
Static {
Studentlist = new arraylist <student> ();
Students [0] = new student ("Zhang San", "male", 23, "Class 1", 94 );
Students [1] = new student ("Li Si", "female", 22, "Class 2", 90 );
Students [2] = new student ("Wang Wu", "male", 21, "Class 5", 84 );
Students [3] = new student ("Zhao ", "female", 24, "Class 3", 98 );
Studentlist. addall (arrays. aslist (students ));
}
/**
* Main Function
*/
Public static void main (string [] ARGs ){
Long start = system. currenttimemillis ();
Generateexcel (xlspath );
Long end = system. currenttimemillis ();
System. Out. println ("Time consumed:" + (end-Start) + "millisecond ");
}
// Create an Excel file
Public static void generateexcel (string path ){
// Create a workbook object
Hssfworkbook WB = new hssfworkbook ();
// Create a worksheet object and name it
Hssfsheet sheet = WB. createsheet ("Student Information statistical table ");
// Traverse the set object to create rows and cells
For (INT I = 0; I <studentlist. Size (); I ++ ){
// Retrieve the student object
Student = studentlist. Get (I );
// Create a row
Hssfrow ROW = sheet. createrow (I );
// Create a cell and assign values
Hssfcell namecell = row. createcell (0 );
Namecell. setcellvalue (student. getname ());
Hssfcell gendercell = row. createcell (1 );
Gendercell. setcellvalue (student. getgender ());
Hssfcell agecell = row. createcell (2 );
Agecell. setcellvalue (student. getage ());
Hssfcell sclasscell = row. createcell (3 );
Sclasscell. setcellvalue (student. getsclass ());
Hssfcell scorecell = row. createcell (4 );
Sclasscell. setcellvalue (student. getscore ());
}
// Generate a file
File file = new file (PATH );
Fileoutputstream Fos = NULL;
Try {
Fos = new fileoutputstream (File );
WB. Write (FOS );
} Catch (exception e ){
E. printstacktrace ();
} Finally {
If (FOS! = NULL ){
Try {
FOS. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
}
} Bean: Package com. Way. Poi. Bean; public class student {private string name;
Private string gender;
Private int age;
Private string sclass;
Private int score;
Public student (){
Super ();
}
Public student (string name, string gender, int age, string sclass, int score ){
Super ();
This. Name = Name;
This. Gender = gender;
This. Age = age;
This. sclass = sclass;
This. Score = score;
} Public String tostring (){
Return "student [age =" + age + ", Gender =" + gender + ", name =" + name + ", sclass =" + sclass + ", score = "+ score +"] ";
} Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public String getgender (){
Return gender;
}
Public void setgender (string gender ){
This. Gender = gender;
}
Public int getage (){
Return age;
}
Public void setage (INT age ){
This. Age = age;
}
Public String getsclass (){
Return sclass;
}
Public void setsclass (string sclass ){
This. sclass = sclass;
}
Public int getscore (){
Return score;
}
Public void setscore (INT score ){
This. Score = score;
}
}