Example usage for org.apache.poi.hssf.usermodel HSSFRow cellIterator

List of usage examples for org.apache.poi.hssf.usermodel HSSFRow cellIterator

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFRow cellIterator.

Prototype

@Override
public Iterator<Cell> cellIterator() 

Source Link

Usage

From source file:util.read1.java

public static ArrayList readsecond() throws Exception {
    ////  ww  w  . j a va 2s  . c o m
    // An excel file name. You can create a file name with a full path
    // information.
    //
    String filename = "/home/mtech/UploadedfFiles/x3.xls";
    //String filename = "C:\\Users\\admin\\Desktop\\x3.xls";
    //
    // Create an ArrayList to store the data read from excel sheet.
    //
    List sheetData = new ArrayList();

    FileInputStream fis = null;
    try {
        //
        // Create a FileInputStream that will be use to read the excel file.
        //
        fis = new FileInputStream(filename);

        //
        // Create an excel workbook from the file system.
        //
        HSSFWorkbook workbook = new HSSFWorkbook(fis);
        //
        // Get the first sheet on the workbook.
        //
        HSSFSheet sheet = workbook.getSheetAt(0);

        //
        // When we have a sheet object in hand we can iterator on each
        // sheet's rows and on each row's cells. We store the data read
        // on an ArrayList so that we can printed the content of the excel
        // to the console.
        //
        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            Iterator cells = row.cellIterator();

            List data = new ArrayList();
            while (cells.hasNext()) {
                HSSFCell cell = (HSSFCell) cells.next();
                data.add(cell);
            }

            sheetData.add(data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            fis.close();
        }
    }

    ArrayList a1 = showExelData(sheetData);
    return a1;
}

From source file:utils.ReadWriteExcelFile.java

public static void readXLSFile(String aFile) throws IOException {
    InputStream ExcelFileToRead = new FileInputStream(aFile);
    HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);

    HSSFSheet sheet = wb.getSheetAt(0);/*from  w w  w.j a  va2  s  .  c  o  m*/
    HSSFRow row;
    HSSFCell cell;

    Iterator rows = sheet.rowIterator();

    while (rows.hasNext()) {
        row = (HSSFRow) rows.next();
        Iterator cells = row.cellIterator();

        while (cells.hasNext()) {
            cell = (HSSFCell) cells.next();

            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                System.out.print(cell.getStringCellValue() + " ");
            } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                System.out.print(cell.getNumericCellValue() + " ");
            } else {
                //U Can Handel Boolean, Formula, Errors
            }
        }
        System.out.println();
    }

}