In this example demonstrate how to read
Excel(.xls) file in java using apache poi jar file is need for dependency.
// Import class
import java.io.File;
import java.io.IOException;
import jxl.Cell; //class from apache POI
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Test {
private String inputFile;
public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}
public void read() throws IOException {
File inputWorkbook = new File(inputFile);
Workbook w;
try {
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
for (int j = 0; j < sheet.getColumns(); j++) {
for (int i = 0; i < sheet.getRows(); i++) {
Cell cell = sheet.getCell(j, i);
CellType type = cell.getType();
// It return cell if type is lable
if (type == CellType.LABEL) {
System.out.println("I got a label "
+ cell.getContents());
}
// It return cell if type is number
if (type == CellType.NUMBER) {
System.out.println("I got a number "
+ cell.getContents());
}
}
}
} catch (BiffException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
Test test = new Test();
test.setInputFile("C:\\Users\\cuboidology8\\Desktop\\New folder\\temp.xls");
//Set file location for reading
test.read();
}
}