根据XML配置规则导入Excel数据(七)测试

  测试代码
 
 
package com.randy; 

import java.io.InputStream; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 

import junit.framework.TestCase; 

import org.apache.commons.beanutils.PropertyUtils; 

import com.ivfly.xlsbean.BeanSpecification; 
import com.ivfly.xlsbean.BeansSpecification; 
import com.ivfly.xlsbean.ExcelAble; 
import com.ivfly.xlsbean.ExcelReader; 
import com.ivfly.xlsbean.test.Bean; 

public  class Test  extends TestCase { 
   public  void test1(){ 
    String xlsdir=ClassLoader.getSystemResource( "com/ivfly/xlsbean/test/").getPath(); 
     //根据配置文件获得描述 
    InputStream xmlStream = ClassLoader.getSystemResourceAsStream( "com/ivfly/xlsbean/test/bean.xml"); 
    BeansSpecification beans = BeansSpecification.getInstance(xmlStream); 
    BeanSpecification bean = beans.getBeanByClassName(Bean. class.getName()); 
    List<ExcelAble> list = ExcelReader.parseXls2Beans(bean,xlsdir, "测试用户数据.xls"); 
    List<String> pl = bean.getPropertyList(); 
    Map<String,String> head = bean.getNv(); 
    Iterator<String> iter = head.keySet().iterator(); 
     while(iter.hasNext()){ 
      String key = iter.next(); 
      String value = head.get(key); 
      System.out.print( "|"+value+""); 
    } 
     
    System.out.println( "---------------------"); 
     for(ExcelAble o : list){ 
       if(!o.isCheck()){ 
         continue
      } 
       for(String p : pl){ 
         try { 
          Object value =  null
          value =PropertyUtils.getProperty(o,p); 
          System.out.print( "|p="+p+ ";v="+value+""); 
        }  catch (Exception e) { 
          e.printStackTrace(); 
        }    
         
         
      } 
//      while(iter2.hasNext()){ 
//        String key = iter2.next(); 
//        Object value = null; 
//        try { 
//          value =PropertyUtils.getProperty(o,key); 
//        } catch (Exception e) { 
//          e.printStackTrace(); 
//        }    
//        System.out.print("|"+value+""); 
        
//      }     
      System.out.println(); 
    } 
     
     
     for(ExcelAble o : list){ 
       if(!o.isCheck()){ 
        System.out.println(o.getMessage()); 
      }        
    } 
  } 

 
 
结果
 
|编号|用户名|QQ号|年龄|出生日期--------------------- 
|p=id;v=wl002|p=name;v=null|p=qq;v=20316558|p=age;v=23|p=birthDate;v=Fri Dec 23 00:00:00 CST 2011 
第4行数据:年龄 数据格式不正确 

第6行数据:年龄 数据格式不正确 

Excel数据
 
最后。
这是自己在元旦假期,闲来无事。花了一整天的时间,研究的结果。
比较仓促。还存在一些问题。比如:
1.这只是针对列表的导入
2.如果是对单个对象的导入则不支持
3.没有字符串长度的验证
4.只是简单验证了下年龄有效性的验证,其他的均没有验证。
5.时间的导入,是否支持多种类型
7.没有提供字典信息编码的支持。
 
以后自己再慢慢 丰富吧。
今天自己又复习了下
Digester
BeanUtils
lang
 


本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/756541,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值