Introduced
Jakarta POI is a set of Java APIs for accessing Microsoft-formatted documents. Jakarta POI has a number of components, including HSSF for manipulating Excel format files and HWPF for manipulating word, where only the HSSF that are currently used to manipulate Excel are relatively mature in a variety of components. Official Homepage Http://poi.apache.org/index.html,API Documentation http://poi.apache.org/apidocs/index.html
Realize
A complete comment has been added to the code.
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
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 Exceloperate {public static void main (string[] args) {//Create Excel Table Createexcel (Getstudent ());
Read Excel table List<student> List = Readexcel ();
System.out.println (List.tostring ()); /** * Initialization Data * * @return Data/private static list<student> getstudent () {List<student> ;
List = new arraylist<student> ();
Student student1 = new Student ("Xiaoming", 8, "second Grade");
Student Student2 = new Student ("small-aperture", 9, "third grade");
Student Student3 = new Student ("Floret", 10, "four Grade");
List.add (STUDENT1); List.adD (Student2);
List.add (STUDENT3);
return list; /** * Create Excel * * @param list * Data/private static void Createexcel (list<student> list
{///create an Excel file Hssfworkbook workbook = new Hssfworkbook ();
Create a worksheet Hssfsheet sheet = workbook.createsheet ("Student form One");
Add table header row Hssfrow Hssfrow = sheet.createrow (0);
Set cell formatting to center Hssfcellstyle cellstyle = Workbook.createcellstyle ();
Cellstyle.setalignment (Hssfcellstyle.align_center);
Add header content Hssfcell Headcell = Hssfrow.createcell (0);
Headcell.setcellvalue ("name");
Headcell.setcellstyle (CellStyle);
Headcell = Hssfrow.createcell (1);
Headcell.setcellvalue ("Age");
Headcell.setcellstyle (CellStyle);
Headcell = Hssfrow.createcell (2);
Headcell.setcellvalue ("grade");
Headcell.setcellstyle (CellStyle);
Add data content for (int i = 0; i < list.size (); i++) {Hssfrow = Sheet.createrow ((int) i + 1);
Student Student = List.get (i);
Create a cell and set the value Hssfcell cell = Hssfrow.createcell (0);
Cell.setcellvalue (Student.getname ());
Cell.setcellstyle (CellStyle);
Cell = Hssfrow.createcell (1);
Cell.setcellvalue (Student.getage ());
Cell.setcellstyle (CellStyle);
Cell = Hssfrow.createcell (2);
Cell.setcellvalue (Student.getgrade ());
Cell.setcellstyle (CellStyle);
//Save the Excel file try {outputstream outputstream = new FileOutputStream ("D:/students.xls");
Workbook.write (OutputStream);
Outputstream.close ();
catch (Exception e) {e.printstacktrace (); }/** * Read Excel * * @return data collection/private static list<student> Readexcel () {List<stu
dent> list = new arraylist<student> ();
Hssfworkbook workbook = null;
try {//Read Excel file InputStream InputStream = new FileInputStream ("D:/students.xls");
workbook = new Hssfworkbook (InputStream);
Inputstream.close (); } CAtch (Exception e) {e.printstacktrace (); }//circular sheet for (int numsheet = 0; Numsheet < workbook.getnumberofsheets (); numsheet++) {Hssfsheet Hssfs
Heet = Workbook.getsheetat (Numsheet);
if (Hssfsheet = = null) {continue; //Loop line for (int rownum = 1; rownum <= hssfsheet.getlastrownum (); rownum++) {Hssfrow Hssfrow = HS
Sfsheet.getrow (rownum);
if (Hssfrow = = null) {continue;
The contents of the cell are stored in the collection Student Student = new Student ();
Hssfcell cell = Hssfrow.getcell (0);
if (cell = = null) {continue;
} student.setname (Cell.getstringcellvalue ());
Cell = Hssfrow.getcell (1);
if (cell = = null) {continue;
} student.setage ((int) cell.getnumericcellvalue ());
Cell = Hssfrow.getcell (2);
if (cell = = null) {continue;
} student.setgrade (Cell.getstringcellvalue ()); List.add (student);
} return list; }
}
Attach the code for the Student class
public class Student {
private String name;
private int age;
private String grade;
Public Student () {
} public
Student (string name, int age, string grade) {
super ();
this.name = name;
This.age = age;
This.grade = grade;
}
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 String Getgrade () {return
grade;
}
public void Setgrade (String grade) {
this.grade = grade;
}
@Override public
String toString () {return
"Student [name=] + name +, age=" + Age + ", grade=" + Grade
+ "]";
}
}
Test results
Exported Excel tables
Students
Print the read Excel data
[Student [Name= Xiao Ming, age=8, grade= second year], Student [name= Small, age=9, grade=, third grade], Student [name= floret, age=10, grade=, Grade four]]
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.