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:POS.test2.pisps_items.java

public static List<items> showItems() {
    String path = "C:\\\\Users\\\\Guinness\\\\Documents\\\\Projects\\\\Algorithm\\\\pisps records\\\\items.xls";
    FileInputStream fis = null;//from  ww w.j  a  v a 2 s.  c  o m
    List sheetData = new ArrayList();
    try {
        fis = new FileInputStream(path);
        HSSFWorkbook workbook = new HSSFWorkbook(fis);
        HSSFSheet sheet = workbook.getSheetAt(0);
        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) {
        JOptionPane.showMessageDialog(null, "Unsupported Format");
    } finally {

        if (fis != null) {
            try {
                fis.close();

            } catch (IOException ex) {
                Logger.getLogger(pisps_items.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    List<items> datas = new ArrayList();
    try {
        fis = new FileInputStream(path);
        int r = 0;
        int r_set = 1;

        String prev_no = "";
        for (int i = 0; i < sheetData.size(); i++) {
            List list = (List) sheetData.get(i);
            int size = list.size();
            List<String> record = new ArrayList();
            for (int j = 0; j < list.size(); j++) {
                HSSFCell cell = (HSSFCell) list.get(j);
                HSSFDataFormatter hdf = new HSSFDataFormatter();
                String data = "";

                if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                    String mydata = cell.getStringCellValue();
                    data = data + "" + mydata + "";
                    record.add(data);
                } else {
                    data = data + cell.getNumericCellValue() + "";
                    record.add(data);

                }

            }
            String[] aw = new String[size];
            int jj = 0;
            for (String s : record) {

                aw[jj] = s;
                jj++;
            }

            String item_code = FitIn.fmt_woc(aw[7]);
            String description = aw[1];
            String category = aw[2];
            String classification = aw[3];
            String sub_classification = aw[4];
            String brand = aw[5];
            String model = aw[6];

            items item = new items(item_code, description, category, classification, sub_classification, brand,
                    model);
            datas.add(item);
        }
        return datas;

    } catch (FileNotFoundException ex) {
        Logger.getLogger(pisps_items.class.getName()).log(Level.SEVERE, null, ex);
    }
    return datas;

}

From source file:POS.test2.pisps_items.java

public static List<cost> showCost() {

    String path = "C:\\\\Users\\\\Guinness\\\\Documents\\\\Projects\\\\Algorithm\\\\pisps records\\\\cost.xls";
    FileInputStream fis = null;//w  ww .ja va  2 s . c o m
    List sheetData = new ArrayList();
    try {
        fis = new FileInputStream(path);
        HSSFWorkbook workbook = new HSSFWorkbook(fis);
        HSSFSheet sheet = workbook.getSheetAt(0);
        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) {
        JOptionPane.showMessageDialog(null, "Unsupported Format");
    } finally {

        if (fis != null) {
            try {
                fis.close();

            } catch (IOException ex) {
                Logger.getLogger(pisps_items.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    List<cost> datas = new ArrayList();
    try {
        fis = new FileInputStream(path);
        int r = 0;
        int r_set = 1;

        String prev_no = "";
        for (int i = 0; i < sheetData.size(); i++) {
            List list = (List) sheetData.get(i);
            int size = list.size();
            List<String> record = new ArrayList();
            for (int j = 0; j < list.size(); j++) {
                HSSFCell cell = (HSSFCell) list.get(j);
                HSSFDataFormatter hdf = new HSSFDataFormatter();
                String data = "";

                if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                    String mydata = cell.getStringCellValue();
                    data = data + "" + mydata + "";
                    record.add(data);
                } else {
                    data = data + cell.getNumericCellValue() + "";
                    record.add(data);

                }

            }
            String[] aw = new String[size];
            int jj = 0;
            for (String s : record) {

                aw[jj] = s;
                jj++;
            }

            String item_code = FitIn.fmt_woc(aw[0]);
            double cost = FitIn.toDouble(aw[1]);
            double price = FitIn.toDouble(aw[2]);
            cost cos = new cost(item_code, cost, price);

            datas.add(cos);
        }
        return datas;

    } catch (FileNotFoundException ex) {
        Logger.getLogger(pisps_items.class.getName()).log(Level.SEVERE, null, ex);
    }
    return datas;

}

From source file:Principal.Main.java

public static void main(String[] args) throws Exception {

    ///*from w  w w .  jav a  2s  . c  o  m*/
    // An excel file name. You can create a file name with a full
    // path information.
    //
    String filename = "listado.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();
                //   System.out.println("Aadiendo Celda: " + cell.toString());
                data.add(cell);
            }
            sheetData.add(data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
    showExelData(sheetData);
}

From source file:qa.dataset.java

public void readfile() {
    try {/*  w  w w.  j  a v a2 s .  co m*/
        InputStream input = new BufferedInputStream(new FileInputStream("E:/Tugas Akhir Ali/dataset.xls"));

        POIFSFileSystem filesystem = new POIFSFileSystem(input);
        HSSFWorkbook workbook = new HSSFWorkbook(filesystem);
        HSSFSheet sheet = workbook.getSheetAt(0);
        String a;
        String b;

        MaxentTagger tagger = new MaxentTagger(
                "E:/Tugas Akhir Ali/stanford-postagger-2011-04-20/models/left3words-wsj-0-18.tagger");
        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            System.out.println("\n");
            Iterator cells = row.cellIterator();
            while (cells.hasNext()) {
                HSSFCell cell = (HSSFCell) cells.next();
                if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType())
                    System.out.print(cell.getNumericCellValue() + "  ");
                else if (HSSFCell.CELL_TYPE_STRING == cell.getCellType())
                    System.out.print(cell.getStringCellValue() + "  ");

                else if (HSSFCell.CELL_TYPE_BLANK == cell.getCellType())
                    System.out.print("Blank");
                else
                    System.out.print("Unknown");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:spires.printing.Dlg_printing_funeral.java

private void show_dialog() {
    jProgressBar3.setString("Loading...Please wait...");
    jProgressBar3.setIndeterminate(true);
    FileDialog fd = new FileDialog(new JDialog(), "Choose .XLS FILE");
    fd.setVisible(true);//ww  w .  java  2  s  . c o m
    if (fd.getDirectory() == null) {
        return;
    }
    String file = fd.getDirectory() + "" + fd.getFile();
    file = file.replace("\\", "\\\\");
    final String file2 = file;
    lbl_file.setText(file);
    if (file == null || file.isEmpty()) {
        return;
    }
    FileInputStream fis = null;
    final List sheetData = new ArrayList();
    try {
        fis = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(fis);
        HSSFSheet sheet = workbook.getSheetAt(0);
        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) {
        JOptionPane.showMessageDialog(null, "Unsupported Format");
    } finally {

        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(Dlg_printing_funeral.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            List<Encoded_Funeral.to_encoded> datas = Encoded_Funeral.showExcelData(sheetData, file2);
            loadData_encoded(datas);
            lbl_records.setText("" + datas.size());
            jProgressBar3.setString("Finished...");
            jProgressBar3.setIndeterminate(false);
        }
    });

    t.start();
}

From source file:spires.printing.Encoded_baptism.java

public static void main(String[] args) {

    String path = "C:\\Users\\Guinness\\Documents\\Documents\\Cathedral Books\\book_27a_ok - Copy.xls";
    FileInputStream fis = null;/*  w ww.  ja v  a 2  s .c o m*/
    final List sheetData = new ArrayList();
    try {

        fis = new FileInputStream(path);
        HSSFWorkbook workbook = new HSSFWorkbook(fis);
        HSSFSheet sheet = workbook.getSheetAt(0);
        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) {
        JOptionPane.showMessageDialog(null, "Unsupported Format");
    } finally {

        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(Dlg_printing_funeral.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    List<Encoded_baptism> datas = showExcelData(sheetData, path);
    System.out.println("Size: " + datas.size());
    //        for (Encoded_baptism to : datas) {
    //            System.out.println("" + to.index_no);
    //        }

    add_parishioners_1(datas, "27");
}

From source file:Teste.importarExcel2.java

public static List ReadFile(String fileName) {
    List cellVectorHolder = new ArrayList();
    try {//from  w w  w.  j  a va 2  s. co m
        FileInputStream myInput = new FileInputStream(fileName);
        POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
        HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Iterator rowIter = mySheet.rowIterator();
        while (rowIter.hasNext()) {
            HSSFRow myRow = (HSSFRow) rowIter.next();
            Iterator cellIter = myRow.cellIterator();
            List cellStoreVector = new ArrayList();
            while (cellIter.hasNext()) {
                HSSFCell myCell = (HSSFCell) cellIter.next();
                cellStoreVector.add(myCell);
            }
            cellVectorHolder.add(cellStoreVector);
        }
    } catch (Exception e) {
    }
    return cellVectorHolder;
}

From source file:transactionController.PurchaseController.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    try {/*from  ww w  .  j  a va2  s.  c o m*/
        JFileChooser fileChooser = new JFileChooser();
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            List sheetData = new ArrayList();
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            HSSFWorkbook workbook = new HSSFWorkbook(fis);
            HSSFSheet sheet = workbook.getSheetAt(0);
            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.toString().toUpperCase().replaceAll("!", "").trim());
                }

                sheetData.add(data);
            }
            dtm.setRowCount(0);
            for (int i = 0; i < sheetData.size(); i++) {
                List list = (List) sheetData.get(i);
                String sr_cd = "";
                if (itemCode.get(list.get(1).toString()) == null) {
                    sr_cd = lb.getRetrofit().create(SupportAPI.class)
                            .validateData("SERIESMST", "SR_CD", "SR_NAME", list.get(1).toString()).execute()
                            .body().get("data").getAsString();
                    if (sr_cd.equalsIgnoreCase("")) {
                        dtm.setRowCount(0);
                        return;
                    } else {
                        itemCode.put(list.get(1).toString(), sr_cd);
                    }
                } else {
                    sr_cd = itemCode.get(list.get(1).toString());
                }
                Vector row = new Vector();
                row.add(list.get(0).toString());
                row.add(list.get(1).toString());
                row.add(list.get(2).toString());
                row.add(list.get(3).toString());
                row.add("1");
                row.add(list.get(5).toString());
                row.add("");
                row.add("0");
                row.add(list.get(8).toString());
                row.add(list.get(9).toString());
                row.add(list.get(10).toString());
                row.add(list.get(11).toString());
                row.add("0");
                row.add("0");
                row.add(list.get(5).toString());
                row.add(list.get(15).toString());
                row.add(sr_cd);
                dtm.addRow(row);
            }
            setTotal();
        } else {
            System.out.println("File access cancelled by user.");
        }

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:usac.centrocalculo.data.LecturaURyS.java

public void readXls(File inputFile) {
    List cellDataList = new ArrayList();
    try {/*from   w  ww  .j a v a 2s.c om*/
        // Get the workbook instance for XLS file
        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
        // Get first sheet from the workbook
        HSSFSheet sheet = workbook.getSheetAt(0);
        Cell cell;
        Row row;
        Iterator rowIterator = sheet.rowIterator();
        while (rowIterator.hasNext()) {
            HSSFRow hssfRow = (HSSFRow) rowIterator.next();
            Iterator iterator = hssfRow.cellIterator();
            List cellTempList = new ArrayList();
            while (iterator.hasNext()) {
                HSSFCell hssfCell = (HSSFCell) iterator.next();
                cellTempList.add(hssfCell);
            }
            cellDataList.add(cellTempList);
        }
        // Iterate through each rows from first sheet
        /*Iterator<Row> rowIterator = sheet.iterator();
                
         while (rowIterator.hasNext()) {
         row = rowIterator.next();
                
         // For each row, iterate through each columns
         Iterator<Cell> cellIterator = row.cellIterator();
                
         while (cellIterator.hasNext()) {
         cell = cellIterator.next();
                
         switch (cell.getCellType()) {
                
         case Cell.CELL_TYPE_BOOLEAN:
                           
         System.out.print(cell.getBooleanCellValue());
         break;
                
         case Cell.CELL_TYPE_NUMERIC:
         System.out.print(cell.getNumericCellValue());
         break;
                
         case Cell.CELL_TYPE_STRING:
         System.out.print(cell.getStringCellValue());
         break;
                
         case Cell.CELL_TYPE_BLANK:
         //System.out.print("<>");
         break;
                
         default:
         //  System.out.print(cell);
         }
         }
         }*/

    } catch (FileNotFoundException e) {
        System.err.println("Exception" + e.getMessage());
    } catch (IOException e) {
        System.err.println("Exception" + e.getMessage());
    }
    printTo(cellDataList);
    printToConsole(cellDataList);
}

From source file:util.read.java

public static Hashtable readfirst() throws Exception {
    ///*from   w ww  .j a va 2  s  .co m*/
    // An excel file name. You can create a file name with a full path
    // information.
    //
    String filename = "/home/mtech/UploadedfFiles/x2.xls";
    // String filename = "C:\\Users\\admin\\Desktop\\x2.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();
        }
    }

    Hashtable h = showExelData(sheetData);
    return h;
}