springboot+element-ui+vue 导入excel并保存数据库

前端

<input ref="file" type="file" accept=".xlsx,.xls" style="display: none;" @change="uploadFile">
<el-button type="primary" icon="el-icon-download" @click="clickFile">导入数据</el-button>
clickFile() {
      this.$refs.file.dispatchEvent(new MouseEvent('click'))
    },
    // 导入数据
    uploadFile() {
      const file = this.$refs.file.files
      var extName = file[0].name.substring(file[0].name.lastIndexOf('.')).toLowerCase()
      var username = this.$store.getters.name
      if (extName === '.xlsx' || extName === '.xls') {
        var formData = new FormData()
        formData.append('file', file[0])
        formData.append('username', username)
        uploadExcel(formData).then((res) => {
          if (res.data.status !== 200) {
            this.$message.error('数据导入失败,请检擦数据格式!')
          } else {
            this.$message({
              type: 'success',
              message: '数据导入成功!'
            })
          }
        }).catch((err) => {
          this.$message.error('数据导入失败!ERR:' + err)
        })
      } else {
        this.$message.error('数据导入失败,请选择正确的xlsx模板文件')
      }
    },

后端

controller层

@PostMapping("/uploadExcel")
@ApiOperation("导入excel")
public String taskUploadExcel(@RequestParam(value = "file") MultipartFile file,@RequestParam(value = "username") String username) throws Exception{
   return taskServiceImpl.taskUploadExcel(file, username);
}

service层

@Transactional
@Override
public String taskUploadExcel(MultipartFile file, String username) throws Exception{
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String fileName = file.getOriginalFilename();
  if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
      throw new Exception("上传文件格式不正确");
  }
  List<TaskData> list = new ArrayList<>();
  try{
      InputStream is = file.getInputStream();
      HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(is));
      HSSFSheet sheet = workbook.getSheetAt(0);
      //获取多少行
      int rows = sheet.getPhysicalNumberOfRows();
      TaskData task = null;
      for (int j = 2; j < rows; j++) {
          task = new TaskData();
          //获得第 j 行
          HSSFRow row = sheet.getRow(j);
          System.out.println("row="+row+"++++++++++++++++++++++++++++++++++++++++++");

          task.setName(row.getCell(0).getStringCellValue());
          System.out.println("name="+ task.getName());

          task.setDescription(row.getCell(1).getStringCellValue());
          System.out.println("description="+ task.getDescription());

          task.setTypeid((int)row.getCell(2).getNumericCellValue());
          System.out.println("Typeid="+ task.getTypeid());

          task.setAttrid((int)row.getCell(3).getNumericCellValue());
          System.out.println("getAttrid="+ task.getAttrid());

          task.setSource(row.getCell(4).getStringCellValue());
          System.out.println("getSource="+ task.getSource());

          task.setPlantime(df.format(row.getCell(5).getDateCellValue()));
          System.out.println("getPlantime="+ task.getPlantime());

          task.setDeadlinetime(df.format(row.getCell(6).getDateCellValue()));
          System.out.println("getDeadlinetime="+ task.getDeadlinetime());

          list.add(task);
          System.out.println("总条数="+list.size());
          System.out.println("结束");
          System.out.println("退出循环");
		  taskDataMapper.uploadExcel(task);
          System.out.println("----------------");
      }
      		return gson.toJson(ok("成功",list));
      }catch (Exception e){
      		return gson.toJson(error("导入失败"+e));
   }
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xx1433251330

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值