package testexcel;
import java.io.IOException;
import java.io.*;
import java.util.Iterator;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
public class Readingexcel {
public static void main
(String[] args
) {
try {
System.
out.
println("before reading");
POIFSFileSystem fs
= new POIFSFileSystem
(new FileInputStream("/home/something/Desktop/Test.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheet("first");
HSSFRow row;
HSSFCell cell;
int rows; // No of rows
rows = sheet.getPhysicalNumberOfRows();
System.
out.
println(sheet.
getRow(1).
getPhysicalNumberOfCells());
int cols = 0; // No of columns
int tmp = 0;
// This trick ensures that we get the data properly even if it doesn’t start from first few rows
for (int i = 0; i < 10 || i < rows; i++) {
row = sheet.getRow(i);
if (row != null) {
tmp = sheet.getRow(i).getPhysicalNumberOfCells();
if (tmp > cols) {
cols = tmp;
}
}
}
for (int r = 0; r < rows; r++) {
row = sheet.getRow(r);
if (row != null) {
for (int c = 0; c < cols; c++) {
cell = row.getCell((short) c);
if (cell != null) {
// Your code here
// s = cell.getData();
System.
out.
println(cell.
getStringCellValue());
}
}
}
}
ioe.printStackTrace();
}
}
}