Example usage for org.apache.poi.ss.usermodel Workbook getSheet

List of usage examples for org.apache.poi.ss.usermodel Workbook getSheet

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Workbook getSheet.

Prototype

Sheet getSheet(String name);

Source Link

Document

Get sheet with the given name

Usage

From source file:simbad.Excel.java

public double mayorLongitud(Workbook workbook) throws FileNotFoundException, IOException {

    Sheet sheet = workbook.getSheet("Sheet1");

    double mayorLon = 0;
    Row row = sheet.getRow(0);/*from  w w  w .  ja v  a  2s. c  o m*/

    for (int j = 0; j < row.getLastCellNum(); j++) {
        if (row.getCell(j).getRichStringCellValue().toString().equals("lon_ciu"))
            for (int i = 1; i < sheet.getLastRowNum(); i++) {
                Row row2 = sheet.getRow(i);
                if (i == 1
                        || Double.parseDouble((row2.getCell(j).getRichStringCellValue().toString())) > mayorLon)
                    mayorLon = Double.parseDouble(row2.getCell(j).getRichStringCellValue().toString());
            }
    }
    return mayorLon;
}

From source file:simbad.Excel.java

public double menorLongitud(Workbook workbook) throws FileNotFoundException, IOException {

    Sheet sheet = workbook.getSheet("Sheet1");
    double menorLon = 0;
    Row row = sheet.getRow(0);/*  w ww .j av a 2 s. com*/

    for (int j = 0; j < row.getLastCellNum(); j++) {
        if (row.getCell(j).getRichStringCellValue().toString().equals("lon_ciu"))
            for (int i = 1; i < sheet.getLastRowNum(); i++) {
                Row row2 = sheet.getRow(i);
                if (i == 1
                        || Double.parseDouble((row2.getCell(j).getRichStringCellValue().toString())) < menorLon)
                    menorLon = Double.parseDouble(row2.getCell(j).getRichStringCellValue().toString());
            }
    }
    return menorLon;

}

From source file:simbad.reporteUnificado.java

private double mayorLatitud() throws FileNotFoundException, IOException {
    Workbook workbook = new XSSFWorkbook(new FileInputStream("D:\\ejemplo_datos.xlsx"));
    Sheet sheet = workbook.getSheet("Sheet1");
    double mayorLat = 0;
    Row row = sheet.getRow(0);/*  w  w  w. j ava2  s  .c  o  m*/

    for (int j = 0; j < row.getLastCellNum(); j++) {
        if (row.getCell(j).getRichStringCellValue().toString().equals("lat_ciu"))
            for (int i = 1; i < sheet.getLastRowNum(); i++) {
                Row row2 = sheet.getRow(i);
                if (i == 1
                        || Double.parseDouble((row2.getCell(j).getRichStringCellValue().toString())) > mayorLat)
                    mayorLat = Double.parseDouble(row2.getCell(j).getRichStringCellValue().toString());
            }
    }
    return mayorLat;

}

From source file:simbad.reporteUnificado.java

private double menorLatitud() throws FileNotFoundException, IOException {
    Workbook workbook = new XSSFWorkbook(new FileInputStream("D:\\ejemplo_datos.xlsx"));
    Sheet sheet = workbook.getSheet("Sheet1");
    double menorLat = 0;
    Row row = sheet.getRow(0);//from  w  w w  . j  a  v  a2  s.  c om

    for (int j = 0; j < row.getLastCellNum(); j++) {
        if (row.getCell(j).getRichStringCellValue().toString().equals("lat_ciu"))
            for (int i = 1; i < sheet.getLastRowNum(); i++) {
                Row row2 = sheet.getRow(i);
                if (i == 1
                        || Double.parseDouble((row2.getCell(j).getRichStringCellValue().toString())) < menorLat)
                    menorLat = Double.parseDouble(row2.getCell(j).getRichStringCellValue().toString());
            }
    }

    return menorLat;
}

From source file:simbad.reporteUnificado.java

