Невозможно прочитать из Excel, формат которого: DDMMYYYY - PullRequest
0 голосов
/ 07 декабря 2011

Здесь, но ячейка возвращает String вместо числа из столбцов даты.Итак, пожалуйста, предложите мне, что я должен сделать, чтобы вернуть его как числовой .........

public Object readRow(Iterator<Cell> cells, Workbook workbook) {
        Cell cell = cells.next();// get the cell from passed cellIterator one by one
        int recordindex = cell.getColumnIndex();//get the index of current cell by calling getColumnIndex() mthd.
        if (recordindex >= record.length) {// check the index of current cell. which is must be less than or equal to 11.
            System.out.println("Forcibly returning the reader as it is trying to read above the allowable length");
            return null;// if current cell index is greater than 11, means after that cell no records in cell or may record is not usable.
        }
        Object value = null;// delare an Object.

        switch (cell.getCellType()) {// check which type of record in the cell.
            **case Cell.CELL_TYPE_NUMERIC**:// if cell has Numeric type record then this case block is run.
                if (recordindex == 1 || recordindex == 2) {// if current cell index is 2 or 3, means record may be Trans Date or Value Date.
                     String cellVal = cell.getDateCellValue().toString();// get date from cell and covert it in String type.
                     DateFormat dateFormat = DateFormat.getDateInstance();
                    try {
                        String date = new SimpleDateFormat("MM/dd/yyyy").format(new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(cellVal.toString()));
                        // in above line first get the cell date in String type parse it to ("EEE MMM dd HH:mm:ss zzz yyyy") date format and finally pass it to format() mthd to fomat in "MM/dd/yyyy" format.
                        cellVal = date;// set the formatted date in cell
                    } catch (Exception e) {
                    }
                    record[recordindex] = cellVal;// save the cell value (data) in String array in same array index as cell's index.
                    //  System.out.println("Cell Value for String " + cellVal);
                    break;
                } else if (recordindex == 0 || recordindex == 3 || recordindex == 6 || recordindex == 7
                        || recordindex == 8 || recordindex == 9) {// if the current cell index is 1, 4, 7, 8, 9 or 10
                    NumberFormat formatter = new DecimalFormat("#0");
                    String cellVal = formatter.format(cell.getNumericCellValue());// if above condition is met then fromat the cell number value.
                    record[recordindex] = cellVal;// save the formatted number value in String array "record" in same index of cell index.
                    //  System.out.println("Cell Value for num " + cellVal);
                    break;

                } else {// This block is run if the cell index is 5 or 6
                    String cellVal = String.valueOf(cell.getNumericCellValue());//get the cell numeric value and convert it to String type. 5, 6 index means DR or Cr
                    if (cellVal.equals("")) {// check cell value is null or not
                        record[recordindex] = null;// if cell value is null then save the null in "record" String array in cell's index index.
                        //System.out.println("Cell Value for blank " + cellVal);
                        break;
                    } else {

                        record[recordindex] = cellVal;// if cell has some value then save it in "record" array cell's index index.
                        break;
                    }

                }

            **case Cell.CELL_TYPE_STRING:** {// if the cell value type is String then this case block is invoke.
                record[recordindex] = cell.getRichStringCellValue().toString();// get current cell value convert it in String type and save it in "record" same as above.
                break;
            }
            case Cell.CELL_TYPE_BLANK: {// cell value is 'blank' then save null in "record" String array in cell's index index.
//                System.out.println("blank cell");

                record[recordindex] = null;
                break;
            }
            case Cell.CELL_TYPE_FORMULA: {
//                HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(workBook);
//                HSSFFormulaEvaluator.CellValue cellValue = evaluator.evaluate(cell);
//                value = cellValue.getNumberValue();

                // record[recordindex] = new Double(cellValue.getNumberValue()).toString();

                break;
            }

            default: {

                break;
            }
        }

        return value;
    }

Ответы [ 2 ]

0 голосов
/ 07 декабря 2011

Ваша функция возвращает ноль наверняка ... Интересно, почему !!

В любом случае, почему бы не пойти на DateFormatting?

DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
String s = "2011-11-10";

Date d = this.format.parse(s);
0 голосов
/ 07 декабря 2011

Я не читаю ваш код, но отвечаю на ваш вопрос.

В Java вы можете использовать SimpleDateFormat для анализа даты из строки.Например:

DateFormat format = new SimpleDateFormat("ddMMyyyy");
String s = "30112011";
Date d = format.parse(s);
System.err.println(d);

Вывод:

Wed Nov 30 00:00:00 GMT 2011
...