本帖最后由 lujuzhi1202 于 2009-10-16 11:37:22 编辑

解决方案 »

  1.   

    2如何将这些数据封装成List 把它作为write方法的参数传进
      把数据先封装成一个类Info,类属性就是name,cellPhone等等,每个配一个get方法
      每从excel中取出一个信息,new一个Info对象,然后add 到 List<Info> 中去
     
    然后遍历List<Info>,每得到一个对象info
    .....
    .....
    ps.setString(1,info.getName());
    ps.setString(2,info.getCellPhone);
    ps.setString(3,info,getAddress);
    ....
    ....
    大概思路是这样
      

  2.   


    private List<ObjInfo> getContentList(File file) throws Exception{
    // 通过Workbook的静态方法getWorkbook选取Excel文件
    Workbook workbook = Workbook.getWorkbook(file);
    // 通过Workbook的getSheet方法选择第一个工作簿(从0开始)
    Sheet sheet = workbook.getSheet(0);
    int rows = sheet.getRows();
    int clos = sheet.getColumns();
    clos = 5;
    List<ObjInfo> list = new ArrayList<ObjInfo>();
    Cell cells[][] = new Cell[clos][rows];
    ObjInfo obj = null;
    for(int r = 1;r < rows;++r){
    obj = new ObjInfo();
    for(int c = 2;c < clos;++c){
    cells[c][r] = sheet.getCell(c,r);
    if(cells != null){
    switch(c){
    case 2:
    obj.setName(cells[c][r].getContents());
    break;
    case 3:
    obj.setTel(cells[c][r].getContents());
    break;
    case 4:
    obj.setAddress(cells[c][r].getContents());
    break;
    case 5:
    obj.setType(cells[c][r].getContents());
    break;
    }
    }
    list.add(obj);
    }
    }
    workbook.close();
    return list;
    }
    public int write(List<ObjInfo> list) throws SQLException{
    Connection con = null;
    PreparedStatement ps = null;
    int i = 0;
    try{
    // 1 加载class
    Class.forName(CConnectInfo.driver);
    // 2 创建连接
    con = DriverManager.getConnection(CConnectInfo.url,CConnectInfo.username,CConnectInfo.password);
    String sql = "insert into consumer(name,cellPhone1,address,productId) values(?,?,?,?,?,?,?)";// ?号是占位符,希望动态赋值的;
    // 3 创建preparedStatement对象
    ps = con.prepareStatement(sql);
    //4 为ps赋值 这里该如何写?
    int seq = 1;
    for(ObjInfo obj : list){
    seq = 1;
    ps.setString(seq++,obj.getName());
    ps.setString(seq++,obj.getTel());
    ps.setString(seq++,obj.getAddress());
    ps.setString(seq++,obj.getType());
    i = ps.executeUpdate();
    }
    }catch(ClassNotFoundException e){
    e.printStackTrace();
    }finally{
    if(con != null) con.close();
    con = null;
    if(ps != null) ps.close();
    ps = null;
    }
    return i;
    }
            public static void main(String[] args) throws Exception{
    JxlReadTest jrt = new JxlReadTest();
    File file = new File("d:/out.xls");
    List<ObjInfo> list = jrt.getContentList(file);
    jrt.write(list);
    }
    class ObjInfo{
    private String name;
    private String tel;
    private String address;
    private String type;
    public String getName(){
    return name;
    }
    public void setName(String name){
    this.name = name;
    }
    public String getTel(){
    return tel;
    }
    public void setTel(String tel){
    this.tel = tel;
    }
    public String getAddress(){
    return address;
    }
    public void setAddress(String address){
    this.address = address;
    }
    public String getType(){
    return type;
    }
    public void setType(String type){
    this.type = type;
    }
    }