jfinal+poi,从excel中获取数据,写入数据库时常见错误处理

1.NotSerializableException

直接上代码

		HSSFWorkbook sheets = new HSSFWorkbook();
        HSSFSheet sheetAt = sheets.getSheetAt(0);
        HSSFRow row = sheetAt.getRow(0);

        Record record = new Record();
        record.set("",row.getCell(0));

出错在最后一行,看源码可以知道Record它实现了Serializable,所以不是它的问题,那就有可能是HSSFRow有问题,查看它的源码可以知道

public final class HSSFRow implements Row, Comparable<HSSFRow> {
.....
}

这两个都是接口,并且没有找到跟Serializable有关的方法,解决方法是调用它的toString()方法。代码如下

		HSSFWorkbook sheets = new HSSFWorkbook();
        HSSFSheet sheetAt = sheets.getSheetAt(0);
        HSSFRow row = sheetAt.getRow(0);

        Record record = new Record();
        record.set("",row.getCell(0).toString());

因为HSSFRow的toString()方法会对数据进行序列化处理。

2.NullPointException

报这个错的原因是有可能表格中有空的数据,解决方法如下:

		HSSFWorkbook sheets = new HSSFWorkbook();
        HSSFSheet sheetAt = sheets.getSheetAt(0);
        HSSFRow row = sheetAt.getRow(0);

        Record record = new Record();
        record.set("",row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).toString());

3.For input string: "1.0"(任意数字)

解决方法如下:

		HSSFWorkbook sheets = new HSSFWorkbook();
        HSSFSheet sheetAt = sheets.getSheetAt(0);
        HSSFRow row = sheetAt.getRow(0);

        Record record = new Record();
        String s = row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).toString();
        String[] split = s.split(".0");
        record.set("",Integer.parseInt(split[0]));

你可能感兴趣的:(Java)