日期:2014-05-17  浏览次数:20828 次

poi导出excel循环效率低下的问题
Java code

//获取需要的用户信息  
lu = comm.findalluser();
  for (User u : lu) {
//查出所有客户信息
     la = salesaccountdao.listpallAccount(u.getUserid());
     if (la.size() > 0) {
        accountLists.addAll(la);
     }
  }
//将查处的所有客户信息导出成excel[color=#FF0000](一下循环费时)[/color]
  int i=0,j=0,size = accountLists.size();
            for (i = 1; i < size; i++) {
                // 获得这个sheet的第i行
                row = sheet1.createRow(i);
                for (j = 0; j < 12; j++) {
                    // 设置每个sheet每一行的宽度,自动,根据需求自行确定
                    sheet1.autoSizeColumn(j + 1, true);
                    // 获得这一行的每j列
                    cell = row.createCell(j);
                    switch (j) {
                    case 0:
                        cell.setCellValue(accountLists.get(i).getName());
                        break;
                    case 1:
                        cell.setCellValue(accountLists.get(i).getAddress());
                        break;
                    case 2:
                        cell.setCellValue(accountLists.get(i).getCity());
                        break;
                    case 3:
                        cell.setCellValue(accountLists.get(i).getCreatetime());
                        break;
                    case 4:
                        cell.setCellValue(accountLists.get(i).getCustomertype());
                        break;
                    case 5:
                        cell.setCellValue(accountLists.get(i).getDescription());
                        break;
                    case 6:
                        cell.setCellValue(accountLists.get(i).getProvince());
                        break;
                    case 7:
                        cell.setCellValue(accountLists.get(i).getStatecode());
                        break;
                    case 8:
                        cell.setCellValue(accountLists.get(i).getFax());
                        break;
                    case 9:
                        cell.setCellValue(accountLists.get(i).getIndustrycode());
                        break;
                    case 10:
                        cell.setCellValue(accountLists.get(i).getRemarks());
                        break;
                    case 11:
                        cell.setCellValue(accountLists.get(i).getWebsiteurl());
                        break;
                    case 12:
                        cell.setCellValue(accountLists.get(i).getTelephone());
                        break;
                    case 13:
                        cell.setCellValue(accountLists.get(i).getUserByOwnerid().getName());
                        break;
                    }
                }
            }
[color=#FF0000]//就在生成excel的嵌套循环中,浪费了大量时间,总的600条数据,打印时间却是110秒钟,请高手指点一二,不胜感激!![/color]



------解决方案--------------------
Java code

int i=0,j=0,size = accountLists.size();
        for (User user : accountLists) {             
            // 获得这个sheet的第i行
            row = sheet1.createRow(i);
            for (j = 0; j < 12; j++) {
                // 获得这一行的每j列
                cell = row.createCell(j);
                switch (j) {
                case 0:
                    cell.setCellValue(user.getName());
                    break;
                case 1:
                    cell.setCellValue(user.getAddress());
                    break;
                case 2:
                    cell.setCellValue(user.getCity());
                    break;
                case 3:
                    cell.setCellValue(user.getCreatetime());
                    break;
                case 4:
                    cell.setCellValue(user.getCustomertype());
                    break;
                case 5:
                    cell.setCellValue(user.getDescription());
                    br