Using POI implementations in 1.java to import Excel
public class Xlsdto {
/**
* Number of courses selected
*/
Private Integer Xkh;
/**
* School Number
*/
Private String XH;
/**
* Name
*/
Private String XM;
/**
* College
*/
Private String YXSMC;
/**
* Course Number
*/
Private Integer kch;
/**
* Course Name
*/
Private String KCM;
/**
* Score
*/
private float CJ;
Public Integer Getxkh () {
return xkh;
}
public void Setxkh (Integer xkh) {
This.xkh = Xkh;
}
Public String Getxh () {
return XH;
}
public void Setxh (String xh) {
This.xh = XH;
}
Public String Getxm () {
return XM;
}
public void Setxm (String xm) {
THIS.XM = XM;
}
Public String GETYXSMC () {
return YXSMC;
}
public void SETYXSMC (String yxsmc) {
THIS.YXSMC = YXSMC;
}
Public Integer Getkch () {
return KCH;
}
public void Setkch (Integer kch) {
This.kch = KCH;
}
Public String getkcm () {
return KCM;
}
public void setkcm (String kcm) {
THIS.KCM = KCM;
}
public float GETCJ () {
return CJ;
}
public void SETCJ (float CJ) {
THIS.CJ = CJ;
}
}
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.ArrayList;
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;
/**
*
* @author hongten</br>
*
* Reference Address: http://hao0610.iteye.com/blog/1160678
*
*/
public class Xlsmain {
/**
* Read xls file contents
*
* @return List<xlsdto> Object
* @throws IOException
* Input/output (I/O) exceptions
*/
Private List<xlsdto> Readxls () throws IOException {
InputStream is = new FileInputStream ("Pldrxkxxmb.xls");
Hssfworkbook Hssfworkbook = new Hssfworkbook (IS);
Xlsdto xlsdto = null;
list<xlsdto> list = new arraylist<xlsdto> ();
Looping Worksheets Sheet
for (int numsheet = 0; Numsheet < hssfworkbook.getnumberofsheets (); numsheet++) {
Hssfsheet Hssfsheet = Hssfworkbook.getsheetat (Numsheet);
if (Hssfsheet = = null) {
Continue
}
Looping rows row
for (int rowNum = 1; rowNum <= hssfsheet.getlastrownum (); rownum++) {
Hssfrow Hssfrow = Hssfsheet.getrow (RowNum);
if (Hssfrow = = null) {
Continue
}
Xlsdto = new Xlsdto ();
Cyclic column cell
0 School Number 1 Name 2 College 3 course name 4 results
for (int cellnum = 0; cellnum <=4; cellnum++) {
Hssfcell xh = Hssfrow.getcell (0);
if (XH = = null) {
Continue
}
Xlsdto.setxh (GetValue (XH));
Hssfcell XM = Hssfrow.getcell (1);
if (XM = = null) {
Continue
}
XLSDTO.SETXM (GetValue (XM));
Hssfcell YXSMC = Hssfrow.getcell (2);
if (YXSMC = = null) {
Continue
}
XLSDTO.SETYXSMC (GetValue (YXSMC));
Hssfcell KCM = Hssfrow.getcell (3);
if (KCM = = null) {
Continue
}
XLSDTO.SETKCM (GetValue (KCM));
Hssfcell CJ = Hssfrow.getcell (4);
if (CJ = = null) {
Continue
}
XLSDTO.SETCJ (Float.parsefloat (GetValue (CJ));
List.add (xlsdto);
}
}
return list;
}
/**
* Get the values in Excel table
*
* @param Hssfcell
* Every grid in Excel
* @return values in each of the squares in Excel
*/
@SuppressWarnings ("Static-access")
Private String GetValue (Hssfcell Hssfcell) {
if (hssfcell.getcelltype () = = Hssfcell.cell_type_boolean) {
Returns the value of a Boolean type
Return string.valueof (Hssfcell.getbooleancellvalue ());
} else if (hssfcell.getcelltype () = = Hssfcell.cell_type_numeric) {
Returns the value of a numeric type
Return string.valueof (Hssfcell.getnumericcellvalue ());
} else {
Returns the value of a string type
Return string.valueof (Hssfcell.getstringcellvalue ());
}
}
}
Importing Excel Using POI implementations in Java