private double mayorLongitud() throws FileNotFoundException, IOException {
    Workbook workbook = new XSSFWorkbook(new FileInputStream("D:\\ejemplo_datos.xlsx"));
    Sheet sheet = workbook.getSheet("Sheet1");

    double mayorLon = 0;
    Row row = sheet.getRow(0);//  w w w  .jav a2  s.  co m

    for (int j = 0; j < row.getLastCellNum(); j++) {
        if (row.getCell(j).getRichStringCellValue().toString().equals("lon_ciu"))
            for (int i = 1; i < sheet.getLastRowNum(); i++) {
                Row row2 = sheet.getRow(i);
                if (i == 1
                        || Double.parseDouble((row2.getCell(j).getRichStringCellValue().toString())) > mayorLon)
                    mayorLon = Double.parseDouble(row2.getCell(j).getRichStringCellValue().toString());
            }
    }
    return mayorLon;
}

From source file:simbad.reporteUnificado.java

private double menorLongitud() throws FileNotFoundException, IOException {
    Workbook workbook = new XSSFWorkbook(new FileInputStream("D:\\ejemplo_datos.xlsx"));
    Sheet sheet = workbook.getSheet("Sheet1");
    double menorLon = 0;
    Row row = sheet.getRow(0);//from   ww w  .  j a  va  2 s  . co m
    mayorLatitud(workbook);
    for (int j = 0; j < row.getLastCellNum(); j++) {
        if (row.getCell(j).getRichStringCellValue().toString().equals("lon_ciu"))
            for (int i = 1; i < sheet.getLastRowNum(); i++) {
                Row row2 = sheet.getRow(i);
                if (i == 1
                        || Double.parseDouble((row2.getCell(j).getRichStringCellValue().toString())) < menorLon)
                    menorLon = Double.parseDouble(row2.getCell(j).getRichStringCellValue().toString());
            }
    }
    return menorLon;

}

From source file:simbad.reporteUnificado.java

public void ObtenerReporte() throws IOException {

    //Se alimenta de los datos proporcionados por metodos privados que interactuan con excel y genera el reporte unificado
    Workbook workbook = new XSSFWorkbook(new FileInputStream("D:\\ejemplo_datos.xlsx"));
    Sheet sheet = workbook.getSheet("Sheet1");

}

From source file:simbad.reporteUnificado.java

public void MostrarReporte() throws IOException {
    Workbook workbook = new XSSFWorkbook(new FileInputStream(
            "C:\\Users\\Miguel\\My Documents\\NetBeansprojects\\SIMBAD\\reporteUnificado.xlsx"));
    Sheet sheet = workbook.getSheet("Sheet1");
    for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
        Row row = rit.next();//from  w  ww  . j  a  v a2s .c  o m
        for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
            Cell cell = cit.next();
            cell.setCellType(Cell.CELL_TYPE_STRING);
            System.out.print(cell.getStringCellValue() + "\t");
        }
        System.out.println();

    }

}

From source file:species.formatReader.SpreadsheetWriter.java

