1. Importing POI-related jar packages
Org.apache.poi jar
2. Code examples
Public List getallexcel (file file, string tableName, String fname,
String Enterpriseid, String ReportID, String ProjectID)
Throws FileNotFoundException, IOException, ClassNotFoundException,
Instantiationexception, Illegalaccessexception,
Nosuchmethodexception, SecurityException, IllegalArgumentException,
InvocationTargetException, ParseException {
List Listt = new ArrayList ();
try {
FileInputStream fis = new FileInputStream (file);
Workbook Workbook = null;
if (Fname.tolowercase (). EndsWith ("xlsx")) {
workbook = new Xssfworkbook (FIS);
} else if (Fname.tolowercase (). EndsWith ("xls")) {
workbook = new Hssfworkbook (new Poifsfilesystem (FIS));
}
int numberofsheets = Workbook.getnumberofsheets ();
for (int i = 0; i < numberofsheets; i++) {
Sheet Sheet = Workbook.getsheetat (i);
for (int j = 1; J < Sheet.getphysicalnumberofrows (); j + +) {//Get each row
Xssfrow row = (Xssfrow) sheet.getrow (j);
if (row!=null) {
List List = new ArrayList ();
for (int k = 0; k < sheet.getrow (0). Getphysicalnumberofcells (); k++) {//Get each cell
Cell cell = Row.getcell (k);
if (cell = = null) {
List.add ("");
Continue
}
Switch (Cell.getcelltype ()) {
Case cell.cell_type_string:
List.add (Cell.getrichstringcellvalue (). getString ());
Break
Case Cell.cell_type_numeric:
if (dateutil.iscelldateformatted (cell)) {
List.add (Cell.getdatecellvalue ());
} else {
List.add (Cell.getnumericcellvalue ());
}
Break
Case Cell.cell_type_boolean:
List.add (Cell.getbooleancellvalue ());
Break
Case Cell.cell_type_formula:
List.add (Cell.getcellformula ());
Break
Default
List.add ("");
Break
}
}
Listt.add (Getbyreflect (TableName, List, Enterpriseid,reportid, ProjectID));
}
}
}
Fis.close ();
} catch (IOException e) {
E.printstacktrace ();
}
return LISTT;
}
Java parses Excel with POI and gets all cell data