Java uses POI to upload and parse Excel documents

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.