2011-12-07 12 views
-1

ここで、セルは日付列の数値の代わりにStringを返しています。だから、数字で返すにはどうすればいいのか教えてください。excelからの読み込みができない形式:DDMMYYYY

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

関連するコードを投稿して問題を具体的に記述してください。私は1つのために、あなたのプログラム全体をデバッグするためにウェードするつもりはありません。 –

+0

「数値が返されない」と言うとき、あなたは何番目の数字を期待していますか?数字は何を表していますか? –

+0

この行に問題がありますか?String cellVal = cell.getDateCellValue()。toString(); //セルから日付を取得し、文字列型で変換します。 – xQbert

答えて

0

私はあなたのコードを読んでいませんが、あなたの質問に答えています。

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 
0

あなたの機能は確かにnullを返している...なぜだろうか!

とにかく、DateFormattingに行ってみませんか?

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

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

このコードを実行しようとしましたか?あなたは結果に驚くかもしれません。 YYYY-MM-DDは有効なパターンではありません。 – sudocode

+0

@sudocode:今は大丈夫ですか? – Nik

+0

試しましたか? ;) – sudocode

関連する問題