无法读取格式为:DDMMYYYY 的 Excel
在这里,但单元格返回字符串而不是日期列中的数字。所以,请建议我该怎么做才能将其返回为数字............
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是在阅读你的代码,而是回答你的问题。
使用 Java,您可以使用 SimpleDateFormat 来解析字符串中的日期。例如:
输出为:
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:
The output is:
你的函数肯定返回 null...我想知道为什么!!
不管怎样,为什么不选择 DateFormatting 呢?
your function is returning null for sure... I wonder why!!
Anyways, why not go for DateFormatting ?