導入Excel的數據

HTML代碼:

    <div class="toolbar_btn top5">
        <form id="uploadForm" action="${ctx}/cruiseRoom/showExcelInfo.json"
            method="post" enctype="multipart/form-data">
            <table>
                <tr>
                    <td><input type="file" id="importExcel" name="file"></td>
                    <td><input type="button" value="導入Excel" id="btnImport"
                        onclick="upLoad()" class="l-button l-button-submit" /></td>
                        <td width="20%"></td>
                        <td><input type="button" value="模板下載" id="downFile" class="l-button l-button-submit" /> </td>
                    <%-- <td><a value="模板下載" src="${filesvrUrl }/filesvr/downLoad?id='cruiseRoom001'" class="l-button l-button-submit"></a></td> --%>

                        <!-- <td width="20%">
                        <input type="button" value="模板上傳" id="upLoadFile" onclick="upLoadFile()" class="l-button l-button-submit" /> </td> -->
                </tr>
            </table>
        </form>
    </div>

js代碼:

  function upLoad(){
        var txtIn = document.getElementById("importExcel");
        var fileName=txtIn.files[0].name;
        var reg=/^(.)+\.(xls|xlsx)$/i;
           if (!reg.test(fileName)) {
              $.ligerDialog.warn("文件格式不對!");
              return;
           }
        var formData = new FormData($( "#uploadForm" )[0]); 
        $.ajax({
            url: "${ctx}/cruiseRoom/showExcelInfo.json",
            contentType:"application/json",
            type: 'post',  
            data: formData,  
            async: false,  
            cache: false,  
            contentType: false,  
            processData: false, 
            success: function(data){
                if(data.success){
                    var list=data.content;
                    loadData(list);
                    flag=true;
                }
             }
        })
    }
      function downFile(){
          var url="${url}";
          var knName="${knName}";
          window.location.href = ctx + "/visa/download?url=" + url + "&knName=" + knName;
      }

controller的代碼:

@RequestMapping(value = "/showExcelInfo.json", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult importExcel(@RequestParam(value = "file", required = false)MultipartFile file){
        AjaxResult ajaxResult = new AjaxResult(true, "批量導入成功!");
        List<CruiseRoom> list = null;
        try {
            list = new ArrayList<CruiseRoom>();
            InputStream input = file.getInputStream();// 按此可以得到流
            Workbook wb = null; 
            int i = 0; int m = 0; String info = "";
            //默認xls
            try{
            wb = new HSSFWorkbook(input);
            }catch(Exception e){
                //如果爲空,則用xlsx再試一次
                input = file.getInputStream();
                wb = new XSSFWorkbook(input);
            }
            //如果爲空,則格式不對
            if(wb==null){
                throw new BusinessException("EXCEL 格式不對");
            }
            Sheet sheet = wb.getSheetAt(0); // 獲得第一個表單
            Map<String,String> typeMap = dictToMapService.getMapByDictType(DictType.CRUISE_ROOM_TYPE.getKey());
            Iterator<Row> rows = sheet.rowIterator(); // 獲得第一個表單的迭代器
            while (rows.hasNext()) {
                Row row = rows.next(); // 獲得行數據
                i = row.getRowNum(); // 獲得行號從0開始
                Iterator<Cell> cells = row.cellIterator(); // 獲得第一行的迭代器
                if (i > 0) {
                    CruiseRoom vo = new CruiseRoom();
                    while (cells.hasNext()) {
                        Cell cell = cells.next();
                        m = cell.getColumnIndex();// 獲得列號從0開始
           switch (cell.getCellType()) { 
                        // 根據cell中的類型來輸出數據
                        case HSSFCell.CELL_TYPE_NUMERIC:
                            info = (int)cell.getNumericCellValue()+"";
                            break;
                        case HSSFCell.CELL_TYPE_STRING:
                            info = cell.getStringCellValue() + "";
                            break;
                        case HSSFCell.CELL_TYPE_BOOLEAN:
                            info = cell.getBooleanCellValue() + "";
                            break;
                        case HSSFCell.CELL_TYPE_FORMULA:
                            info = cell.getCellFormula() + "";
                            break;
                        default:
                            info = "";
                            break;
                        }

            switch (m) {
                        case 0:
                            /*Set set=typeMap.entrySet();  
                            Iterator it=set.iterator();  
                            while(it.hasNext()) {  
                                  Map.Entry entry=(Map.Entry)it.next();  
                                  if(entry.getValue().equals(info.trim())) { 
                                      vo.setType(info.trim());
                                   }
                                }*/
                            vo.setName(info.trim());
                            break;
                        case 1:
                            Set set=typeMap.entrySet();  
                            Iterator it=set.iterator();  
                            while(it.hasNext()) {  
                                  Map.Entry entry=(Map.Entry)it.next();  
                                  if(entry.getValue().equals(info.trim())) { 
                                      vo.setType(info.trim());
                                   }
                                }
                            break;
                        case 2:
                            vo.setWindows(info.trim());
                            break;
                        case 3:
                            vo.setArea(info.trim());
                            break;
/*                      case 4:
                            vo.setMinGuestNum(Integer.parseInt(info.trim()));
                            break;*/
                        case 4:
                            vo.setMaxGuestNum(Integer.parseInt(info.trim()));
                            break;
                        case 5:
                            vo.setFloor(info.trim());
                            break;
                        case 6:
                            vo.setFacility(info.trim());
                            break;
                        case 7:
                            vo.setStatus((DataStatusType.AVAILABLY.getValue()));
                            break;
                        case 8:
                            vo.setRemark(info.trim());
                            break;
                        default:
                            break;
                        }
                    }
                    list.add(vo);
                }
            }
            ajaxResult.setContent(list);
        } catch (BusinessException e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage(e.getMessage());
            logger.error("批量導入出錯: "+e.getMessage(), e);
        } catch (IOException e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage(e.getMessage());
            logger.error("批量導入出錯: "+e.getMessage(), e);
        }
        return ajaxResult;

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章