作者:乔山办公网日期:
返回目录:excel表格制作
太笼统了,只能给出一个POI读取Excel的大致方法。
对数据的校验,与具体的文件有关。
你定义的那一列是时间类型的,那一列是数字列的。
盲目的读取,无法判断数据的类型。
思路为:为了百防止数字变成科学计数法方式表示度,在源文件以及java代码中都用文本的方式去生成和解析excel,具体如问下: 1.生成Excel时,设置单元答格格式为STRING,即: //关键内代码容HSSFCell cell = new HSSFCell();cell.setCellType(HSSFCell.CELL_TYPE_STRING);2.同理,解析的时候,首先要保证源excel文件中该单元格格式是文本类型的,然后在java代码里用STRING类型去解析: //关键代码String value = cell.getStringCellValue()。
public static String getStringCellValue(Cell cell) {
if(cell==null){
return null;
}
String value = "";
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else {
value = "";
}
} else {
value =
new DecimalFormat("0").format(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_FORMULA: //导入时如果为公式生成的数据则无e799bee5baa6e58685e5aeb9336值
// System.out.println("Formula:" + cell.getStringCellValue());
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
value = "";
break;
case Cell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y" : "N");
break;
default:
value = "";
}
return value;
}
这个你要把原来excel中的数据分类型来读取,分文本、数字、日期等,如果是文本型的,就不是科学计数法表示的了。