zoukankan      html  css  js  c++  java
  • 继上篇后的Excel批量数据导入

    上篇Excel动态生成模板,此篇将借用此模板进行Excel数据的批量导入。

    说明:由于数据库中部分数据储存的是编码或者Id,因此,这里就需要用到上篇中的全局数据,判断是否有数据,有数据直接使用,没有数据,再去数据库查询,减少资源消耗;

      1 public String add(HttpServletRequest request, HttpServletResponse response) {
      2         Map<String, Object> params = new HashMap<String, Object>();
      3         List<MultipartFile> files = ((MultipartRequest) request).getFiles("file");
      4         for (MultipartFile filePic : files) {
      5             if(filePic.isEmpty() && "".equals(request.getParameter("PARENTID"))){
      6                 //null;
      7             }else if (filePic.isEmpty()) {
      8                 boolean ok = getService().insert(params);
      9                 if (!ok) {
     10                     //false;
     11                 } else {
     12                     //true;
     13                 }
     14             } else {
     15                 List<MultipartFile> filesList = ((MultipartRequest) request).getFiles("file");
     16                 for(MultipartFile file : filesList){
     17                     if (file.isEmpty())
     18                         continue;
     19                     //开始解析文件
     20                     try {
     21                         InputStream is = file.getInputStream();
     22                         Workbook wb = new HSSFWorkbook(is);  //暂只能解析97-2003版本(.xls),缺少03以上版本(.xlsx)的jar包
     23                         //获取第一个sheet
     24                         Sheet sheet = wb.getSheetAt(0);
     25                         //获取最大行数
     26                         int rownum = sheet.getPhysicalNumberOfRows();
     27                         //获取第一行
     28                         Row row = sheet.getRow(0);
     29                         //获取最大列数
     30                         int colnum = row.getPhysicalNumberOfCells();
     31                         for(int i=1;i<rownum;i++){
     32                             Map<String , String > map = new LinkedHashMap<String, String>();
     33                             row = sheet.getRow(i);
     34                             if(row!=null){
     35                                 for(int j=0;j<colnum;j++){
     36                                     row.getCell(j).setCellType(HSSFCell.CELL_TYPE_STRING);//先强制转换成String类型
     37                                     String cellData = row.getCell(j).getStringCellValue();//再读取
     38                                     map.put(columns[j], cellData);
     39                                 }
     40                             }else{
     41                                 break;
     42                             }
     43                             list.add(map);
     44                         }
     45                         
     46                     } catch (IllegalStateException e) {
     47                         e.printStackTrace();
     48                     } catch (IOException e) {
     49                         e.printStackTrace();
     50                     }
     51                 }
     52                 //根据需要替换map中的K,V值,这里将商户类型,省名等替换成编码。
     53                 for(Map map :list){
     54                     if(faMerchantList.size()==0){
     55                         faMerchantList = service.findList("UU_BANK_MERCHANT.queryName", paramData);
     56                     }
     57                     if(merchantList.size()==0){
     58                         merchantList = bankMerchantTypeService.findList("UU_BANK_MERCHANTTYPE.query", paramData);
     59                     }
     60                     if(proviceLists.size()==0){
     61                         proviceLists = serviceOut.findList("UU_BANK_BANKOUTLET.selectProvices", paramData);
     62                     }
     63                     //商户类型
     64                     String entry = (String) map.get("TYPEID");
     65                     for(Object object:merchantList){
     66                         Map entryOb=(Map) object;
     67                         String Names=(String) entryOb.get("NAME");
     68                         if(null!=entry && !"".equals(entry) && entry.equals(Names)){
     69                             String Id=String.valueOf(entryOb.get("ID"));
     70                             map.put("TYPEID", Id);
     71                             break;
     72                         }
     73                     }
     74                     //商户父级id
     75                     String entryf = (String) map.get("PARENTID");
     76                     for(Object object:faMerchantList){
     77                         Map entryOb=(Map) object;
     78                         String Names=(String) entryOb.get("NAME");
     79                         if(null!=entryf && !"".equals(entryf) && entryf.equals(Names)){
     80                             String Id=String.valueOf(entryOb.get("ID"));
     81                             map.put("PARENTID", Id);
     82                             break;
     83                         }
     84                     }
     85                     //
     86                     String provi = (String) map.get("PROVICE");
     87                     String provinceId="";
     88                     for(Object object:proviceLists){
     89                         Map entryOb=(Map) object;
     90                         String Names=(String) entryOb.get("NAME");
     91                         if(null!=provi && !"".equals(provi) && provi.equals(Names)){
     92                             provinceId=String.valueOf(entryOb.get("CITYCODE"));;
     93                             map.put("PROVICE", provinceId);
     94                             break;
     95                         }
     96                     }
     97                     //
     98                     String city = (String) map.get("CITY");
     99                     if(!provinceId.isEmpty()){
    100                         String city_Code = provinceId.substring( 0, 2);
    101                         params.put("CITY_CODE", city_Code);            
    102                         params.put("CITYCODE", provinceId);
    103                     }    
    104                     params.put("NAME", city);
    105                     String cityId = serviceOut.load("UU_BANK_BANKOUTLET.selectCityId", params);
    106                     map.put("CITY", cityId);
    107                     //
    108                     String county = (String) map.get("COUNTY");
    109                     if(!cityId.isEmpty()){
    110                         String city_Code = cityId.substring( 0, 4);
    111                         params.put("CITY_CODE", city_Code);            
    112                         params.put("CITYCODE", cityId);
    113                     }    
    114                     params.put("NAME", county);
    115                     String countyId = serviceOut.load("UU_BANK_BANKOUTLET.selectCountyId", params);
    116                     map.put("COUNTY", countyId);    
    117                 }
    118                 for(Map map : list) {
    119                     boolean ok = service.insert(map);
    120                     if (!ok) {
    121                         //false;
    122                     } else {
    123                         //true;
    124                     }
    125                 }
    126             }
    127         
    128         }
    129     }

    试想:在EXCEL里面选择省、市、区的名字,自动填入省市区的编码,或者在导入数据时在Excel中使用函数替换。-----渣渣的想法。

    使用jar包poi3.7.

  • 相关阅读:
    URAL 1998 The old Padawan 二分
    URAL 1997 Those are not the droids you're looking for 二分图最大匹配
    URAL 1995 Illegal spices 贪心构造
    URAL 1993 This cheeseburger you don't need 模拟题
    URAL 1992 CVS
    URAL 1991 The battle near the swamp 水题
    Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
    Codeforces Beta Round #7 D. Palindrome Degree hash
    Codeforces Beta Round #7 C. Line Exgcd
    Codeforces Beta Round #7 B. Memory Manager 模拟题
  • 原文地址:https://www.cnblogs.com/renwangxu/p/9407118.html
Copyright © 2011-2022 走看看