无法读取格式为:DDMMYYYY 的 Excel

发布于 2024-12-19 18:03:34 字数 4676 浏览 0 评论 0原文

在这里,但单元格返回字符串而不是日期列中的数字。所以,请建议我该怎么做才能将其返回为数字............

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;
    }

Here, but the cell is returning String in place of numeric from date columns. So, please suggest me what should i do to return it as numeric.........

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

会发光的星星闪亮亮i 2024-12-26 18:03:34

我不是在阅读你的代码,而是回答你的问题。

使用 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

I'm not reading your code, but answering your question.

With Java you can use a SimpleDateFormat to parse a Date from a String. For example:

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

The output is:

Wed Nov 30 00:00:00 GMT 2011
梨涡 2024-12-26 18:03:34

你的函数肯定返回 null...我想知道为什么!!

不管怎样,为什么不选择 DateFormatting 呢?

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

Date d = this.format.parse(s);

your function is returning null for sure... I wonder why!!

Anyways, why not go for DateFormatting ?

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

Date d = this.format.parse(s);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文