Example usage for org.apache.poi.ss.usermodel Cell getNumericCellValue

List of usage examples for org.apache.poi.ss.usermodel Cell getNumericCellValue

Introduction

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

Prototype

double getNumericCellValue();

Source Link

Document

Get the value of the cell as a number.

Usage

From source file:fenix.util.convert.InvoiceConverter.java

License:Open Source License

/**
 *   ?./*from  w  w  w. ja  v  a  2s  .c  om*/
 * @param cell - ?, ?? ? .
 * @return -  ? .
 * @throws UnsupportedEncodingException 
 */
private static String validateString(Cell cell) throws UnsupportedEncodingException {
    if (cell == null) {
        return "";
    } else {
        int cellType = cell.getCellType();
        String result;
        switch (cellType) {
        case Cell.CELL_TYPE_STRING:
            result = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_NUMERIC:
            result = ("" + cell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            result = ("" + cell.getNumericCellValue());
            break;
        default:
            result = "";
            break;
        }
        return result;
    }
}

From source file:fenix.util.convert.InvoiceConverter.java

License:Open Source License

/**
 *   ? ?.//from w w  w.j  a  v  a 2  s  .  com
 * @param cell - ?, ?? ? 
 * @return - ? .
 */
private static Integer validateInt(Cell cell) {
    if (cell == null) {
        return 0;
    }
    Double d = cell.getNumericCellValue();
    Integer result = d.intValue();
    return result;
}

From source file:fenix.util.convert.InvoiceConverter.java

License:Open Source License

private static Double validateDouble(Cell cell) {
    if (cell == null) {
        return 0d;
    }//from  www.  jav a  2  s  .  co  m
    Double d = cell.getNumericCellValue();
    return d;
}

From source file:fenix.util.convert.POIConverter.java

License:Open Source License

/**
 *   ?.// w  ww . j a va  2 s.c  o  m
 * @param cell - ?, ?? ? .
 * @return -  ? .
 * @throws UnsupportedEncodingException 
 */
private String validateString(Cell cell) throws UnsupportedEncodingException {
    if (cell == null) {
        return "";
    } else {
        int cellType = cell.getCellType();
        String result;
        switch (cellType) {
        case Cell.CELL_TYPE_STRING:
            result = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_NUMERIC:
            result = ("" + cell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            result = ("" + cell.getNumericCellValue());
            break;
        default:
            result = "";
            break;
        }
        return result;
    }
}

From source file:fenix.util.convert.POIConverter.java

License:Open Source License

/**
 *   ? ?.//from   w  ww  . ja  v  a  2s .  c o  m
 * @param cell - ?, ?? ? 
 * @return - ? .
 */
private Integer validateInt(Cell cell) {
    assert cell != null;
    Double d = cell.getNumericCellValue();
    Integer result = d.intValue();
    return result;
}

From source file:fenix.util.convert.POIConverter.java

License:Open Source License

private Double validateDouble(Cell cell) {
    assert cell != null;
    Double d = cell.getNumericCellValue();
    return d;
}

From source file:fft.FFT.java

License:Open Source License

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

    InputStream myxls = new FileInputStream("/Users/huangge/Documents/workspace/fft/src/BxDec99.xls");
    HSSFWorkbook wb = new HSSFWorkbook(myxls);
    HSSFSheet sheet = wb.getSheetAt(0);//from  ww w .  j  a v  a2s  .c o  m
    int rowStart = Math.min(15, sheet.getFirstRowNum());
    int rowEnd = Math.max(1400, sheet.getLastRowNum());
    Row r_for_rowCount = sheet.getRow(0);
    int lastColumn = Math.min(r_for_rowCount.getLastCellNum(), 1000);

    double[][] res = new double[lastColumn - 1][rowEnd];
    Workbook wb_out = new HSSFWorkbook(); // or new XSSFWorkbook();
    Sheet sheet_out = wb_out.createSheet();
    int count = 0;
    for (int j = 1; j < lastColumn; j++) {//make res matrix
        count = 0;
        for (int i = 1; i <= rowEnd; i++) {
            Row r = sheet.getRow(i);
            Cell c = r.getCell(3, Row.RETURN_BLANK_AS_NULL);
            if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
                break;
            } else if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                res[j - 1][i - 1] = c.getNumericCellValue();
                count++;
            }
        }
    }

    int N = count;
    int nextPowTwo = 1;
    while (nextPowTwo < N) {
        nextPowTwo += nextPowTwo;
    }
    N = nextPowTwo;
    FFT fft = new FFT(N);
    double[] window = fft.getWindow();
    double[] re = new double[N];
    Arrays.fill(re, 0);
    ;
    double[] im = new double[N];

    for (int i = 0; i < re.length / 2; i++) {//initial sheet
        Row row_cre = sheet_out.createRow(i);
        for (int k = 0; k < lastColumn - 1; k++) {
            Cell cell = row_cre.createCell((short) (k));
        }
    }

    for (int j = 1; j < lastColumn; j++) {//make result sheet
        for (int i = 0; i < count; i++) {
            re[i] = res[j - 1][i];
            im[i] = 0;
        }
        beforeAfter(fft, re, im);
        for (int i = 0; i < re.length / 2; i++) {
            Row row_out = sheet_out.getRow(i);
            Cell cell = row_out.getCell((short) (j - 1));
            cell.setCellValue(Math.abs(re[i]));
        }

    }

    FileOutputStream fileOut//write file
            = new FileOutputStream("/Users/huangge/Documents/workspace/fft/src/workbook.xls");
    wb_out.write(fileOut);
    fileOut.close();

    long time = System.currentTimeMillis();
    double iter = 10;
    for (int i = 0; i < iter; i++)
        // fft.fft(re,im);
        time = System.currentTimeMillis() - time;
    System.out.println("Averaged " + (time / iter) + "ms per iteration");
}

