的excel無法讀取在此處,但單元格正在返回String而不是日期列中的數字。所以,請建議我該怎麼做才能將它作爲數字返回......無法讀取其格式爲: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;
}
請只發布代碼的相關位,並具體說明您的問題。我有一個人不會通過你的整個程序來調試它。 –
當你說'它沒有返回數字'時,你期望得到什麼數字,數字代表什麼? –
你是否在這一行中引發了你自己的問題:String cellVal = cell.getDateCellValue()。toString(); //從單元格獲取日期並將其轉換爲字符串類型。 – xQbert