Public void ImportFile (Multipartfile uploadfile) throws IOException {
String fileName = Uploadfile.getoriginalfilename ();
String fileType = "";
if (Filename.lastindexof (".")! =-1
&& Filename.lastindexof (".") + 1 < filename.length ()) {
FileType = filename.substring (Filename.lastindexof (".") + 1);
}
try {
InputStream fis = Uploadfile.getinputstream ();
Workbook Workbook = null;
if (Filetype.tolowercase (). Equals ("xlsx")) {
workbook = new Xssfworkbook (FIS);
} else if (Filetype.tolowercase (). Equals ("xls")) {
workbook = new Hssfworkbook (FIS);
}
//Get each sheet table
int numberofsheets = Workbook.getnumberofsheets ();
for (int i = 0; i < numberofsheets; i++) {
Sheet Sheet = Workbook.getsheetat (i);
//Get each row
For (int j = 1; J < Sheet.getphysicalnumberofrows (); j + +) {
xssfrow row = (Xssfrow) sheet.getrow (j);
if (row! = null) {
//Get each cell
for (int k = 0; k < sheet.getrow (0)
. Getphysicalnumberofcells (); k++) {
cell cell = Row.getcell (k);
//Set return value type
Cell.setcelltype (cell.cell_type_string);
}
}
}
fis.close ();
}
} catch (Exception e) {
e.printstacktrace ();
}
}
Handling of return value types
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 ());
}
}
Java uses POI to upload and parse Excel documents