Example usage for org.apache.poi.hssf.usermodel HSSFWorkbook getSheetAt

List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook getSheetAt

Introduction

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

Prototype

@Override
public HSSFSheet getSheetAt(int index) 

Source Link

Document

Get the HSSFSheet object at the given index.

Usage

From source file:it.redev.parco.job.SoccorsiParserJob.java

License:Open Source License

@Override
public void execute() throws DatabaseException, RemoteSafeException {
    try {//from  ww  w.  j a v a 2  s .  c o m
        addInfoMessage("Caricamento file " + getFilePath());

        InputStream file = super.openStream();

        HSSFWorkbook workbook = new HSSFWorkbook(file);

        Sheet sheet = workbook.getSheetAt(0);

        int rows = sheet.getLastRowNum();

        for (int i = 1; i < rows; i++) {
            Row row = sheet.getRow(i);
            super.addSoccorso(parse(row));
        }

        getInfo().setDataInizio(DateUtils.create(year, month - 1, 1));
        getInfo().setDataFine(DateUtils.lastDayOfMoth(getInfo().getDataInizio()));
        super.saveSoccorsi();

        file.close();
    } catch (FileNotFoundException e) {
        throw new DatabaseException(e);
    } catch (IOException e) {
        throw new DatabaseException(e);
    } catch (Exception e) {
        throw new DatabaseException(e);
    } finally {
        super.removeFileQuietly();
    }

    // Enqueue job soccorsi
    SoccorsiJob job = new SoccorsiJob(month, year, getUser());
    getEntityManager().persist(job.toJob());
}

From source file:it.smartcommunitylab.riciclo.app.importer.converter.DataImporter.java

License:Apache License

private Rifiuti readExcel(InputStream inp) throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
    ExcelExtractor extractor = new ExcelExtractor(wb);

    extractor.setFormulasNotResults(true);
    extractor.setIncludeSheetNames(false);

    Rifiuti rifiuti = new Rifiuti();

    Set<String> sheetNames = Sets.newHashSet();
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        sheetNames.add(wb.getSheetAt(i).getSheetName());
    }/* w  w w  .ja va 2 s  .  co  m*/

    Set missingExpected = Sets.newHashSet(expectedSheets);
    missingExpected.removeAll(sheetNames);

    Set additionalFound = Sets.newHashSet(sheetNames);
    additionalFound.removeAll(expectedSheets);

    if (!missingExpected.isEmpty() || !additionalFound.isEmpty()) {
        throw new ImportError(Lists.newArrayList("Missing sheet(s) expected: " + missingExpected,
                "Additional sheet(s) found: " + additionalFound));
    }

    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        Thread.sleep(500);
        //         System.out.println(sheet.getSheetName());
        //         if (sheet.getRow(0).getLastCellNum() == 1 && !oneColumnAsMany.contains(sheet.getSheetName())) {
        //         } else {
        {
            System.err.println(">" + sheet.getSheetName());
            List<Map<String, String>> result = getSheetMap(sheet);
            mapMap(rifiuti, sheet.getSheetName(), result);
        }
    }

    completePuntiRaccolta(rifiuti);

    return rifiuti;
}

From source file:it.smartcommunitylab.ungiorno.importer.Importer.java

License:Apache License

/**
 * @param appId/*from  w w  w. j a v  a 2 s .  co  m*/
 * @param schoolId
 * @param wb
 * @throws ImportError 
 */
private void mapChildrenData(String appId, String schoolId, HSSFWorkbook wb) throws ImportError {
    Map<String, KidProfile> kidMap = null;
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        List<Map<String, String>> result = getSheetMap(sheet);
        if (SHEET_PROFILO.equals(sheet.getSheetName())) {
            kidMap = parseKidProfiles(appId, schoolId, result);
        }
        if (SHEET_UTENTI.equals(sheet.getSheetName())) {
            parseUsers(appId, schoolId, result, kidMap);
        }
        if (SHEET_DELEGHE.equals(sheet.getSheetName())) {
            parseDeleghe(appId, schoolId, result, kidMap);
        }
        if (SHEET_ALLERGIE.equals(sheet.getSheetName())) {
            parseAllergie(appId, schoolId, result, kidMap);
        }
        if (SHEET_BUS.equals(sheet.getSheetName())) {
            parseKidBus(appId, schoolId, result, kidMap);
        }
        if (SHEET_INSEGNANTI.equals(sheet.getSheetName())) {
            parseInsegnanti(appId, schoolId, result, kidMap);
        }

    }
}

From source file:it.smartcommunitylab.ungiorno.importer.Importer.java

License:Apache License

