赞
踩
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
/** * 导入 Excel 数据 * * @param file 你要导入的 Excel 文件 * @return * @throws IOException */ @PostMapping("/import") public RespBean importData(MultipartFile file) throws IOException { // 1.自定义一个工具类拿到要解析的文件并解析成要存储的数据 List<Employee> list = POIUtils.excel2Employee(file); // 2.遍历输出你解析的数据格式是否正确 for (Employee employee : list) { System.out.println(employee.toString()); } // 3.进行数据库添加操作 if (employeeService.addEmp(list) == 1) { return RespBean.ok("上传成功!"); } return RespBean.error("上传失败!"); }
/** * 响应结果类 */ public class RespBean { // 自定义状态码 private Integer status; // 自定义响应消息内容 private String msg; // 自定义返回的对象 private Object obj; public static RespBean build() { return new RespBean(); } // 响应成功返回 200 正确消息 msg public static RespBean ok(String msg) { return new RespBean(200,msg,null); } // 相应成功返回 200 正确消息 msg 以及要返回的对象 obj public static RespBean ok(String msg,Object obj) { return new RespBean(200,msg,obj); } // 响应成功返回 500 错误消息 msg public static RespBean error(String msg) { return new RespBean(500,msg,null); } // 相应成功返回 500 错误消息 msg 以及要返回的对象 obj public static RespBean error(String msg,Object obj) { return new RespBean(500,msg,obj); } private RespBean() { } private RespBean(Integer status, String msg, Object obj) { this.status = status; this.msg = msg; this.obj = obj; } public Integer getStatus() { return status; } public RespBean setStatus(Integer status) { this.status = status; return this; } public String getMsg() { return msg; } public RespBean setMsg(String msg) { this.msg = msg; return this; } public Object getObj() { return obj; } public RespBean setObj(Object obj) { this.obj = obj; return this; } }
/** * Excel 解析成数据集合 * * @return */ public static List<Employee> excel2Employee(MultipartFile file) { List<Employee> list = new ArrayList<>(); Employee employee = null; try { //1. 创建一个 workbook 对象 HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream()); //2. 获取 workbook 中表单的数量 int numberOfSheets = workbook.getNumberOfSheets(); for (int i = 0; i < numberOfSheets; i++) { //3. 获取表单 HSSFSheet sheet = workbook.getSheetAt(i); //4. 获取表单中的行数 int physicalNumberOfRows = sheet.getPhysicalNumberOfRows(); for (int j = 0; j < physicalNumberOfRows; j++) { //5. 跳过标题行 if (j == 0) { continue;//跳过标题行 } //6. 获取行 HSSFRow row = sheet.getRow(j); if (row == null) { continue;//防止数据中间有空行 } //7. 获取列数 int physicalNumberOfCells = row.getPhysicalNumberOfCells(); employee = new Employee(); for (int k = 0; k < physicalNumberOfCells; k++) { HSSFCell cell = row.getCell(k); switch (cell.getCellType()) { // 类型是 String 进入此 case 块 case STRING: String cellValue = cell.getStringCellValue(); switch (k) { case 1: employee.setName(cellValue); break; case 2: employee.setWorkID(cellValue); break; case 3: employee.setGender(cellValue); break; case 5: employee.setIdCard(cellValue); break; case 6: employee.setWedlock(cellValue); break; case 7: employee.setNationId(cellValue); break; case 8: employee.setNativePlace(cellValue); break; case 9: employee.setPoliticId(cellValue); break; case 10: employee.setPhone(cellValue); break; case 11: employee.setAddress(cellValue); break; case 12: employee.setDepartmentId(cellValue); break; case 13: employee.setJobLevelId(cellValue); break; case 14: employee.setPosId(cellValue); break; case 15: employee.setEngageForm(cellValue); break; case 16: employee.setTiptopDegree(cellValue); break; case 17: employee.setSpecialty(cellValue); break; case 18: employee.setSchool(cellValue); break; case 20: employee.setWorkState(cellValue); break; case 21: employee.setEmail(cellValue); break; } break; // 类型是 Date或者数字 进入此 case 块 default: { switch (k) { case 4: employee.setBirthday(cell.getDateCellValue()); break; case 19: employee.setBeginDate(cell.getDateCellValue()); break; case 23: employee.setBeginContract(cell.getDateCellValue()); break; case 24: employee.setEndContract(cell.getDateCellValue()); break; case 22: employee.setContractTerm(cell.getNumericCellValue()); break; case 25: employee.setConversionTime(cell.getDateCellValue()); break; } } break; } } // 最后将解析后的数据添加到员工集合中 list.add(employee); } } } catch (IOException e) { e.printStackTrace(); } return list; }
:show-file-list
是否显示已上传文件列表:before-upload
上传文件前的调用事件:on-success
上传成功后的调用事件:on-error
上传成功后的调用事件:disabled
是否禁用上传组件action
要上传的路径 <el-upload
:show-file-list="false"
:before-upload="beforeUpload"
:on-success="onSuccess"
:on-error="onError"
:disabled="importDisabled"
style="display: inline-flex;margin-right: 10px;"
action="这里面填写刚刚写好的服务端接口路径">
<el-button size="small" :disabled="importDisabled" type="success" :icon="importDataIcon">
{{importDataText}}
</el-button>
</el-upload>
data(){
return{
// 导入按钮的文本
importDataText: '导入数据',
// 导入按钮的图标
importDataIcon: 'el-icon-upload2',
// 导入按钮是否被禁用
importDisabled: false
}
}
methods: { // 导入文件失败后回调 onError() { this.importDataText = '导入数据'; this.importDataIcon = 'el-icon-upload2'; this.importDisabled = false; this.initEmps(); this.$message.success("导入失败!"); }, // 导入文件成功后回调 onSuccess() { // 成功后文本修改为原来的导入数据 this.importDataText = '导入数据'; // 图标修改 this.importDataIcon = 'el-icon-upload2'; // 将上传组件改为允许使用 this.importDisabled = false; // 调用刷新数据的方法 this.initEmps(); // message 弹出消息 this.$message.success("导入成功!"); }, // 上传文件调用 beforeUpload() { // 将文本修改为正在导入 this.importDataText = '正在导入'; // 修改其图标 this.importDataIcon = 'el-icon-loading'; // 将其上传组件暂时禁用 this.importDisabled = true; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。