Java parses Excel with POI and gets all cell data

Source: Internet
Author: User

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

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.