private HSSFWorkbook readFile(InputStream inp, Set<String> expected) throws IOException, ImportError {

    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
    ExcelExtractor extractor = new ExcelExtractor(wb);

    extractor.setFormulasNotResults(true);
    extractor.setIncludeSheetNames(false);

    Set<String> sheetNames = Sets.newHashSet();
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        sheetNames.add(wb.getSheetAt(i).getSheetName());
    }/*from   w w w  . j  a va2  s .c o  m*/

    Set<String> missingExpected = new HashSet<String>(expected);
    missingExpected.removeAll(sheetNames);

    Set<String> additionalFound = Sets.newHashSet(sheetNames);
    additionalFound.removeAll(expected);

    if (!missingExpected.isEmpty() || !additionalFound.isEmpty()) {
        System.err.println("Missing sheet(s) expected: " + missingExpected + " - Additional sheet(s) found: "
                + additionalFound);
        throw new ImportError(Lists.newArrayList("Missing sheet(s) expected: " + missingExpected,
                "Additional sheet(s) found: " + additionalFound));
    }
    return wb;
}

From source file:it.smartcommunitylab.ungiorno.importer.Importer.java

License:Apache License

/**
 * @param wb//from ww w  . j a  v a 2  s.com
 * @param schoolProfile
 * @param teachers
 */
private void mapSchoolData(String appId, String schoolId, HSSFWorkbook wb) {
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        {
            List<Map<String, String>> result = getSheetMap(sheet);
            if (SHEET_PROFILO.equals(sheet.getSheetName())) {
                parseProfile(schoolProfile, result);
            }
            if (SHEET_ASSENZE.equals(sheet.getSheetName())) {
                List<TypeDef> types = parseTypes(result);
                schoolProfile.setAbsenceTypes(types);
            }
            if (SHEET_MALATTIE.equals(sheet.getSheetName())) {
                List<TypeDef> types = parseTypes(result);
                schoolProfile.setFrequentIllnesses(types);
            }
            if (SHEET_TIPOLOGIE_NOTE.equals(sheet.getSheetName())) {
                List<TypeDef> types = parseTypes(result);
                schoolProfile.setTeacherNoteTypes(types);
            }
            if (SHEET_CIBI.equals(sheet.getSheetName())) {
                List<TypeDef> types = parseTypes(result);
                schoolProfile.setFoodTypes(types);
            }
            if (SHEET_BUS.equals(sheet.getSheetName())) {
                schoolProfile.setBuses(parseBuses(result));
            }
            if (SHEET_SEZIONI.equals(sheet.getSheetName())) {
                schoolProfile.setSections(parseSections(result));
            }
            if (SHEET_INSEGNANTI.equals(sheet.getSheetName())) {
                parseTeachers(appId, schoolId, result);
            }
        }
    }
}

From source file:javaapplication2.Frame1.java