public static void writeHeadersInFormat(Workbook wb, JSONElement headerMarkers1, JSONArray orderedArray) {
    JSONObject headerMarkers = (JSONObject) headerMarkers1;
    //System.out.println("=CLASS===="+headerMarkers.getClass());
    Object o = headerMarkers.remove("undefined");
    Sheet sheet = wb.getSheet("headerMetadata");
    if (sheet != null) {
        int sindex = wb.getSheetIndex(sheet);
        wb.removeSheetAt(sindex);//from ww  w .  j a va 2s . c o m
    }
    sheet = wb.createSheet("headerMetadata");
    Map<String, Map<String, String>> reverseMarkers = new HashMap<String, Map<String, String>>();
    int rownum = 0;

    for (Object entry1 : headerMarkers.entrySet()) {
        Entry<String, Map<String, String>> entry = (Entry<String, Map<String, String>>) entry1;
        String headerName = entry.getKey();
        headerName = headerName.trim().toLowerCase();
        Map<String, String> headerValues = new HashMap<String, String>();
        for (Map.Entry<String, String> en : entry.getValue().entrySet()) {
            //System.out.println("=======HERE======" + en.getKey());
            if (en.getKey() != "undefined") {
                String val = (en.getValue() instanceof String ? en.getValue() : null);
                headerValues.put(en.getKey(), val);
            }
        }
        //System.out.println("==========NEW FUNC==============");
        //System.out.println(headerName);
        //System.out.println("---------" + entry.getValue() + entry.getValue().getClass());
        System.out.println("-------==============" + headerValues + headerValues.getClass());
        String dataColumns = "";
        if (headerValues.get("dataColumns") != null) {
            dataColumns = headerValues.get("dataColumns");
            dataColumns = dataColumns.trim();
        }
        String group = "";
        if (headerValues.get("group") != null) {
            //System.out.println(headerValues.get("group"));
            group = headerValues.get("group");
            group = group.trim();
        }

        //System.out.println("====ERROR FINIDING==" + headerValues.get("group"));
        String includeHeadings = "";
        if (headerValues.get("header") != null) {
            includeHeadings = headerValues.get("header");
            includeHeadings = includeHeadings.trim();
        }

        String append = "";
        if (headerValues.get("append") != null) {
            append = headerValues.get("append");
            append = append.trim();
        }

        String delimiter = "";
        if (headerValues.get("delimiter") != null) {
            delimiter = headerValues.get("delimiter");
            delimiter = delimiter.trim();
        }

        String images = "";
        if (headerValues.get("images") != null) {
            images = headerValues.get("images");
            images = images.trim();
        }

        String contributor = "";
        if (headerValues.get("contributor") != null) {
            contributor = headerValues.get("contributor");
            contributor = contributor.trim();
        }

        String attributions = "";
        if (headerValues.get("attributions") != null) {
            attributions = headerValues.get("attributions");
            attributions = attributions.trim();
        }

        String references = "";
        if (headerValues.get("references") != null) {
            references = headerValues.get("references");
            references = references.trim();
        }

        String license = "";
        if (headerValues.get("license") != null) {
            license = headerValues.get("license");
            license = license.trim();
        }

        String audience = "";
        if (headerValues.get("audience") != null) {
            audience = headerValues.get("audience");
            audience = audience.trim();
        }

        String language = "";
        if (headerValues.get("language") != null) {
            language = headerValues.get("language");
            language = language.trim();
        }

        //System.out.println("=======" + dataColumns);
        List<String> dcList = Arrays.asList(dataColumns.split(","));
        Iterator<String> dcIterator = dcList.iterator();
        while (dcIterator.hasNext()) {
            String nextVal = dcIterator.next();
            nextVal = nextVal.trim();
            //System.out.println("==THIS IS THE VALUE=== " + nextVal);
            if (!nextVal.equals("") && !nextVal.equals(null)) {
                if (reverseMarkers.containsKey(nextVal)) {
                    Map<String, String> m = reverseMarkers.get(nextVal);
                    String fieldNames = m.get("fieldNames");
                    if (fieldNames != "") {
                        fieldNames += FIELD_SEP + headerName;
                        m.put("fieldNames", fieldNames);
                    } else {
                        m.put("fieldNames", headerName);
                    }
                    String contentDelimiter = m.get("contentDelimiter");
                    if (contentDelimiter != "") {
                        contentDelimiter += COLUMN_SEP + headerName + KEYVALUE_SEP + delimiter;
                        m.put("contentDelimiter", contentDelimiter);
                    } else {
                        m.put("contentDelimiter", headerName + KEYVALUE_SEP + delimiter);
                    }
                    String contentFormat = m.get("contentFormat");
                    if (contentFormat != "") {
                        contentFormat += COLUMN_SEP + headerName + KEYVALUE_SEP + "Group=" + group + ";"
                                + "includeheadings=" + includeHeadings + ";" + "append=" + append + ";";
                        m.put("contentFormat", contentFormat);
                    } else {
                        m.put("contentFormat", headerName + KEYVALUE_SEP + "Group=" + group + ";"
                                + "includeheadings=" + includeHeadings + ";" + "append=" + append + ";");
                    }
                    String imagesCol = m.get("images");
                    if (imagesCol != "") {
                        imagesCol += COLUMN_SEP + headerName + KEYVALUE_SEP + images;
                        m.put("images", imagesCol);
                    } else {
                        m.put("images", headerName + KEYVALUE_SEP + images);
                    }
                    String contributorCol = m.get("contributor");
                    if (contributorCol != "") {
                        contributorCol += COLUMN_SEP + headerName + KEYVALUE_SEP + contributor;
                        m.put("contributor", contributorCol);
                    } else {
                        m.put("contributor", headerName + KEYVALUE_SEP + contributor);
                    }
                    String attributionsCol = m.get("attributions");
                    if (attributionsCol != "") {
                        attributionsCol += COLUMN_SEP + headerName + KEYVALUE_SEP + attributions;
                        m.put("attributions", attributionsCol);
                    } else {
                        m.put("attributions", headerName + KEYVALUE_SEP + attributions);
                    }
                    String referencesCol = m.get("references");
                    if (referencesCol != "") {
                        referencesCol += COLUMN_SEP + headerName + KEYVALUE_SEP + references;
                        m.put("references", referencesCol);
                    } else {
                        m.put("references", headerName + KEYVALUE_SEP + references);
                    }

                    String licenseCol = m.get("license");
                    if (licenseCol != "") {
                        licenseCol += COLUMN_SEP + headerName + KEYVALUE_SEP + license;
                        m.put("license", licenseCol);
                    } else {
                        m.put("license", headerName + KEYVALUE_SEP + license);
                    }
                    String audienceCol = m.get("audience");
                    if (audienceCol != "") {
                        audienceCol += COLUMN_SEP + headerName + KEYVALUE_SEP + audience;
                        m.put("audience", audienceCol);
                    } else {
                        m.put("audience", headerName + KEYVALUE_SEP + audience);
                    }

                    String languageCol = m.get("language");
                    if (languageCol != "") {
                        languageCol = language;
                        m.put("language", languageCol);
                    } else {
                        m.put("language", language);
                    }

                } else {
                    Map<String, String> m1 = new HashMap();
                    m1.put("fieldNames", headerName);
                    m1.put("contentDelimiter", headerName + KEYVALUE_SEP + delimiter);
                    m1.put("contentFormat", headerName + KEYVALUE_SEP + "Group=" + group + ";"
                            + "includeheadings=" + includeHeadings + ";" + "append=" + append + ";");
                    m1.put("images", headerName + KEYVALUE_SEP + images);
                    m1.put("contributor", headerName + KEYVALUE_SEP + contributor);
                    m1.put("attributions", headerName + KEYVALUE_SEP + attributions);
                    m1.put("references", headerName + KEYVALUE_SEP + references);
                    m1.put("license", headerName + KEYVALUE_SEP + license);
                    m1.put("audience", headerName + KEYVALUE_SEP + audience);
                    m1.put("language", language);

                    reverseMarkers.put(nextVal, m1);
                }
            }
        }

    }

    Row row = sheet.createRow(rownum++);

    String[] headerRowValues = { "CONCEPT", "CATEGORY", "SUBCATEGORY", "FIELD NAME(S)", "CONTENT DELIMITER",
            "CONTENT FORMAT", "IMAGES", "CONTRIBUTOR", "ATTRIBUTIONS", "REFERENCES", "LICENSE", "AUDIENCE",
            "LANGUAGE" };
    int numOfColumns = headerRowValues.length;
    for (int cellNum = 0; cellNum < numOfColumns; cellNum++) {
        Cell cell = row.createCell(cellNum);
        cell.setCellValue(headerRowValues[cellNum]);
    }
    //System.out.println("====REVERSE MARKERS=====" + reverseMarkers);
    for (Map.Entry<String, Map<String, String>> entry : reverseMarkers.entrySet()) {
        String[] arr = new String[numOfColumns];
        String headerName = entry.getKey();
        List<String> pipedNameList = Arrays.asList(headerName.split("\\|"));
        Iterator<String> pnlIterator = pipedNameList.iterator();
        for (int i = 0; i < 3; i++) {
            if (pnlIterator.hasNext()) {
                arr[i] = pnlIterator.next();
            } else {
                arr[i] = "";
            }
        }
        Map<String, String> m2 = new HashMap(entry.getValue());
        arr[3] = m2.get("fieldNames");
        arr[4] = m2.get("contentDelimiter");
        arr[5] = m2.get("contentFormat");
        arr[6] = m2.get("images");
        arr[7] = m2.get("contributor");
        arr[8] = m2.get("attributions");
        arr[9] = m2.get("references");
        arr[10] = m2.get("license");
        arr[11] = m2.get("audience");
        arr[12] = m2.get("language");

        //Rewriting fieldnames according to columns in sheet 1

        String[] tokens = arr[3].split(FIELD_SEP);
        String finalKeysOrdered = "";
        int orderedArraySize = orderedArray.length();
        for (int k = 0; k < orderedArraySize; k++) {
            String kkk = orderedArray.getString(k);
            for (String token : tokens) {
                if (token.equals(kkk)) {
                    if (finalKeysOrdered == "") {
                        finalKeysOrdered = token;
                    } else {
                        finalKeysOrdered = finalKeysOrdered + FIELD_SEP + token;
                    }
                }
            }
        }
        arr[3] = finalKeysOrdered;
        /////////

        row = sheet.createRow(rownum++);
        for (int cellNum = 0; cellNum < numOfColumns; cellNum++) {
            Cell cell = row.createCell(cellNum);
            cell.setCellValue(arr[cellNum]);
        }

    }

}

