Import Java.io.File;
Import Java.io.FileInputStream;
Import Org.apache.poi.ss.usermodel.Row;
Import Org.apache.poi.ss.usermodel.Sheet;
Import Org.apache.poi.ss.usermodel.Workbook;
Import Org.apache.poi.ss.usermodel.WorkbookFactory;
public class Testexcel {
public static void Main (string[] args) {
try {
File File = new file ("C://users/administrator/desktop/test.xls");
FileInputStream is = new FileInputStream (file);
Workbook Workbook = Workbookfactory.create (IS);//This way Excel 2003/2007/2010 can handle
Sheet Sheet = workbook.getsheetat (0);//First Sheet
int rowCount = Sheet.getphysicalnumberofrows ();//Get Total rows
Row Row1 = sheet.getrow (0);//Get the first line to determine if Excel is canonical
if (!row1.getcell (0). toString (). Trim (). Equals ("ordinal") | |
!row1.getcell (1). toString (). Trim (). Equals ("Evaluation Year") | |
!row1.getcell (2). toString (). Trim (). Equals ("Taxpayer identification number") | |
!row1.getcell (3). toString (). Trim (). Equals ("Taxpayer name") | |
!row1.getcell (4). toString (). Trim (). Equals ("Evaluation result")) {
Return
}
for (int i=1;i<rowcount;i++) {
Row row = Sheet.getrow (i);
int cellcount = Row.getphysicalnumberofcells (); Get total number of columns
String XH = "";//serial number
String pjnd = "";//Evaluation Year
String NSRSBH = "";//Taxpayer Identification number
String nsrmc = "";//Taxpayer name
String PJJG = "";//Evaluation Results
for (int c=0;c<cellcount;c++) {
String cellstr = Row.getcell (c). toString (). Trim ();
if (!cellstr.equals ("") &&cellstr!=null) {
if (c==0) xh=cellstr;
if (c==1) pjnd=cellstr;
if (c==2) nsrsbh=cellstr;
if (c==3) nsrmc=cellstr;
if (c==4) pjjg=cellstr;
}
}
System.out.println (xh+ "|" +pjnd+ "|" +nsrsbh+ "|" +nsrmc+ "|" +PJJG);//Get Data for database operations
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}
Java read Excel2003, 2007, 2010 Tutorials