void fillData(File file) {
    int index = -1;
    HSSFWorkbook workbook = null;
    try {/* w ww .j av  a 2 s  .c  o  m*/
        try {
            FileInputStream inputStream = new FileInputStream(file);
            workbook = new HSSFWorkbook(inputStream);
        } catch (IOException ex) {
            Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, ex);
        }

        String[] strs = new String[workbook.getNumberOfSheets()];
        //get all sheet names from selected workbook
        for (int i = 0; i < strs.length; i++) {
            strs[i] = workbook.getSheetName(i);
        }
        JFrame frame = new JFrame("Input Dialog");

        String selectedsheet = (String) JOptionPane.showInputDialog(frame,
                "Which worksheet you want to import ?", "Select Worksheet", JOptionPane.QUESTION_MESSAGE, null,
                strs, strs[0]);

        if (selectedsheet != null) {
            for (int i = 0; i < strs.length; i++) {
                if (workbook.getSheetName(i).equalsIgnoreCase(selectedsheet))
                    index = i;
            }
            HSSFSheet sheet = workbook.getSheetAt(index);
            HSSFRow row = sheet.getRow(0);

            headers.clear();
            //int value=row.getLastCellNum();
            for (int i = 0; i < row.getLastCellNum(); i++) {
                HSSFCell cell1 = row.getCell(i);
                headers.add(cell1.toString());

            }

            data.clear();
            for (int j = 1; j < sheet.getLastRowNum() + 1; j++) {
                Vector d = new Vector();
                row = sheet.getRow(j);
                int noofrows = row.getLastCellNum();
                for (int i = 0; i < noofrows; i++) { //To handle empty excel cells 
                    HSSFCell cell = row.getCell(i, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK);
                    d.add(cell.toString());
                }
                d.add("\n");
                data.add(d);
            }
        } else {
            return;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:javaexcel.LeyendoExcel.java

private void leeFicheroExcel(String fileName) {
    List cellDataList = new ArrayList();
    try {/*from w  w w.j  a v  a 2  s. c o  m*/

        //            POIFSFileSystem: ciclo de vida completo del sistema de archivos.
        //            HSSFWorkbook: primer objeto construido.
        //            HSSFSheet: hojas de clculo.
        //            HSSFRow: fila de una hoja de clculo.
        //            HSSFCell: celda en una fila de la hoja de clculo.

        FileInputStream fileInputStream = new FileInputStream(fileName);
        POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
        HSSFWorkbook libro = new HSSFWorkbook(fsFileSystem);
        HSSFSheet hoja = libro.getSheetAt(0);

        Iterator rowIterator = hoja.rowIterator();
        while (rowIterator.hasNext()) {
            HSSFRow fila = (HSSFRow) rowIterator.next();
            Iterator iterator = fila.cellIterator();
            List cellTempList = new ArrayList();
            while (iterator.hasNext()) {
                HSSFCell hssfCell = (HSSFCell) iterator.next();
                cellTempList.add(hssfCell);
            }
            cellDataList.add(cellTempList);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:javafxapplication12.FXMLDocumentController.java

public void Search_to_file() throws FileNotFoundException, IOException {
    // File ?      ?

    Map staff = new HashMap<Integer, String>();
    ArrayList count_list = new ArrayList();
    for (int i = 0; i < temp_file_name.size(); i++) {
        InputStream in = new FileInputStream("Version/" + (String) temp_file_name.get(i));
        HSSFWorkbook wb = new HSSFWorkbook(in);
        Sheet s = wb.getSheetAt(0);
        int count = 0;
        boolean input_str = false;
        for (int j = 0; j <= s.getPhysicalNumberOfRows() - 1; j++) {
            String condition = wb.getSheetAt(0).getRow(j).getCell(1).getStringCellValue();
            String result = wb.getSheetAt(0).getRow(j).getCell(2).getStringCellValue();
            String date = wb.getSheetAt(0).getRow(j).getCell(3).getStringCellValue();
            String tester = wb.getSheetAt(0).getRow(j).getCell(4).getStringCellValue();
            ///??  ??  ?  ? Exel  
            if (tester.indexOf(Search_people.getText()) != -1) {
                System.out.println(" 1 ? ? 2");
                input_str = true;/* w  w  w. ja  v  a2  s.c  om*/
                count++;
            }
            if (date.indexOf(Search_people.getText()) != -1) {
                System.out.println(" 1 ? ? 2");
                input_str = true;
                count++;
            }
            if (result.indexOf(Search_people.getText()) != -1) {
                System.out.println(" 1 ? ? 2");
                input_str = true;
                count++;
            }
            if (condition.indexOf(Search_people.getText()) != -1) {
                System.out.println(" 1 ? ? 2");
                input_str = true;
                count++;
            }

            System.out.println("Count of " + count);
        }

        if (input_str) {
            System.out.println("input str work!!!");
            staff.put(count, (String) temp_file_name.get(i));
            count_list.add(count);
        }
        in.close();
    }

    TreeMap<Integer, String> sorted = new TreeMap<Integer, String>(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2.compareTo(o1);
        }
    });

    sorted.putAll(staff);
    ver_list.clear();
    File_name.clear();
    for (int i = 0; i < count_list.size(); i++) {
        String date = (String) sorted.get(count_list.get(i));
        System.out.println("Date:" + date);
        System.out.println("Date_sub:" + date.substring(0, 8) + "Time_sub:" + date.substring(9, 17));
        Version_file ch = new Version_file(i + 1, date.substring(0, 8), date.substring(9, 17));
        ver_list.add(ch);
        File_name.add(date);
    }

    //File_name.add(staff.get(25));
    //System.out.println(count_list.get(i));
    //;
    /*
    int i=0; 
    while(i != list.size()){ 
    Row row = sheet.createRow(i);
    row.createCell(0).setCellValue("asdasd");
    }*/
}

From source file:javafxapplication12.FXMLDocumentController.java

public void OpenFile() throws IOException {
    TablePosition pos = FileTable.getSelectionModel().getSelectedCells().get(0);
    int row_file = pos.getRow();
    System.out.println(File_name.get(row_file));
    add.setVisible(false);/*from   ww w. j  ava  2 s.c  o m*/
    delete.setVisible(false);
    save.setVisible(false);

    data.clear();

    InputStream in = new FileInputStream("Version/" + (String) File_name.get(row_file));
    HSSFWorkbook wb = new HSSFWorkbook(in);
    Sheet s = wb.getSheetAt(0);
    for (int i = 0; i <= s.getPhysicalNumberOfRows() - 1; i++) {
        String condition = wb.getSheetAt(0).getRow(i).getCell(1).getStringCellValue();
        String result = wb.getSheetAt(0).getRow(i).getCell(2).getStringCellValue();
        String date = wb.getSheetAt(0).getRow(i).getCell(3).getStringCellValue();
        String tester = wb.getSheetAt(0).getRow(i).getCell(4).getStringCellValue();

        Check ch = new Check(data.size() + 1, condition, result, date, tester);
        data.add(ch);
    }
}

From source file:jexcelcompi.JExcelCompi.java

static int celdaUnitaria(String nombre, int fila, int columna) {
    int valor = 0;
    try {// www  .ja  va 2 s  . com
        FileInputStream fis = new FileInputStream(new File(nombre));
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        HSSFSheet sheet = wb.getSheetAt(0);
        System.out.println("dato " + wb.getSheetAt(0).getRow(fila).getCell(columna));
        valor = (int) (double) (wb.getSheetAt(0).getRow(fila).getCell(columna).getNumericCellValue());
    } catch (IOException ex) {
        Logger.getLogger(JExcelCompi.class.getName()).log(Level.SEVERE, null, ex);
    }
    return valor;
}