Example usage for org.apache.poi.xssf.usermodel XSSFWorkbook getSheetAt

List of usage examples for org.apache.poi.xssf.usermodel XSSFWorkbook getSheetAt

Introduction

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

Prototype

@Override
public XSSFSheet getSheetAt(int index) 

Source Link

Document

Get the XSSFSheet object at the given index.

Usage

From source file:com.vsquaresystem.safedeals.marketprice.MarketPriceService.java

public Vector read() throws IOException {

    File excelFile = attachmentUtils.getDirectoryByAttachmentType(AttachmentUtils.AttachmentType.MARKET_PRICE);
    File[] listofFiles = excelFile.listFiles();
    String fileName = excelFile + "/" + listofFiles[0].getName();

    Vector cellVectorHolder = new Vector();
    int type;/*from  w w w .j  a v  a 2 s  . c  o  m*/
    try {
        FileInputStream myInput = new FileInputStream(fileName);

        XSSFWorkbook myWorkBook = new XSSFWorkbook(myInput);
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Iterator rowIter = mySheet.rowIterator();
        while (rowIter.hasNext()) {
            XSSFRow myRow = (XSSFRow) rowIter.next();
            Iterator cellIter = myRow.cellIterator();

            List list = new ArrayList();
            while (cellIter.hasNext()) {
                XSSFCell myCell = (XSSFCell) cellIter.next();
                if (myCell != null) {
                    switch (myCell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;
                    case Cell.CELL_TYPE_STRING:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;
                    case Cell.CELL_TYPE_BLANK:
                        break;
                    case Cell.CELL_TYPE_ERROR:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;

                    case Cell.CELL_TYPE_FORMULA:
                        break;
                    }
                }

            }
            logger.info("Line Line108 {}" + list);
            System.out.println("MAINlist" + list);
            cellVectorHolder.addElement(list);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return cellVectorHolder;

}

From source file:com.vsquaresystem.safedeals.rawmarketprice.RawMarketPriceService.java

public Vector read() throws IOException {

    File excelFile = attachmentUtils
            .getDirectoryByAttachmentType(AttachmentUtils.AttachmentType.RAW_MARKET_PRICE);
    File[] listofFiles = excelFile.listFiles();
    String fileName = excelFile + "/" + listofFiles[0].getName();

    Vector cellVectorHolder = new Vector();
    int type;//from   www  .  j  a v  a2  s  .  c  om
    try {
        FileInputStream myInput = new FileInputStream(fileName);

        XSSFWorkbook myWorkBook = new XSSFWorkbook(myInput);
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Iterator rowIter = mySheet.rowIterator();
        while (rowIter.hasNext()) {
            XSSFRow myRow = (XSSFRow) rowIter.next();
            Iterator cellIter = myRow.cellIterator();

            List list = new ArrayList();
            while (cellIter.hasNext()) {
                XSSFCell myCell = (XSSFCell) cellIter.next();
                if (myCell != null) {
                    switch (myCell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;
                    case Cell.CELL_TYPE_STRING:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;
                    case Cell.CELL_TYPE_BLANK:
                        break;
                    case Cell.CELL_TYPE_ERROR:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;

                    case Cell.CELL_TYPE_FORMULA:
                        break;
                    }
                }

            }

            cellVectorHolder.addElement(list);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cellVectorHolder;

}

From source file:com.vsquaresystem.safedeals.readyreckoner.ReadyReckonerService.java

public Vector read() throws IOException {

    File excelFile = attachmentUtils
            .getDirectoryByAttachmentType(AttachmentUtils.AttachmentType.READY_RECKONER);
    File[] listofFiles = excelFile.listFiles();
    String fileName = excelFile + "/" + listofFiles[0].getName();

    Vector cellVectorHolder = new Vector();
    int type;//  w  w w  . j ava2s  .c  o  m
    try {
        FileInputStream myInput = new FileInputStream(fileName);

        XSSFWorkbook myWorkBook = new XSSFWorkbook(myInput);
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Iterator rowIter = mySheet.rowIterator();
        while (rowIter.hasNext()) {
            XSSFRow myRow = (XSSFRow) rowIter.next();
            Iterator cellIter = myRow.cellIterator();

            List list = new ArrayList();
            while (cellIter.hasNext()) {
                XSSFCell myCell = (XSSFCell) cellIter.next();
                if (myCell != null) {
                    switch (myCell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;
                    case Cell.CELL_TYPE_STRING:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;
                    case Cell.CELL_TYPE_BLANK:
                        break;
                    case Cell.CELL_TYPE_ERROR:
                        System.out.println(new DataFormatter().formatCellValue(myCell));
                        list.add(new DataFormatter().formatCellValue(myCell));
                        break;

                    case Cell.CELL_TYPE_FORMULA:
                        break;
                    }
                }

            }
            cellVectorHolder.addElement(list);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return cellVectorHolder;

}

From source file:com.xl.main.ReadExcelSampleSilk.java

public static String read(String filename) {
    Gson gson = new Gson();
    Map<String, List<SampleSinkBean>> values = new HashMap<String, List<SampleSinkBean>>();
    List<SampleSinkBean> byRow = new ArrayList<SampleSinkBean>();
    try {//from   www . j  a va  2s  .  co  m

        FileInputStream file = null;
        if (filename == null) {
            file = new FileInputStream(new File("H:\\anil\\sample-sink.xlsx"));
        } else {
            file = new FileInputStream(new File(filename));
        }

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        //Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);

        //Iterate through each rows one by one
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            if (row.getRowNum() > 0 && row.getRowNum() < 20) {
                SampleSinkBean sb = new SampleSinkBean();
                //System.out.println("row value" + sheet.getRow(3).getCell(3));
                while (cellIterator.hasNext()) {//
                    Cell cell = cellIterator.next();

                    String cellString = " ";
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_NUMERIC:
                        cellString = cell.getNumericCellValue() + "";
                        break;
                    case Cell.CELL_TYPE_FORMULA:
                        cellString = cell.getStringCellValue() + "";

                        break;
                    case Cell.CELL_TYPE_ERROR:
                        cellString = cell.getErrorCellValue() + "";
                        break;
                    default:
                        cellString = cell.getStringCellValue() + "";

                    }

                    switch (cell.getColumnIndex()) {
                    case 0:
                        sb.setFrYear(cellString);
                        break;
                    case 1:
                        sb.setVpmod(cellString);
                    case 2:
                        sb.setProjectName(cellString);
                    case 3:
                        sb.setProjectWorktype(cellString);
                    case 4:
                        sb.setBusinessObjective(cellString);

                    }

                }
                byRow.add(sb);

            }
            // System.out.println("");

        }
        values.put("sink", byRow);
        System.out.println("output *********" + gson.toJson(values));

        file.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return gson.toJson(values);
}

From source file:com.yanglb.utilitys.codegen.core.reader.BaseReader.java

License:Apache License

/**
 * ?/*from ww  w .j  a  v a  2 s.com*/
 * @throws CodeGenException 
 */
private void doReader() throws CodeGenException {
    // 
    XSSFWorkbook wb = null;
    try {
        // jar??new File
        if (this.excelFile.startsWith("jar:")) {
            String path = this.excelFile.substring(4);
            InputStream is = this.getClass().getResourceAsStream(path);
            wb = new XSSFWorkbook(is);
        } else {
            File file = new File(this.excelFile);
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
            wb = new XSSFWorkbook(in);
        }

        // ?
        HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);

        // ??
        if (this.sheets == null || this.sheets.length == 0) {
            // ?
            for (int i = 0; i < wb.getNumberOfSheets(); i++) {
                XSSFSheet sheet = wb.getSheetAt(i);

                // ???Sheet
                if (!this.isReadable(sheet.getSheetName())) {
                    continue;
                }
                this.results.add(this.onReader(sheet));
            }
        } else {
            // ?Sheet
            for (String sheetName : this.sheets) {
                XSSFSheet sheet = wb.getSheet(sheetName);
                if (sheet == null) {
                    throw new CodeGenException(String.format(MsgUtility.getString("E_004"), sheetName));
                }
                this.results.add(this.onReader(sheet));
            }
        }
    } catch (FileNotFoundException e) {
        // ???
        throw new CodeGenException(e.getMessage());
    } catch (UnImplementException e) {
        this.results.clear();
        e.printStackTrace();
    } catch (IOException e) {
        throw new CodeGenException(MsgUtility.getString("E_005"));
    } finally {
        try {
            if (wb != null)
                wb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.yyl.common.utils.excel.ExcelTools.java

private static List<List<Object>> readXLSXWithHeader(InputStream inputStream) throws IOException {
    //        InputStream is = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(inputStream);
    XSSFSheet sheet = wb.getSheetAt(0);

    List<List<Object>> o = new ArrayList();
    List<Object> temp = null;
    if (sheet == null) {
        return null;
    }//ww w.  j  a v  a2 s  . c o m

    for (int row_index = 0; row_index <= sheet.getLastRowNum(); row_index++) {
        XSSFRow row = sheet.getRow(row_index);
        if (row == null) {
            continue;
        }
        temp = new ArrayList();
        for (int col_index = 0; col_index <= row.getLastCellNum(); col_index++) {
            temp.add(getCellValue(row.getCell(col_index)));
        }
        o.add(temp);
    }

    return o;
}

From source file:com.yyl.common.utils.excel.ExcelTools.java

private static List<List<Object>> readXLSX(InputStream inputStream) throws IOException {
    //        InputStream is = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(inputStream);
    XSSFSheet sheet = wb.getSheetAt(0);

    List<List<Object>> o = new ArrayList();
    List<Object> temp = null;
    if (sheet == null) {
        return null;
    }/*  w w  w  .ja v a  2  s .co  m*/

    for (int row_index = 1; row_index <= sheet.getLastRowNum(); row_index++) {
        XSSFRow row = sheet.getRow(row_index);
        if (row == null) {
            continue;
        }
        temp = new ArrayList();
        for (int col_index = 0; col_index <= row.getLastCellNum(); col_index++) {
            temp.add(getCellValue(row.getCell(col_index)));
        }
        o.add(temp);
    }

    return o;
}

From source file:com.yyl.common.utils.excel.ExcelTools.java

private static List<Map<String, String>> readXLSX(InputStream inputStream, Map<String, Integer> keyMaps)
        throws IOException {
    //        InputStream is = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(inputStream);

    List<Map<String, String>> list = new ArrayList();
    Map<String, String> temp = null;
    for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
        XSSFSheet sheet = wb.getSheetAt(sheetIndex);
        if (sheet == null) {
            continue;
        }//from  ww w  .  jav  a  2s  .c o  m
        for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
            XSSFRow row = sheet.getRow(rowIndex);
            temp = new HashMap();
            if (row != null) {
                for (Entry<String, Integer> entry : keyMaps.entrySet()) {
                    int index = entry.getValue();
                    XSSFCell cell = row.getCell(index);
                    temp.put(entry.getKey(), getCellValue(cell));
                }
            }
            list.add(temp);
        }
    }
    return list;
}

From source file:comparararchivos.CompararArchivos.java

/**
 * @param args the command line arguments
 *///from   w  w w . j  a  v a 2s  . c o  m
public static void main(String[] args) {
    // TODO code application logic here
    File excel1 = null;
    FileInputStream fl1 = null;
    XSSFWorkbook book1 = null;

    File excel2 = null;
    FileInputStream fl2 = null;
    XSSFWorkbook book2 = null;

    try {
        excel1 = new File("D:\\Users\\jose.gil\\Documents\\Anotaciones\\ProfesorB.xlsx");
        excel2 = new File("D:\\Users\\jose.gil\\Documents\\Anotaciones\\ProfesorD.xlsx");

        PrintWriter file = new PrintWriter("diferencias.txt", "UTF-8");

        fl1 = new FileInputStream(excel1);
        fl2 = new FileInputStream(excel2);

        book1 = new XSSFWorkbook(fl1);
        book2 = new XSSFWorkbook(fl2);

        XSSFSheet sheet_A = book1.getSheetAt(0);
        XSSFSheet sheet_B = book2.getSheetAt(0);

        Iterator<Row> itrA = sheet_A.iterator();
        Iterator<Row> itrB = sheet_B.iterator();

        int totalDiferencias = 0;
        int numFila = 2;
        while (itrA.hasNext() && itrB.hasNext()) {
            Row rowA = itrA.next();
            Row rowB = itrB.next();
            if (rowA.getRowNum() == 0)
                continue;

            Iterator<Cell> cellitA = rowA.cellIterator();
            Iterator<Cell> cellitB = rowB.cellIterator();

            Cell celA = cellitA.next();
            Cell celB = cellitB.next();

            //Se esta en las celdas del numero de Aviso
            celA = cellitA.next();
            celB = cellitB.next();

            int numAvisoA = (int) celA.getNumericCellValue();
            int numAvisoB = (int) celB.getNumericCellValue();

            if (numAvisoA != numAvisoB) {
                System.out.println("Numero de Aviso: " + numAvisoA);
                continue;
            }

            //Se esta en las celdas de la categoria
            celA = cellitA.next();
            celB = cellitB.next();

            String textA = celA.getStringCellValue();
            //System.out.println("Categoria A: "+textA);
            String textB = celB.getStringCellValue();
            //System.out.println("Categoria B: "+textB);

            if (!textA.equals(textB)) {
                System.out.println("Fila: " + numFila + " Numero de Aviso: " + numAvisoA + " Texto B: " + textA
                        + " - Texto D: " + textB);
                file.println("Fila: " + numFila + " Numero de Aviso: " + numAvisoA + " Texto B: " + textA
                        + " - Texto D: " + textB);
                totalDiferencias++;
            }

            numFila++;
        }

        System.out.println("\nTotal diferencias: " + totalDiferencias);
        file.println("\nTotal diferencias: " + totalDiferencias);

        file.close();

    } catch (FileNotFoundException fe) {
        fe.printStackTrace();
    } catch (IOException ie) {
        ie.printStackTrace();
    }

}

From source file:comparator.Comparator.java

public static void transcoding_Map_HUG() throws IOException {
    //Get the input files
    FileInputStream mvcFile = new FileInputStream(new File(
            "\\\\hes-nas-drize.hes.adhes.hesge.ch\\home\\luc.mottin\\Documents\\Expand\\MVC2.0\\Informal_epSOS-MVC_V2_0_(DRAFT)_03.xlsx"));
    //Get the workbook instance for XLS file 
    XSSFWorkbook mvcWorkbook = new XSSFWorkbook(mvcFile);
    XSSFSheet mvcSheet;// ww w  .  ja  v  a 2  s  . c om
    Iterator<Row> mvcRowIterator;
    String mvcSheetName;
    int mvcCol;
    boolean mvcColFound;
    Row mvcRow;
    Row mvcRow2;
    Iterator<Cell> mvcCellIterator;
    boolean statusOK = false;

    //OUTPUT
    String code_src;
    String code_dest;
    String name_dest = "";
    String value_set_name_dest = "";
    String status = "none";
    String value_set_name_source = "";
    String value_set_oid_dest = "";
    String parent_system_code_dest = "";
    String parent_system_oid_dest = "";
    String comment = "";
    String map_level = "0";
    String review = "0";
    String version = "";

    //Prepare the output file
    Writer csvW = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
            "\\\\hes-nas-drize.hes.adhes.hesge.ch\\home\\luc.mottin\\Documents\\Expand\\Map_HUG\\map_hug_to_mvc_2.0.csv"),
            "UTF-8"));
    csvW.write('\ufeff');
    csvW.write(
            "code_src;code_dest;name_dest;value_set_name_dest;status;value_set_name_source;value_set_oid_dest;parent_system_code_dest;parent_system_oid_dest;comment;map_level;review;version;");
    csvW.write("\n");

    //Read csv map
    String map = "\\\\hes-nas-drize.hes.adhes.hesge.ch\\home\\luc.mottin\\Documents\\Expand\\Map_HUG\\map_hug_to_mvc_1_9.csv";
    try {
        BufferedReader br = new BufferedReader(new FileReader(map));
        String line = "";
        String csvSplitBy = ";";
        String[] maLigne;

        //jump over the first line
        br.readLine();
        //pour chaque ligne de la map
        while ((line = br.readLine()) != null) {
            statusOK = false;

            maLigne = line.split(csvSplitBy);
            code_src = maLigne[0];
            code_dest = maLigne[1];

            //Get the sheet from the MTC workbook
            for (int i = 0; i < mvcWorkbook.getNumberOfSheets(); i++) {
                mvcSheet = mvcWorkbook.getSheetAt(i);

                //Get iterator to all the rows in current MTC sheet
                mvcRowIterator = mvcSheet.iterator();

                //Get the name of MTTC sheet, compare them MAP entries
                //MVC data files are called "epSOSsheetName"
                mvcSheetName = mvcSheet.getSheetName();

                //And process the file matching to find the good sheet
                if (mvcSheetName.equals(maLigne[3])) {
                    value_set_name_dest = mvcSheetName;
                    value_set_name_source = maLigne[5];

                    mvcCol = 0;
                    mvcColFound = false;

                    while (mvcRowIterator.hasNext()) {
                        mvcRow = mvcRowIterator.next();
                        mvcRow2 = mvcRow;

                        if (mvcColFound == false) {
                            mvcCellIterator = mvcRow.cellIterator();

                            while (mvcCellIterator.hasNext()) {
                                Cell mvcCell = mvcCellIterator.next();

                                if (mvcCell.getCellType() == 1
                                        && (mvcCell.getStringCellValue().equals("Parent Code System:"))) {
                                    mvcCol = mvcCell.getColumnIndex() + 1;
                                    mvcRow.getCell(mvcCol, Row.CREATE_NULL_AS_BLANK)
                                            .setCellType(Cell.CELL_TYPE_STRING);
                                    parent_system_code_dest = mvcRow.getCell(mvcCol).getStringCellValue()
                                            .trim();
                                }
                                if (mvcCell.getCellType() == 1
                                        && (mvcCell.getStringCellValue().equals("OID Parent Code System:"))) {
                                    mvcCol = mvcCell.getColumnIndex() + 1;
                                    mvcRow.getCell(mvcCol, Row.CREATE_NULL_AS_BLANK)
                                            .setCellType(Cell.CELL_TYPE_STRING);
                                    parent_system_oid_dest = mvcRow.getCell(mvcCol).getStringCellValue().trim();
                                }
                                if (mvcCell.getCellType() == 1
                                        && (mvcCell.getStringCellValue().equals("epSOS OID:"))) {
                                    mvcCol = mvcCell.getColumnIndex() + 1;
                                    mvcRow.getCell(mvcCol, Row.CREATE_NULL_AS_BLANK)
                                            .setCellType(Cell.CELL_TYPE_STRING);
                                    value_set_oid_dest = mvcRow.getCell(mvcCol).getStringCellValue().trim();
                                }
                                if (mvcCell.getCellType() == 1
                                        && (mvcCell.getStringCellValue().equals("version:"))) {
                                    mvcCol = mvcCell.getColumnIndex() + 1;
                                    mvcRow.getCell(mvcCol, Row.CREATE_NULL_AS_BLANK)
                                            .setCellType(Cell.CELL_TYPE_STRING);
                                    version = mvcRow.getCell(mvcCol).getStringCellValue().trim();
                                }

                                if (mvcCell.getCellType() == 1
                                        && (mvcCell.getStringCellValue().equals("epSOS Code")
                                                || mvcCell.getStringCellValue().equals("Code"))) {
                                    mvcCol = mvcCell.getColumnIndex();
                                    mvcColFound = true;
                                    break;
                                }
                            }
                        } else {
                            mvcRow.getCell(mvcCol, Row.CREATE_NULL_AS_BLANK).setCellType(Cell.CELL_TYPE_STRING);
                            if (mvcRow.getCell(mvcCol).getStringCellValue().trim().equals(code_dest)) {
                                statusOK = true;
                                mvcRow2.getCell(mvcCol + 1, Row.CREATE_NULL_AS_BLANK)
                                        .setCellType(Cell.CELL_TYPE_STRING);
                                name_dest = mvcRow2.getCell(mvcCol + 1).getStringCellValue().trim();
                                break;
                            }

                        }
                    }
                    if (statusOK == true) {
                        break;
                    } else {
                        parent_system_code_dest = "";
                        parent_system_oid_dest = "";
                        value_set_oid_dest = "";
                        version = "";
                    }
                }
            }

            if (statusOK != true) {
                //TO CHECK MANUALY
                status = "manual";
                name_dest = maLigne[2];
                comment = "mvc2.0 no hug code";
            }

            //Write the mapping
            csvW.write(code_src + ";" + code_dest + ";" + name_dest + ";" + value_set_name_dest + ";" + status
                    + ";" + value_set_name_source + ";" + value_set_oid_dest + ";" + parent_system_code_dest
                    + ";" + parent_system_oid_dest + ";" + comment + ";" + map_level + ";" + review + ";"
                    + version + ";");
            csvW.write("\n");
            //reset status
            status = "none";
            comment = "";

        }

        br.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    csvW.flush();
    csvW.close();

}