There are two ways to import Excel: JXL and POI
1. JXL parsing Excel
public class Jxlreadexcel {
/**
* JXL parsing Excel
* @author David
* @param args
*/
public static void Main (string[] args) {
try {
Create Workbook
Workbook Workbook = Workbook.getworkbook (New File ("E:/students.xls"));
Gets the first sheet of sheet
Sheet Sheet = workbook.getsheet (0);
Get Data
for (int i = 0; i < sheet.getrows (); i++) {
for (int j = 0; J < Sheet.getcolumns (); j + +) {
Cell cell = Sheet.getcell (j,i);
System.out.print (cell.getcontents () + "");
}
System.out.println ();
}
Workbook.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}
}
2. POI parsing Excel
public class Poireadexcel {
/**
* Poi Parse Excel file content
* @author David
* @param args
*/
public static void Main (string[] args) {
Excel files that need to be parsed
File File = new file ("E:/students.xls");
try {
Create Excel to read file contents
Hssfworkbook Workbook =
New Hssfworkbook (Fileutils.openinputstream (file));
Read Default first worksheet sheet
Hssfsheet sheet = workbook.getsheetat (0);
int firstrownum = 0;
Gets the last line number in the sheet
int lastrownum = Sheet.getlastrownum ();
for (int i = firstrownum; I <=lastRowNum; i++) {
Hssfrow row = Sheet.getrow (i);
Gets the last cell column number of the current line
int lastcellnum = Row.getlastcellnum ();
for (int j = 0; J < Lastcellnum; J + +) {
Hssfcell cell = Row.getcell (j);
if (Row.getcell (j)!=null) {
Row.getcell (j). Setcelltype (cell.cell_type_string);
}
String value = Cell.getstringcellvalue ();
System.out.print (Value + "");
}
System.out.println ();
}
} catch (IOException e) {
E.printstacktrace ();
}
}
}
Two ways to import Java Excel JXL and POI