多张表struts2导入excel数据 其中一张表是一对多 ,故excel横向cell长度可变

来源:互联网 发布:php sql错误提示 编辑:程序博客网 时间:2024/04/30 04:17


注意图片中圈出部分是同一张表横向显示的

话不多说 直接代码


public String importExcle() {

        TEmployee employee = (TEmployee) request.getSession().getAttribute(
                "employee");
        // 声明数据流
        InputStream is = null;
        if(employee!=null){
        if (excel != null) {
            //如果出错记住导入到的行和列
            int hang=0;
            try {
                
                // 解析excel 2007 版本文件
                is = new FileInputStream(excel);
                XSSFWorkbook work = new XSSFWorkbook(is);
                XSSFSheet sheet = work.getSheetAt(0);
                int rows = sheet.getPhysicalNumberOfRows();
                if (sheet != null) {
                    System.out.println("解析excel 2007版本文件");
                    /*--------------------------------------------------------------------------------------------------------------------------*/
                    // 导入啊 2007版
                    /*--------------------------------------------------------------------------------------------------------------------------*/
                    // 循环表的行,从第二行开始
                    for (int i = 1; i < rows; i++) {
                        hang=i;
                        XSSFRow row = sheet.getRow(i);
                        if (row == null) {// 判断是否为空
                            continue;
                        }
                          TImportantCustomer ic = new TImportantCustomer();
                             if (row.getCell((short)(0)) != null) {ic.setCompanyName(row.getCell((short) 0).getStringCellValue());}
                             if (row.getCell((short)(1)) != null) {ic.setLinkMan(row.getCell((short) 1).getStringCellValue());}
                             if (row.getCell((short)(2)) != null) { ic.setTell(row.getCell((short) 2).getStringCellValue());}
                             if (row.getCell((short)(3)) != null) {ic.setTelephone(String.valueOf(new BigDecimal(row.getCell((short) 3).getNumericCellValue()).toPlainString()));}
                             if (row.getCell((short)(4)) != null) { ic.setEmail(row.getCell((short) 4).getStringCellValue());}
                             if (row.getCell((short)(5)) != null) { ic.setQq(String.valueOf(new BigDecimal(row.getCell((short) 5).getNumericCellValue()).toPlainString()));}
                             if (row.getCell((short)(6)) != null) { ic.setCompanyDetail(row.getCell((short) 6).getStringCellValue());}
                                   // 重要客户信息保存
                                    ic.setCreateDate(new Date());
                                    importantCustomerBiz.addicustomer(ic);
                                TNextFollowDetail nf = new TNextFollowDetail();
                                 if (row.getCell((short)(7)) != null) { nf.setCustomerLevel((int)row.getCell((short) 7).getNumericCellValue());}
                                 if (row.getCell((short)(8)) != null) { nf.setMainRequire(row.getCell((short) 8).getStringCellValue());}
                                 if (row.getCell((short)(9)) != null) { nf.setNextFollowDetail(row.getCell((short) 9).getStringCellValue());}
                                 if (row.getCell((short)(10)) != null) {  nf.setNextFollowDate(row.getCell((short) 10).getDateCellValue());}
                                // 下次跟进保存
                                nf.setIcId(ic.getId());
                                nextFollowDetailBiz.addNextFollowDetail(nf);// 上次跟进时间未能填写(在导入历史记录时填写(查询最近时间))
                                TCustomerFollowDetail icf = null;
                                //计算循环次数
                                int k= (row.getLastCellNum()-10)/3;
                            for (int j = 0; j <k; j++) {
                                  icf=new TCustomerFollowDetail();
                                    if (row.getCell((short)(3*j+11))!= null) {
                                        icf.setFollowMan(row.getCell((short)(3*j+11)).getStringCellValue());
                                        }
                                    if (row.getCell((short)(3*j+12)) != null) {
                                        icf.setFollowDate(row.getCell((short)(3*j+12)).getDateCellValue());
                                        }
                                    if (row.getCell((short)(3*j+13)) != null) {
                                        icf.setFollowContent(row.getCell((short)(3*j+13)).getStringCellValue());
                                        }
                                if(StringUtils.hasText(icf.getFollowMan())||StringUtils.hasText(icf.getFollowContent())||icf.getFollowDate()!=null){
                                    // 保存历史跟进
                                    icf.setIcId(ic.getId());
                                    customerFollowDetailBiz.addFollow(icf);
                                    //添加上次跟进时间
                                    TNextFollowDetail next=nextFollowDetailBiz.findByProperty(ic.getId());
                                    //获取上次插入最近的时间
                                    Date maxdate=customerFollowDetailBiz.findLastFollowDetailByDate(ic.getId());
                                    next.setLastFollowDate(maxdate);
                                    nextFollowDetailBiz.updateNextFollowDetail(next);
                                }
                              }
                            }
                    request.setAttribute("message", "导入成功(=^_^=)");
                }
            } catch (Exception e) {
                /*--------------------------------------------------------------------------------------------------------------------------*/
                // 导入啊 2003版
                /*--------------------------------------------------------------------------------------------------------------------------*/
                try {
                    // 解析excel 2003 版本文件
                    is = new FileInputStream(excel);
                    HSSFWorkbook work = new HSSFWorkbook(is);
                    HSSFSheet sheet = work.getSheetAt(0);
                    if (sheet != null) {
                        System.out.println("解析excel 2003 版本文件");
                        int rows = sheet.getPhysicalNumberOfRows();
                        // 循环表的行,从第二行开始
                        for (int i = 1; i < rows; i++) {
                            hang=i;
                            HSSFRow row = sheet.getRow(i);
                            if (row == null) {// 判断是否为空
                                continue;
                            }
                               TImportantCustomer ic = new TImportantCustomer();
                             if (row.getCell((short)(0)) != null) {ic.setCompanyName(row.getCell((short) 0).getStringCellValue());}
                             if (row.getCell((short)(1)) != null) {ic.setLinkMan(row.getCell((short) 1).getStringCellValue());}
                             if (row.getCell((short)(2)) != null) { ic.setTell(row.getCell((short) 2).getStringCellValue());}
                             if (row.getCell((short)(3)) != null) {ic.setTelephone(String.valueOf(new BigDecimal(row.getCell((short) 3).getNumericCellValue()).toPlainString()));}
                             if (row.getCell((short)(4)) != null) { ic.setEmail(row.getCell((short) 4).getStringCellValue());}
                             if (row.getCell((short)(5)) != null) { ic.setQq(String.valueOf(new BigDecimal(row.getCell((short) 5).getNumericCellValue()).toPlainString()));}
                             if (row.getCell((short)(6)) != null) { ic.setCompanyDetail(row.getCell((short) 6).getStringCellValue());}
                                   // 重要客户信息保存
                                    ic.setCreateDate(new Date());
                                    importantCustomerBiz.addicustomer(ic);
                                TNextFollowDetail nf = new TNextFollowDetail();
                                 if (row.getCell((short)(7)) != null) { nf.setCustomerLevel((int)row.getCell((short) 7).getNumericCellValue());}
                                 if (row.getCell((short)(8)) != null) { nf.setMainRequire(row.getCell((short) 8).getStringCellValue());}
                                 if (row.getCell((short)(9)) != null) { nf.setNextFollowDetail(row.getCell((short) 9).getStringCellValue());}
                                 if (row.getCell((short)(10)) != null) {  nf.setNextFollowDate(row.getCell((short) 10).getDateCellValue());}
                                // 下次跟进保存
                                nf.setIcId(ic.getId());
                                nextFollowDetailBiz.addNextFollowDetail(nf);// 上次跟进时间未能填写(在导入历史记录时填写(查询最近时间))
                                TCustomerFollowDetail icf = null;
                                //计算循环次数
                                int k= (row.getLastCellNum()-10)/3;
                            for (int j = 0; j <k; j++) {
                                  icf=new TCustomerFollowDetail();
                                    if (row.getCell((short)(3*j+11))!= null) {
                                        icf.setFollowMan(row.getCell((short)(3*j+11)).getStringCellValue());
                                        }
                                    if (row.getCell((short)(3*j+12)) != null) {
                                        icf.setFollowDate(row.getCell((short)(3*j+12)).getDateCellValue());
                                        }
                                    if (row.getCell((short)(3*j+13)) != null) {
                                        icf.setFollowContent(row.getCell((short)(3*j+13)).getStringCellValue());
                                        }
                                if(StringUtils.hasText(icf.getFollowMan())||StringUtils.hasText(icf.getFollowContent())||icf.getFollowDate()!=null){
                                    // 保存历史跟进
                                    icf.setIcId(ic.getId());
                                    customerFollowDetailBiz.addFollow(icf);
                                    //添加上次跟进时间
                                    TNextFollowDetail next=nextFollowDetailBiz.findByProperty(ic.getId());
                                    //获取上次插入最近的时间
                                    Date maxdate=customerFollowDetailBiz.findLastFollowDetailByDate(ic.getId());
                                    next.setLastFollowDate(maxdate);
                                    nextFollowDetailBiz.updateNextFollowDetail(next);
                                }
                              }
                            }
                        request.setAttribute("message", "导入成功(=^_^=)");
                        }
                } catch (Exception e1) {
                    request.setAttribute("message", "第"+hang+"行之后导入失败!");
                    e1.printStackTrace();
                }
            }
        }
        }else{
            request.setAttribute("message", "登录超时!");
        }
        // 查询时对象中含有值对查询有误
        if (icustomer != null) {
            icustomer = null;
        }

        return icustomerList();

    }






0 0