excel导入到数据库

找朋友要得笔记,明天去公司写代码,先粘贴写笔记,明天修改

    //导入excel
    @RequestMapping("/upload")
    @ResponseBody
    public int detailupload(@RequestParam("file") MultipartFile uploadFile) {
        int num =0;
        FileUploadPathObject uploadPathObject;
        try {
            
            CommonsMultipartFile cf = (CommonsMultipartFile) uploadFile;
            DiskFileItem fi = (DiskFileItem) cf.getFileItem();
            File file = fi.getStoreLocation();
            InputStream is = new FileInputStream(file);
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);
            int index = 1;
            Boolean flag = true;
            while (flag) {
                Row row = sheet.getRow(index++);
                if (row == null)
                    break;
                Cell cell0row.getCell(0);
                cell0.setCellType(Cell.CELL_TYPE_STRING);
                
                Cell cell1 = row.getCell(1);
                cell1.setCellType(Cell.CELL_TYPE_STRING);
                
                Cell cell2 = row.getCell(2);
                cell2.setCellType(Cell.CELL_TYPE_STRING);
                
                Cell cell3 = row.getCell(3);
                cell3.setCellType(Cell.CELL_TYPE_NUMERIC);
                
                Cell cell4 = row.getCell(4);
                cell4.setCellType(Cell.CELL_TYPE_NUMERIC);
                
                RiskContract contract = new RiskContract();
                contract.setContractCode(cell0.getStringCellValue());
                contract.setContractName(cell1.getStringCellValue());
                contract.setExchangeName(cell2.getStringCellValue());
                contract.setContractdetail(BigDecimal.valueOf(cell3.getNumericCellValue()));
                contract.setMinchangeprice(BigDecimal.valueOf(cell4.getNumericCellValue()));
                contract.setUnit("CNY");
                contract.setVarietyCode("aaaa");
                contract.setPromptDay(new Date());
                contract.setPublicDay(new Date());
                num=riskcontractServiceImpl.addriskcontract(contract);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            
        }
        return num;
    }

前端--layui
//导入合约excel
    layui.use('upload'function(){
          var $ = layui.jquery
          ,upload = layui.upload;
          
          //指定允许上传的文件类型
          upload.render({
            elem: '#upload'
            ,url: '${path}/contract/upload/'
            ,accept: 'file' //普通文件
            ,done: function(num){
                if(num>0){
                    alert("合约导入成功");
                    RefreshPage();
                }else{
                    alert("请检测文件格式是否为xls或xlsx");
                }    
            }
          }); 
        });

前端html
 <button type="button" class="layui-btn" id="upload"><i class="layui-icon">i>导入合约button>

你可能感兴趣的:(excel导入到数据库)