1. Backend
@Log(title = "Bus Stop", businessType = BusinessType.IMPORT) @PreAuthorize("@ss.hasPermi('busStop:busStop:import')") @PostMapping("/importData") public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {<!-- --> ExcelUtil<BusStop> util = new ExcelUtil<BusStop>(BusStop.class); List<BusStop> busStopList = util.importExcel(file.getInputStream()); LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); String operName = loginUser.getUsername(); String message = busStopService.importUser(busStopList, updateSupport, operName); return AjaxResult.success(message); } @GetMapping("/importTemplate") public AjaxResult importTemplate() {<!-- --> ExcelUtil<BusStop> util = new ExcelUtil<BusStop>(BusStop.class); return util.importTemplateExcel("Bus stop data"); }
in the implementation class
/** * Import bus stop information * @param busStopList * @param isUpdateSupport * @param operName * @return */ @Override public String importUser(List<BusStop> busStopList, boolean isUpdateSupport, String operName) {<!-- --> if (StringUtils.isNull(busStopList) || busStopList.size() == 0) {<!-- --> throw new CustomException("The imported bus stop data cannot be empty!"); } int successNum = 0; int failureNum = 0; StringBuilder successMsg = new StringBuilder(); StringBuilder failureMsg = new StringBuilder(); for (BusStop busStop : busStopList) {<!-- --> try {<!-- --> //Verify whether this bus stop sign exists SysUser u = busStopMapper.selectBusStopByWfcId(busStop.getWfcId()); if (StringUtils.isNull(u)) {<!-- --> busStop.setCreateBy(operName); this.insertBusStop(busStop); successNum + + ; successMsg.append("<br/>" + successNum + ", stop sign " + busStop.getWfcId() + " Import successful"); } else if (isUpdateSupport) {<!-- --> busStop.setUpdateBy(operName); this.updateBusStop(busStop); successNum + + ; successMsg.append("<br/>" + successNum + ", stop sign " + busStop.getWfcId() + " Update successful"); } else {<!-- --> failureNum + + ; failureMsg.append("<br/>" + failureNum + ", stop sign " + busStop.getWfcId() + " already exists"); } } catch (Exception e) {<!-- --> failureNum + + ; String msg = "<br/>" + failureNum + ", stop sign " + busStop.getWfcId() + " Import failed: "; failureMsg.append(msg + e.getMessage()); //log.error(msg, e); } } if (failureNum > 0) {<!-- --> failureMsg.insert(0, "Sorry, the import failed! A total of " + failureNum + " data formats are incorrect, the error is as follows: "); throw new CustomException(failureMsg.toString()); } else {<!-- --> successMsg.insert(0, "Congratulations, all the data has been imported successfully! There are " + successNum + " items in total. The data is as follows: "); } return successMsg.toString(); }
2. Front end
data return
//User import parameters upload: {<!-- --> // Whether to display the pop-up layer (user import) open: false, // Pop-up layer title (imported by user) title: "", // Whether to disable uploading isUploading: false, // Whether to update existing station sign data updateSupport: 0, //Set the upload request header headers: {<!-- --> Authorization: "Bearer " + getToken() }, //Upload address url: process.env.VUE_APP_BASE_API + "/busStop/busStop/importData" },
placed in methods
/** Import button operation */ handleImport() {<!-- --> this.upload.title = "Site sign import"; this.upload.open = true; }, /** Download template operation */ importTemplate() {<!-- --> importTemplate().then(response => {<!-- --> this.download(response.msg); }); }, //Processing file upload handleFileUploadProgress(event, file, fileList) {<!-- --> this.upload.isUploading = true; }, // File upload successfully processed handleFileSuccess(response, file, fileList) {<!-- --> this.upload.open = false; this.upload.isUploading = false; this.$refs.upload.clearFiles(); this.$alert(response.msg, "Import results", {<!-- --> dangerouslyUseHTMLString: true }); this.getList(); }, //Submit the uploaded file submitFileForm() {<!-- --> this.$refs.upload.submit(); }
placed in temple
<!-- User import dialog box --> <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"> Drag files here, or <em>Click to upload</em> </div> <div class="el-upload__tip" slot="tip"> <el-checkbox v-model="upload.updateSupport" />Whether to update existing user data <el-link type="info" style="font-size:12px" @click="importTemplate">Download template</el-link> </div> <div class="el-upload__tip" style="color:red" slot="tip">Tip: Only "xls" or "xlsx" format files are allowed to be imported! </div> </el-upload> <div slot="footer" class="dialog-footer"> <el-button type="primary" @click="submitFileForm">Confirm</el-button> <el-button @click="upload.open = false">Cancel</el-button> </div> </el-dialog>
placed in el-row
<el-col :span="1.5"> <el-button type="info" icon="el-icon-upload2" size="mini" @click="handleImport" v-hasPermi="['busStop:busStop:import']" >Import</el-button> </el-col>