From source file:species.formatReader.SpreadsheetWriter.java

public static void writeHeaderMarkersInSheet(Workbook wb, Map<String, Map<String, String>> headerMarkers) {
    Sheet sheet = wb.getSheet("headerMetadata");
    if (sheet != null) {
        int sindex = wb.getSheetIndex(sheet);
        wb.removeSheetAt(sindex);/*  w w w. j  a va 2s  .  com*/
    }
    sheet = wb.createSheet("headerMetadata");
    int rownum = 0;
    String headerName;
    //int headerMarkersSize = 0;
    /*
       for(int i = 0; i<headerMarkersSize; i++){
       JSONObject marker = headerMarkers.get(i);
            
       }*/

    Row row = sheet.createRow(rownum++);
    for (int cellNum = 0; cellNum <= 2; cellNum++) {
        Cell cell = row.createCell(cellNum);
        if (cellNum == 0) {
            //System.out.println("===HEADER NAME IN MARKER SHEET==== " );

            cell.setCellValue("column_name");
        } else if (cellNum == 1) {
            //System.out.println("===TAGS IN MARKER SHEET==== " );
            cell.setCellValue("type");
        } else if (cellNum == 2) {
            //System.out.println("===VALUES IN MARKER SHEET==== " );
            cell.setCellValue("value");
        }
    }
    for (Map.Entry<String, Map<String, String>> entry : headerMarkers.entrySet()) {
        headerName = entry.getKey();
        //System.out.println("===HEADER NAME IN MARKER SHEET==== " + headerName );
        for (Map.Entry<String, String> en : entry.getValue().entrySet()) {
            row = sheet.createRow(rownum++);
            for (int cellNum = 0; cellNum <= 2; cellNum++) {
                Cell cell = row.createCell(cellNum);
                if (cellNum == 0) {
                    //System.out.println("===HEADER NAME IN MARKER SHEET==== " + headerName );

                    cell.setCellValue(headerName);
                } else if (cellNum == 1) {
                    //System.out.println("===TAGS IN MARKER SHEET==== " + en.getKey());
                    cell.setCellValue(en.getKey());
                } else if (cellNum == 2) {
                    //System.out.println("===VALUES IN MARKER SHEET==== " + en.getValue());
                    cell.setCellValue(en.getValue());
                }
            }

        }
    }

    return;
}