当前位置: 代码迷 >> Java Web开发 >> 关于java读取excel的有关问题。读取到某行有空值的异常
  详细解决方案

关于java读取excel的有关问题。读取到某行有空值的异常

热度:252   发布时间:2016-04-13 22:30:40.0
关于java读取excel的问题。读取到某行有空值的错误
	private void readXml(String fileName) {
 
 boolean isE2007 = false;    //判断是否是excel2007格式  
        if(fileName.endsWith("xlsx"))  
            isE2007 = true;  
        try {  
            InputStream input = new FileInputStream(fileName);  //建立输入流  
            Workbook wb  = null;  
            //根据文件格式(2003或者2007)来初始化  
            if(isE2007)  
                wb = new XSSFWorkbook(input);  
            else  
                wb = new HSSFWorkbook(input);  
            Sheet sheet = wb.getSheetAt(0);     //获得第一个表单  
            Iterator<Row> rows = sheet.rowIterator(); //获得第一个表单的迭代器   
            while (rows.hasNext()) {  
                Row row = rows.next();  //获得行数据                  
                System.out.println("Row #" + row.getRowNum());  //获得行号从0开始  
                Iterator<Cell> cells = row.cellIterator();    //获得第一行的迭代器  
                String value = "";  
                while (cells.hasNext()) {  
                    Cell cell = cells.next();  //获得列数据
                    System.out.println("Cell #" + cell.getColumnIndex());  
                    switch (cell.getCellType()) {   //根据cell中的类型来输出数据  
                    case HSSFCell.CELL_TYPE_NUMERIC:  
                     //   System.out.println(cell.getNumericCellValue());  
                        DecimalFormat df = new DecimalFormat("0");                           
                        String shu = df.format(cell.getNumericCellValue());  //将科学计数的转换
                        value += shu+ ","; 
                        System.out.println(shu);
                        break;  
                    case HSSFCell.CELL_TYPE_STRING:  
                        System.out.println(cell.getStringCellValue());  
                        value += cell.getStringCellValue() + ","; 
                        break;  
                    case HSSFCell.CELL_TYPE_BOOLEAN:  
                        System.out.println(cell.getBooleanCellValue());  
                        value += cell.getBooleanCellValue()+ ","; 
                        break;  
                    case HSSFCell.CELL_TYPE_FORMULA:  
                        System.out.println(cell.getCellFormula());  
                        value += cell.getCellFormula()+ ","; 
                        break; 
                    default:  
                        System.out.println("unsuported sell type");  
                    break;  
                    }                  
                } 
                //写入数据库
                if(row.getRowNum()>0){
                String[] val = value.split(",");
                System.out.println(value);
                AddressList addressList= new AddressList();                 
                addressList.setName(val[0]); 
                addressList.setTel(val[1]);
                addressList.setEmall(val[2]);
                addressList.setFax(val[3]);
                addressList.setRoomnum(val[4]);
                addressList.setOph(val[5]);
                addressList.setAddress(val[6]);
                addressList.setComments(val[7]); 
                addressList.setSort(Integer.parseInt(val[8]));
             //addressList.setBirthday(val[9]);
                
                addressList.setPid(pid);
                addressList.setUserid(userid);                 
                addressList.setType(type);      
                addressList.setSex(0);                 
                addressListService.save(addressList);
                }
            }   
             
        } catch (IOException ex) {  
            ex.printStackTrace();  
        }  
    }   


代码如上,原文http://blog.csdn.net/shuwei003/article/details/6741649 ,我在原文的基础上修改的,我是导入到sql里面 出现问题的地方是在//写入数据库,
当读取到第五列的时候因为是空的,addressList.setName(val[0]); 这就取不到,因为是根据数字来取值的,没有值就错。一错整个方法就运行不了
------解决思路----------------------
http://blog.csdn.net/shuwei003/article/details/6741649

试试能不能满足你的需求
------解决思路----------------------
   while (cells.hasNext()) {  
                    Cell cell = cells.next();  //获得列数据
                    System.out.println("Cell #" + cell.getColumnIndex());  
                    switch (cell.getCellType()) {   //根据cell中的类型来输出数据  
                    case HSSFCell.CELL_TYPE_NUMERIC:  
                     //   System.out.println(cell.getNumericCellValue());  
                        DecimalFormat df = new DecimalFormat("0");                              
                        String shu = df.format(cell.getNumericCellValue());  //将科学计数的转换
                        value += shu+ ","; 
                        System.out.println(shu);
                        break;  
                    case HSSFCell.CELL_TYPE_STRING:  
                        System.out.println(cell.getStringCellValue());  
                        value += cell.getStringCellValue() + ","; 
                        break;  
                    case HSSFCell.CELL_TYPE_BOOLEAN:  
                        System.out.println(cell.getBooleanCellValue());  
                        value += cell.getBooleanCellValue()+ ","; 
                        break;  
                    case HSSFCell.CELL_TYPE_FORMULA:  
                        System.out.println(cell.getCellFormula());  
                        value += cell.getCellFormula()+ ","; 
                        break; 
                    default:  
                        System.out.println("unsuported sell type");  
                    break;  
                    }                     
                } 

具体看看你的 数据 我看上面每一行都有 个  
 default:  
 value +=  ","; 
                        System.out.println("unsuported sell type");  
                    break;  
试试 。
你在应该 value 的个数和你 的单元格个数数量一样。 
------解决思路----------------------
全部用String来接收他,初始给个null;
然后加个判断if (a=null){a=0;}
等要操作的时候强转int
  相关解决方案