Skip to content

Instantly share code, notes, and snippets.

@moinuddin14
Created April 7, 2017 06:42
Show Gist options
  • Save moinuddin14/7713529933f3893478fd02baca46c2c0 to your computer and use it in GitHub Desktop.
Save moinuddin14/7713529933f3893478fd02baca46c2c0 to your computer and use it in GitHub Desktop.
Read all values from excel spreadsheet
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
/**
* Created by apple on 4/7/17.
*/
public class ReadAllCells {
static HSSFRow row;
public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException {
FileInputStream fileInputStream = new FileInputStream(new File(System.getProperty("user.dir") + "/data/test.xls"));
//OPCPackage pkg = OPCPackage.open(new File(System.getProperty("user.dir")+"/data/test.xls"));
//XSSFWorkbook xl = new HSSFWorkbook(pkg);
HSSFWorkbook xl = new HSSFWorkbook(new POIFSFileSystem(fileInputStream));
//This is 0 based index start
//Sheet xlSheet = xl.getSheetAt(0);
HSSFSheet spreadsheet = xl.getSheetAt(0);
Iterator<Row> rowIterator = spreadsheet.iterator();
while (rowIterator.hasNext()) {
row = (HSSFRow) rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.print(
cell.getNumericCellValue() + " \t\t ");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(
cell.getStringCellValue() + " \t\t ");
break;
}
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment