mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 08:13:44 +08:00 
			
		
		
		
	update 增加excel导入样例
This commit is contained in:
		@@ -1,27 +1,31 @@
 | 
			
		||||
package com.ruoyi.demo.controller;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.bean.BeanUtil;
 | 
			
		||||
import com.ruoyi.common.annotation.Log;
 | 
			
		||||
import com.ruoyi.common.annotation.RepeatSubmit;
 | 
			
		||||
import com.ruoyi.common.core.controller.BaseController;
 | 
			
		||||
import com.ruoyi.common.core.domain.AjaxResult;
 | 
			
		||||
import com.ruoyi.common.core.domain.entity.SysUser;
 | 
			
		||||
import com.ruoyi.common.core.page.TableDataInfo;
 | 
			
		||||
import com.ruoyi.common.core.validate.AddGroup;
 | 
			
		||||
import com.ruoyi.common.core.validate.EditGroup;
 | 
			
		||||
import com.ruoyi.common.core.validate.QueryGroup;
 | 
			
		||||
import com.ruoyi.common.enums.BusinessType;
 | 
			
		||||
import com.ruoyi.common.utils.ValidatorUtils;
 | 
			
		||||
import com.ruoyi.common.utils.poi.ExcelResult;
 | 
			
		||||
import com.ruoyi.common.utils.poi.ExcelUtil;
 | 
			
		||||
import com.ruoyi.demo.domain.TestDemo;
 | 
			
		||||
import com.ruoyi.demo.domain.bo.TestDemoBo;
 | 
			
		||||
import com.ruoyi.demo.domain.bo.TestDemoImportVo;
 | 
			
		||||
import com.ruoyi.demo.domain.vo.TestDemoVo;
 | 
			
		||||
import com.ruoyi.demo.service.ITestDemoService;
 | 
			
		||||
import io.swagger.annotations.Api;
 | 
			
		||||
import io.swagger.annotations.ApiOperation;
 | 
			
		||||
import io.swagger.annotations.ApiParam;
 | 
			
		||||
import io.swagger.annotations.*;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.security.access.prepost.PreAuthorize;
 | 
			
		||||
import org.springframework.validation.annotation.Validated;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
import org.springframework.web.multipart.MultipartFile;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletResponse;
 | 
			
		||||
import javax.validation.constraints.NotEmpty;
 | 
			
		||||
@@ -65,6 +69,21 @@ public class TestDemoController extends BaseController {
 | 
			
		||||
        return iTestDemoService.customPageList(bo);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ApiOperation("导入测试单表")
 | 
			
		||||
    @ApiImplicitParams({
 | 
			
		||||
        @ApiImplicitParam(name = "file", value = "导入文件", dataType = "java.io.File", required = true),
 | 
			
		||||
    })
 | 
			
		||||
    @Log(title = "测试单表", businessType = BusinessType.IMPORT)
 | 
			
		||||
    @PreAuthorize("@ss.hasPermi('demo:demo:import')")
 | 
			
		||||
    @PostMapping("/importData")
 | 
			
		||||
    public AjaxResult<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
 | 
			
		||||
        ExcelResult<TestDemoImportVo> excelResult = ExcelUtil.importExcel(file.getInputStream(), TestDemoImportVo.class, true, true);
 | 
			
		||||
        List<TestDemoImportVo> testDemoImportVoList = excelResult.getList();
 | 
			
		||||
        List<TestDemo> TestDemoList = BeanUtil.copyToList(testDemoImportVoList, TestDemo.class);
 | 
			
		||||
        iTestDemoService.saveAll(TestDemoList);
 | 
			
		||||
        return AjaxResult.success(excelResult.getAnalysis());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 导出测试单表列表
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,66 @@
 | 
			
		||||
package com.ruoyi.demo.domain.bo;
 | 
			
		||||
 | 
			
		||||
import com.alibaba.excel.annotation.ExcelProperty;
 | 
			
		||||
import com.ruoyi.common.core.domain.BaseEntity;
 | 
			
		||||
import com.ruoyi.common.core.validate.AddGroup;
 | 
			
		||||
import com.ruoyi.common.core.validate.EditGroup;
 | 
			
		||||
import io.swagger.annotations.ApiModel;
 | 
			
		||||
import io.swagger.annotations.ApiModelProperty;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
 | 
			
		||||
import javax.validation.constraints.NotBlank;
 | 
			
		||||
import javax.validation.constraints.NotNull;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 测试单表业务对象 test_demo
 | 
			
		||||
 *
 | 
			
		||||
 * @author Lion Li
 | 
			
		||||
 * @date 2021-07-26
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
@ApiModel("测试单表业务对象")
 | 
			
		||||
public class TestDemoImportVo {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 部门id
 | 
			
		||||
     */
 | 
			
		||||
	@ApiModelProperty("部门id")
 | 
			
		||||
    @NotNull(message = "部门id不能为空")
 | 
			
		||||
    @ExcelProperty(value = "部门id")
 | 
			
		||||
    private Long deptId;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 用户id
 | 
			
		||||
     */
 | 
			
		||||
	@ApiModelProperty("用户id")
 | 
			
		||||
    @NotNull(message = "用户id不能为空")
 | 
			
		||||
    @ExcelProperty(value = "用户id")
 | 
			
		||||
    private Long userId;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 排序号
 | 
			
		||||
     */
 | 
			
		||||
	@ApiModelProperty("排序号")
 | 
			
		||||
    @NotNull(message = "排序号不能为空")
 | 
			
		||||
    @ExcelProperty(value = "排序号")
 | 
			
		||||
    private Long orderNum;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * key键
 | 
			
		||||
     */
 | 
			
		||||
	@ApiModelProperty("key键")
 | 
			
		||||
    @NotBlank(message = "key键不能为空")
 | 
			
		||||
    @ExcelProperty(value = "key键")
 | 
			
		||||
    private String testKey;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 值
 | 
			
		||||
     */
 | 
			
		||||
	@ApiModelProperty("值")
 | 
			
		||||
    @NotBlank(message = "值不能为空")
 | 
			
		||||
    @ExcelProperty(value = "值")
 | 
			
		||||
    private String value;
 | 
			
		||||
}
 | 
			
		||||
@@ -71,6 +71,16 @@
 | 
			
		||||
          v-hasPermi="['demo:demo:remove']"
 | 
			
		||||
        >删除</el-button>
 | 
			
		||||
      </el-col>
 | 
			
		||||
      <el-col :span="1.5">
 | 
			
		||||
        <el-button
 | 
			
		||||
          type="info"
 | 
			
		||||
          plain
 | 
			
		||||
          icon="el-icon-upload2"
 | 
			
		||||
          size="mini"
 | 
			
		||||
          @click="handleImport"
 | 
			
		||||
          v-hasPermi="['demo:demo:import']"
 | 
			
		||||
        >导入</el-button>
 | 
			
		||||
      </el-col>
 | 
			
		||||
      <el-col :span="1.5">
 | 
			
		||||
        <el-button
 | 
			
		||||
          type="warning"
 | 
			
		||||
@@ -164,11 +174,34 @@
 | 
			
		||||
        <el-button @click="cancel">取 消</el-button>
 | 
			
		||||
      </div>
 | 
			
		||||
    </el-dialog>
 | 
			
		||||
    <!-- 用户导入对话框 -->
 | 
			
		||||
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
 | 
			
		||||
      <el-upload
 | 
			
		||||
        ref="upload"
 | 
			
		||||
        :limit="1"
 | 
			
		||||
        accept=".xlsx, .xls"
 | 
			
		||||
        :headers="upload.headers"
 | 
			
		||||
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
 | 
			
		||||
        :disabled="upload.isUploading"
 | 
			
		||||
        :on-progress="handleFileUploadProgress"
 | 
			
		||||
        :on-success="handleFileSuccess"
 | 
			
		||||
        :auto-upload="false"
 | 
			
		||||
        drag
 | 
			
		||||
      >
 | 
			
		||||
        <i class="el-icon-upload"></i>
 | 
			
		||||
        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
 | 
			
		||||
      </el-upload>
 | 
			
		||||
      <div slot="footer" class="dialog-footer">
 | 
			
		||||
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
 | 
			
		||||
        <el-button @click="upload.open = false">取 消</el-button>
 | 
			
		||||
      </div>
 | 
			
		||||
    </el-dialog>
 | 
			
		||||
  </div>
 | 
			
		||||
</template>
 | 
			
		||||
 | 
			
		||||
<script>
 | 
			
		||||
import { listDemo, pageDemo, getDemo, delDemo, addDemo, updateDemo } from "@/api/demo/demo";
 | 
			
		||||
import {getToken} from "@/utils/auth";
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
  name: "Demo",
 | 
			
		||||
@@ -198,6 +231,19 @@ export default {
 | 
			
		||||
      open: false,
 | 
			
		||||
      // 创建时间时间范围
 | 
			
		||||
      daterangeCreateTime: [],
 | 
			
		||||
      // 用户导入参数
 | 
			
		||||
      upload: {
 | 
			
		||||
        // 是否显示弹出层(用户导入)
 | 
			
		||||
        open: false,
 | 
			
		||||
        // 弹出层标题(用户导入)
 | 
			
		||||
        title: "",
 | 
			
		||||
        // 是否禁用上传
 | 
			
		||||
        isUploading: false,
 | 
			
		||||
        // 设置上传的请求头部
 | 
			
		||||
        headers: { Authorization: "Bearer " + getToken() },
 | 
			
		||||
        // 上传的地址
 | 
			
		||||
        url: process.env.VUE_APP_BASE_API + "/demo/demo/importData"
 | 
			
		||||
      },
 | 
			
		||||
      // 查询参数
 | 
			
		||||
      queryParams: {
 | 
			
		||||
        pageNum: 1,
 | 
			
		||||
@@ -353,11 +399,32 @@ export default {
 | 
			
		||||
        this.loading = false;
 | 
			
		||||
      });
 | 
			
		||||
    },
 | 
			
		||||
    /** 导入按钮操作 */
 | 
			
		||||
    handleImport() {
 | 
			
		||||
      this.upload.title = "用户导入";
 | 
			
		||||
      this.upload.open = true;
 | 
			
		||||
    },
 | 
			
		||||
    /** 导出按钮操作 */
 | 
			
		||||
    handleExport() {
 | 
			
		||||
      this.download('demo/demo/export', {
 | 
			
		||||
        ...this.queryParams
 | 
			
		||||
      }, `demo_${new Date().getTime()}.xlsx`)
 | 
			
		||||
    },
 | 
			
		||||
    // 文件上传中处理
 | 
			
		||||
    handleFileUploadProgress(event, file, fileList) {
 | 
			
		||||
      this.upload.isUploading = true;
 | 
			
		||||
    },
 | 
			
		||||
    // 文件上传成功处理
 | 
			
		||||
    handleFileSuccess(response, file, fileList) {
 | 
			
		||||
      this.upload.open = false;
 | 
			
		||||
      this.upload.isUploading = false;
 | 
			
		||||
      this.$refs.upload.clearFiles();
 | 
			
		||||
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
 | 
			
		||||
      this.getList();
 | 
			
		||||
    },
 | 
			
		||||
    // 提交上传文件
 | 
			
		||||
    submitFileForm() {
 | 
			
		||||
      this.$refs.upload.submit();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user