Example usage for org.apache.poi.hssf.usermodel HSSFSheet rowIterator

List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet rowIterator

Introduction

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

Prototype

@Override
public Iterator<Row> rowIterator() 

Source Link

Usage

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

public void readXls(File inputFile) {
    List cellDataList = new ArrayList();
    try {//from  w  w  w. j  a v  a 2s  .  c  o  m
        // 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:users.registration.CreateBtechStudentUsers.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 *
 * @param request servlet request/*from   ww w  . j  a v  a2  s.  c  o  m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    int count = 0;

    String message;
    Session session = null;
    Transaction transaction = null;

    ArrayList<ArrayList<String>> credentialTable = new ArrayList<>();

    ArrayList<String> header = new ArrayList<>();

    header.add("Username");
    header.add("Password");

    credentialTable.add(header);

    try {
        session = hibernate.HibernateUtil.getSessionFactory().openSession();
        transaction = session.beginTransaction();

        /**
         * opening file
         */
        String path = (String) request.getAttribute("filePath");
        FileInputStream userSheet = new FileInputStream(new File(path));
        HSSFWorkbook workbook = new HSSFWorkbook(userSheet);
        HSSFSheet spreadSheet = workbook.getSheetAt(0);

        Iterator<Row> rowIterator = spreadSheet.rowIterator();

        /**
         * Skipping First row
         */
        if (rowIterator.hasNext())
            rowIterator.next();

        /**
         * Reading file row by row
         */
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            if (isEmptyRow(row))
                continue;

            String username = String.valueOf((int) row.getCell(2).getNumericCellValue());

            ArrayList<String> credential = registerUser(username, session);
            insert_Student_Details(username, row, session);
            insert_BTech_Academic_info(username, row, session);

            credentialTable.add(credential);
            count++;
        }

        String filePath = getServletContext().getRealPath("") + File.separator + "files" + File.separator;
        File file = new File(filePath);
        file.mkdir();
        String fileName = "BTech student credential list.pdf";
        PdfGenerator pdfGenerator = new PdfGenerator(filePath + fileName);
        pdfGenerator.createPdf(credentialTable);

        log("file created at : " + filePath + fileName);

        transaction.commit();

        response.sendRedirect("/files/" + fileName);

    } catch (IOException | NoSuchAlgorithmException | RuntimeException | DocumentException e) {

        log(getServletInfo(), e);

        if (transaction != null)
            transaction.rollback();

        message = "Error : " + e.getLocalizedMessage();

        if (e.getCause() != null)
            message += "<br> Cause : " + e.getCause().getLocalizedMessage();

        request.setAttribute("message", message);

        RequestDispatcher rd = request.getRequestDispatcher("/admin/student_management/regiester.jsp");
        rd.forward(request, response);
    } finally {
        if (session != null)
            session.close();

    }
}

From source file:util.read.java

public static Hashtable readfirst() throws Exception {
    ///*from  w  ww. j  a  v a  2s  .c  om*/
    // 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;
}

From source file:util.read1.java

public static ArrayList readsecond() throws Exception {
    ///*from   www  . ja  va2 s .  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:utilesBD.servidoresDatos.JServerServidorDatosExcel.java

public JListDatos recuperarDatosExcel(final JListDatos v, final HSSFSheet hssfSheet,
        JListDatosFiltroConj loFiltro, final String psTabla) throws Exception {
    BufferedReader miDStream = null;
    Iterator<Row> moIterator = hssfSheet.rowIterator();
    try {/*from w  w w.  j av a2s  .  c om*/
        HSSFRow hssfRow = null;
        //si hay cabezera nos la saltamos
        if (mbCabezera && moIterator.hasNext()) {
            hssfRow = (HSSFRow) moIterator.next();
        }
        while (moIterator.hasNext()) {
            hssfRow = (HSSFRow) moIterator.next();
            JFilaDatosDefecto loFilaDef = leeLineaExcel(hssfRow);

            IFilaDatos loFila = JFilaCrearDefecto.getFilaCrear().getFilaDatos(psTabla);
            loFila.setArray(loFilaDef.moArrayDatos());
            if (loFiltro.mbCumpleFiltro(loFila)) {
                v.add(loFila);
            }
        }

        return v;
    } finally {
        if (miDStream != null) {
            miDStream.close();
        }
    }
}

From source file:utility.UploadDataFromFile.java

private void addBillItemMaster() throws IOException, SQLException {
    int i = 0;//  w w w  . j a va  2 s  .  c om
    String filename = jtxtDatabaseLocation.getText();

    //
    // Create an ArrayList to store the data read from excel sheet.
    //
    List sheetData = new ArrayList();

    FileInputStream fis = null;
    //
    // 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 = sheet.getRow(i);
        i++;

        List data = new ArrayList();
        if (row != null) {
            for (int j = 0; j < 8; j++) {
                HSSFCell cell = row.getCell(j);
                if (cell != null) {
                    data.add(cell.toString().toUpperCase());
                } else {
                    data.add("");
                }
            }
        } else {
            break;
        }

        sheetData.add(data);
    }
    addBillItemMasterINDatabase(sheetData);
}