From source file:File.XLS.ReadXLS.java

public void readXLSFile(String path, String filename, int Fcol, int Lcol, int Frow, int Lrow) {
    try {/* w  ww .j a v  a2 s .co  m*/
        //String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        FileInputStream file = new FileInputStream(new File(path + filename + ".xls"));

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

        HSSFSheet sheet = workbook.getSheetAt(0);
        for (int x = Frow; x < Lrow; x++) {
            Row rows = sheet.getRow(x);
            if (rows != null) {
                String key = null;
                for (int colIndex = Fcol; colIndex <= Lcol; colIndex++) {
                    Cell cell = rows.getCell(colIndex);
                    if (cell != null) {
                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "\t");
                            break;
                        }
                    }
                }
            }
            System.out.println("");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:File.XLSX.ReadXLSX.java

/**
 * @param args the command line arguments
 *///from  w w  w. java  2  s.  c om
public void readXLSXFile(String path, String filename, int Fcol, int Lcol, int Frow, int Lrow) {
    try {
        //String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        FileInputStream file = new FileInputStream(new File(path + filename + ".xlsx"));

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

        //Get first and last colomn in file
        //int Fcols = (int) upper.indexOf(Fcol.toUpperCase());
        //int Lcols = (int) upper.indexOf(Lcol.toUpperCase());
        //Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);
        for (int x = Frow; x < Lrow; x++) {
            Row rows = sheet.getRow(x);
            if (rows != null) {
                String key = null;
                for (int colIndex = Fcol; colIndex <= Lcol; colIndex++) {
                    Cell cell = rows.getCell(colIndex);
                    if (cell != null) {
                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "\t");
                            break;
                        }
                    }
                }
            }
            System.out.println("");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:FileHelper.ExcelHelper.java

public DataSheet ReadTestCaseFileFromSheet(String fileName, String sheetName, MyDataHash myDataHash,
        String rawData) {//w ww  .  jav  a  2s.  c om
    try {
        File excel = new File(fileName);
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook book = new XSSFWorkbook(fis);
        XSSFSheet sheet = book.getSheet(sheetName);
        Iterator<Row> itr = sheet.iterator();
        DataSheet dataSheet = new DataSheet();
        ArrayList<RowDataFromFile> datas = new ArrayList<RowDataFromFile>();
        ArrayList<DataHash> dataHash = new ArrayList<>();
        int colmnDataStart = 0, colmnDataStop = 0, numReal = 0;
        ArrayList<NameDynamic> nameDynamic = new ArrayList<NameDynamic>();
        ArrayList<DataInput> listDataInput = new ArrayList<>();
        ArrayList<DataInputLevel2> dataInputLevel2 = new ArrayList<>();
        while (itr.hasNext()) {
            RowDataFromFile dataRow = new RowDataFromFile();
            JsonObject jObjReq = new JsonObject();
            String caller = "";

            Row row = itr.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            Cell cell = cellIterator.next();
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING: {
                String str = cell.getStringCellValue();
                if (str.equals("STT")) {
                    while (cellIterator.hasNext()) {
                        Cell cell1 = cellIterator.next();
                        switch (cell1.getCellType()) {
                        case Cell.CELL_TYPE_STRING: {
                            //                                        System.out.println(cell1.getStringCellValue());
                            if (cell1.getStringCellValue().equals("Data Request")) {
                                colmnDataStart = cell1.getColumnIndex();
                            }
                            if (cell1.getStringCellValue().equals("Threads")) {
                                colmnDataStop = cell1.getColumnIndex() - 1;
                            }
                            if (cell1.getStringCellValue().equals("Result Real")) {
                                //                                            System.out.println("Colmn Reail: " + cell1.getColumnIndex());
                                numReal = cell1.getColumnIndex();
                            }
                            break;
                        }
                        case Cell.CELL_TYPE_NUMERIC: {
                            System.out.println(cell1.getNumericCellValue());
                            break;
                        }
                        }
                    }
                    Row row1 = sheet.getRow(1);
                    Row row2 = sheet.getRow(2);
                    Row row3 = sheet.getRow(3);
                    Row row4 = sheet.getRow(4);
                    Cell cellColmn;
                    Cell cellColmn2;
                    int numColmn = colmnDataStart;
                    while (numColmn <= colmnDataStop) {
                        cellColmn = row1.getCell(numColmn);
                        String temp = GetValueStringFromCell(cellColmn);
                        cellColmn2 = row2.getCell(numColmn);
                        NameDynamic nameDy = CutStrGetNameDynamic(GetValueStringFromCell(cellColmn2));
                        if (nameDy.getIsDyn().equals("1")) { // Check Data is change when run Thread
                            nameDynamic.add(nameDy);
                        }
                        // Add to list save data api
                        listDataInput.add(new DataInput(temp, nameDy.getName()));
                        DataHash dataHt = myDataHash.CheckNameDataIsHash(sheetName, nameDy.getName());
                        if (dataHt != null) {
                            dataHt.setNumColumn(numColmn);
                            dataHash.add(dataHt);
                        }
                        if (temp.equals("Object")) { // Exist object group datas name
                            ArrayList<DataInput> listDataIputLevel2 = new ArrayList<>();
                            cellColmn = row3.getCell(numColmn);
                            cellColmn2 = row4.getCell(numColmn);
                            String tempT = GetValueStringFromCell(cellColmn);
                            if (!tempT.equals("")) {
                                while (!GetValueStringFromCell(cellColmn).equals("")) {
                                    nameDy = CutStrGetNameDynamic(GetValueStringFromCell(cellColmn2));
                                    if (nameDy.getIsDyn().equals("1")) { // Check Data is change when run Thread
                                        nameDynamic.add(nameDy);
                                    }
                                    dataHt = myDataHash.CheckNameDataIsHash(sheetName, nameDy.getName());
                                    if (dataHt != null) {
                                        dataHt.setNumColumn(numColmn);
                                        dataHash.add(dataHt);
                                    }
                                    listDataIputLevel2.add(
                                            new DataInput(GetValueStringFromCell(cellColmn), nameDy.getName()));
                                    numColmn++;
                                    cellColmn = row3.getCell(numColmn);
                                    cellColmn2 = row4.getCell(numColmn);
                                }
                                numColmn--;
                                dataInputLevel2.add(new DataInputLevel2(listDataIputLevel2));
                            } else {
                                dataInputLevel2.add(new DataInputLevel2(listDataIputLevel2));
                            }
                        }
                        numColmn++;
                    }
                    Gson gson = new Gson();
                    System.out.println(gson.toJson(listDataInput));
                    System.out.println(gson.toJson(dataHash));
                }
                break;
            }
            case Cell.CELL_TYPE_NUMERIC: {
                //                        System.out.println(cell.getNumericCellValue());
                if (cell.getNumericCellValue() > 0) {
                    dataRow.setId(row.getRowNum());
                    String isSecutiry = "no";
                    int arrIndex = 0;
                    int arrIndexReq = 0; // Object con
                    int arrIndexRow = 0;
                    while (cellIterator.hasNext()) {
                        Cell cell1 = cellIterator.next();
                        if ((cell1.getColumnIndex() >= colmnDataStart)
                                && (cell1.getColumnIndex() < colmnDataStop)) {
                            if (listDataInput.get(arrIndex).getType().equals("Object")) {
                                JsonObject jObj = new JsonObject();
                                int i = 0;
                                int size = dataInputLevel2.get(arrIndexReq).getListDataIputLevel2().size();
                                while (i < size) {
                                    if (dataInputLevel2.get(arrIndexReq).getListDataIputLevel2().get(i)
                                            .getType().equals("String")) {
                                        String value = GetValueStringFromCell(cell1);
                                        if (!dataHash.isEmpty()) {
                                            for (DataHash dataH : dataHash) {
                                                if (dataH.getNumColumn() == cell1.getColumnIndex()) {
                                                    value = EncryptHelper.EncryptData(value,
                                                            dataH.getAlgorithm(), dataH.getKey(),
                                                            dataH.getIv());
                                                }
                                            }
                                        }
                                        jObj.addProperty(dataInputLevel2.get(arrIndexReq)
                                                .getListDataIputLevel2().get(i).getName(), value);
                                    } else if (dataInputLevel2.get(arrIndexReq).getListDataIputLevel2().get(i)
                                            .getType().equals("Integer")) {
                                        int value = GetValueIntegerFromCell(cell1);
                                        jObj.addProperty(dataInputLevel2.get(arrIndexReq)
                                                .getListDataIputLevel2().get(i).getName(), value);
                                    } else if (dataInputLevel2.get(arrIndexReq).getListDataIputLevel2().get(i)
                                            .getType().equals("Object")) {
                                        String value = GetValueStringFromCell(cell1);
                                        Gson gson = new Gson();
                                        JsonObject obj = gson.fromJson(value, JsonObject.class);
                                        jObj.add(dataInputLevel2.get(arrIndexReq).getListDataIputLevel2().get(i)
                                                .getName(), obj);
                                    }
                                    i++;
                                    if (i < size) {
                                        cell1 = cellIterator.next();
                                    }
                                }
                                arrIndexReq++;
                                jObjReq.add(listDataInput.get(arrIndex).getName(), jObj);
                            } else if (listDataInput.get(arrIndex).getType().equals("String")) {
                                String value = GetValueStringFromCell(cell1);
                                if (!dataHash.isEmpty()) {
                                    for (DataHash dataH : dataHash) {
                                        if (dataH.getNumColumn() == cell1.getColumnIndex()) {
                                            value = EncryptHelper.EncryptData(value, dataH.getAlgorithm(),
                                                    dataH.getKey(), dataH.getIv());
                                        }
                                    }
                                }
                                jObjReq.addProperty(listDataInput.get(arrIndex).getName(), value);
                            } else if (listDataInput.get(arrIndex).getType().equals("Integer")) {
                                int value = GetValueIntegerFromCell(cell1);
                                jObjReq.addProperty(listDataInput.get(arrIndex).getName(), value);
                            }
                            arrIndex++;
                        } else if (cell1.getColumnIndex() == colmnDataStop) {
                            isSecutiry = GetValueStringFromCell(cell1);
                            dataRow.setNameAlgorithm(isSecutiry);
                        } else if (cell1.getColumnIndex() > colmnDataStop) {
                            if (arrIndexRow == 0) {
                                dataRow.setThread(GetValueIntegerFromCell(cell1));
                            } else if (arrIndexRow == 1) {
                                dataRow.setResultExpect(GetValueStringFromCell(cell1));
                            }
                            arrIndexRow++;
                        }
                    }
                    //                            System.out.println("data: " + jObj.toString());
                    //                            System.out.println("data Req: " + jObjReq.toString());
                    String[] arrR = rawData.split(",");
                    String rawDataNew = "";
                    char a = '"';
                    for (String str : arrR) {
                        if (str.charAt(0) == a) {
                            String value = str.substring(1, str.length() - 1);
                            rawDataNew += value;
                        } else {
                            JsonElement je = jObjReq.get(str);
                            if (je.isJsonObject()) {
                                String value = je.toString();
                                rawDataNew += value;
                            } else {
                                String value = je.getAsString();
                                rawDataNew += value;
                            }
                        }
                    }
                    String[] arr = isSecutiry.split("-");
                    if (arr[0].equals("chksum")) {
                        String chksum = CheckSumInquireCard.createCheckSum(isSecutiry, rawDataNew);
                        //                                System.out.println("chksum: " + chksum);
                        jObjReq.addProperty(listDataInput.get(arrIndex).getName(), chksum);
                    } else if (arr[0].equals("signature")) {
                        String signature = RSASHA1Signature.getSignature(isSecutiry, rawDataNew);
                        //                                System.out.println("signature: " + signature);
                        jObjReq.addProperty(listDataInput.get(arrIndex).getName(), signature);
                    }
                    //                            System.out.println("data Request: " + jObjReq.toString());
                    dataRow.setData(jObjReq);
                    dataRow.setNumReal(numReal);
                    Gson gson = new Gson();
                    System.out.println("data row: " + gson.toJson(dataRow));
                    datas.add(dataRow);
                }
                break;
            }
            }
        }
        dataSheet.setDatas(datas);
        dataSheet.setNameDynamic(nameDynamic);
        dataSheet.setListDataInput(listDataInput);
        dataSheet.setDataInputLevel2(dataInputLevel2);
        Gson gson = new Gson();
        //            System.out.println("save data: " + gson.toJson(datas));
        fis.close();
        return dataSheet;
    } catch (Throwable t) {
        System.out.println("Throwsable: " + t.getMessage());
        return new DataSheet();
    }
}