Cannot get a STRING value from a NUMERIC cell
错误一:
导入Excel表格数据,该列设置数据类型为String,输入数据是int类型
解决方法:cell.setCellType(CellType.STRING);
错误二:日期类型需要定义格式
解决方法如下:
for(int i = 1 ; i <= totalRowNum ; i++)
{
//获得第i行对象
Row row = sheet.getRow(i);
//获得获得第i行第0列的 String类型对象
Cell cell = row.getCell((short)0);
id = (int) cell.getNumericCellValue();
cell = row.getCell((short)1);
**cell.setCellType(CellType.STRING);**
barcode = cell.getStringCellValue().toString();
cell = row.getCell((short)2);
bookname = cell.getStringCellValue().toString();
cell = row.getCell((short)3);
typeid = (int) cell.getNumericCellValue();
cell = row.getCell((short)4);
author = cell.getStringCellValue().toString();
cell = row.getCell((short)5);
ISBN = cell.getStringCellValue().toString();
cell = row.getCell((short)6);
publisher = cell.getStringCellValue().toString();
cell = row.getCell((short)7);
price = (float) cell.getNumericCellValue();
cell = row.getCell((short)8);
page = (int) cell.getNumericCellValue();
cell = row.getCell((short)9);
if (cell != null)
{
if (cell.getCellTypeEnum() != CellType.STRING && HSSFDateUtil.isCellDateFormatted(cell))
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
intime = sdf.format(date);
System.out.println(intime);
}
}
cell = row.getCell((short)10);
introduc = cell.getStringCellValue().toString();
cell = row.getCell((short)11);
bcount = (int) cell.getNumericCellValue();
cell = row.getCell((short)12);
operator = cell.getStringCellValue().toString();
//获得一个数字类型的数据
//cell = row.getCell((short)1);
//latitude = (int) cell.getNumericCellValue();
book.setId(id);
book.setBarcode(barcode);
book.setBookname(bookname);
book.setTypeid(typeid);
book.setAuthor(author);
book.setISBN(ISBN);
book.setPublisher(publisher);
book.setPrice(price);
book.setPage(page);
book.setIntime(intime);
book.setIntroduc(introduc);;
book.setBcount(bcount);
book.setOperator(operator);
bookDao.add(book);
}