From source file:utility.UploadDataFromFile.java

private void addDoctorAccountMaster() throws IOException, SQLException {
    int i = 0;//from w  w  w  .ja  v  a 2s  .  c o m
    String filename = jtxtDatabaseLocation.getText();

    //
    // Create an ArrayList to store the data read from excel sheet.
    //
    List sheetData = new ArrayList();

    FileInputStream fis = null;
    //
    // 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 = sheet.getRow(i);
        i++;

        List data = new ArrayList();
        if (row != null) {
            for (int j = 0; j < 9; j++) {
                HSSFCell cell = row.getCell(j);
                if (cell != null) {
                    data.add(cell.toString().toUpperCase());
                } else {
                    data.add("");
                }
            }
        } else {
            break;
        }

        sheetData.add(data);
    }
    addAccountInDatabase(sheetData);
}

From source file:utility.UploadDataFromFile.java

private void addReferanceDoctorAccountMaster() throws IOException, SQLException {
    int i = 0;//  w  w  w  .  j  av  a2 s .c  o  m
    String filename = jtxtDatabaseLocation.getText();

    //
    // Create an ArrayList to store the data read from excel sheet.
    //
    List sheetData = new ArrayList();

    FileInputStream fis = null;
    //
    // 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 = sheet.getRow(i);
        i++;

        List data = new ArrayList();
        if (row != null) {
            for (int j = 0; j < 8; j++) {
                HSSFCell cell = row.getCell(j);
                if (cell != null) {
                    data.add(cell.toString().toUpperCase());
                } else {
                    data.add("");
                }
            }
        } else {
            break;
        }

        sheetData.add(data);
    }
    addRefDoctorAccountInDatabase(sheetData);
}

From source file:utility.UploadDataFromFile.java

private void addDoctorMaster() throws IOException, SQLException {
    int i = 0;//from w w  w. j  ava  2s  .co  m
    String filename = jtxtDatabaseLocation.getText();

    //
    // Create an ArrayList to store the data read from excel sheet.
    //
    List sheetData = new ArrayList();

    FileInputStream fis = null;
    //
    // 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 = sheet.getRow(i);
        i++;

        List data = new ArrayList();
        if (row != null) {
            for (int j = 0; j < 3; j++) {
                HSSFCell cell = row.getCell(j);
                if (cell != null) {
                    data.add(cell.toString().toUpperCase());
                } else {
                    data.add("");
                }
            }
        } else {
            break;
        }

        sheetData.add(data);
    }
    addDoctorInDatabase(sheetData);
}

From source file:utility.UploadDataFromFile.java

private void addPatientMaster() throws IOException, SQLException {
    int i = 0;/* ww  w. j  a va  2s. c o  m*/
    String filename = jtxtDatabaseLocation.getText();

    //
    // Create an ArrayList to store the data read from excel sheet.
    //
    List sheetData = new ArrayList();

    FileInputStream fis = null;
    //
    // 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 = sheet.getRow(i);
        i++;

        List data = new ArrayList();
        if (row != null) {
            for (int j = 0; j < 11; j++) {
                HSSFCell cell = row.getCell(j);
                if (cell != null) {
                    data.add(cell.toString().toUpperCase());
                } else {
                    data.add("");
                }
            }
        } else {
            break;
        }

        sheetData.add(data);
    }
    addPatientInDatabase(sheetData);
}