转载请注明出处:
官方文档: https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read
优点:节省内存消耗,可大量减少网络开销
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.1.0</version> </dependency>
导入对应的表格字段解析实体类:
public class ConfigImportExcelRow implements Serializable { @ExcelProperty(value = "姓名", index = 0) private String userName; /** * 昵称 */ @ExcelProperty(value = "昵称", index = 1) private String nickName; @ExcelProperty(value = "年龄", index = 2) private Integer age; }
接口示例
@PostMapping("/import") public JSONResult import(@RequestParam(name = "file") MultipartFile file, HttpServletResponse response) { List<ConfigImportExcelRow> userRowList = null; try { userRowList = EasyExcel.read(file.getInputStream()).headRowNumber(2) .head(ConfigImportExcelRow.class).sheet().doReadSync(); } catch (Exception e) { LOGGER.error("导入配置异常", e); } return "导入结果"; }
head()指定Excel行对应的POJO
sheet() 读取第一个sheet
headRowNumber 从头的第几行开始读取:可以设置1,因为头就是一行。如果多行头,可以设置其他值。不传入也可以没有指定头,也就是默认1行
当有多个 sheet的时候,可以将 read 改为 readSheet 方法:
官方示例:
同样对导出的实体字段添加 @ExcelProperty 注解
public class ConfigExportExcelRow implements Serializable { @ExcelProperty(value = "姓名", index = 0) private String userName; /** * 昵称 */ @ExcelProperty(value = "昵称", index = 1) private String nickName; @ExcelProperty(value = "年龄", index = 2) private Integer age; }
调用示例
@GetMapping("/export") @ApiOperation(value = "导出明细") public JSONResult importUser(HttpServletResponse response) { return JSONResult.okResult(); } public void exportWorkerPlan(Long cycleId, HttpServletResponse response) { try { List<ConfigExportExcelRow> excelRowList = new ArrayList<>(); ConfigExportExcelRow result1 = new ConfigExportExcelRow(); result1.setUserName("张三"); result1.setNickName("张三"); result1.setAge(22); excelRowList.add(result1); AssessPersonConfigImportResult result2 = new AssessPersonConfigImportResult(); result2.setUserName("李四"); result2.setNickName("李四"); result2.setAge("33"); excelRowList.add(result2); String fileFix = DateUtils.formatDate(System.currentTimeMillis(), DateFormatEnum.YYYYMMDDHHMMSSS); String fileName = "导出结果_" + fileFix; response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 fileName = URLEncoder.encode(fileName, "UTF-8").replace("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); // 内容样式策略 WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); // 垂直居中,水平居中 contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); contentWriteCellStyle.setBorderTop(BorderStyle.THIN); contentWriteCellStyle.setBorderRight(BorderStyle.THIN); contentWriteCellStyle.setBorderBottom(BorderStyle.THIN); // 字体策略 WriteFont contentWriteFont = new WriteFont(); // 字体大小 contentWriteFont.setFontHeightInPoints((short) 10); contentWriteCellStyle.setWriteFont(contentWriteFont); //设置输出流和模板信息 File excelTemplate = ResourceUtils.getFile(EXCEL_TEMPLATE_PATH); ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()) .registerWriteHandler(new HorizontalCellStyleStrategy(null, contentWriteCellStyle)) .excelType(ExcelTypeEnum.XLSX) .withTemplate(excelTemplate).build(); WriteSheet writeSheet = EasyExcel.writerSheet().build(); //开启自动换行,自动换行表示每次写入一条list数据是都会重新生成一行空行,此选项默认是关闭的,需要提前设置为true FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); excelWriter.fill(excelRowList, fillConfig, writeSheet); excelWriter.finish(); } catch (Exception e) { LOGGER.error("导出Excel失败, errorMessage={}", e.getMessage(), e); throw new BusinessException(ActionStatus.PARAMAS_ERROR.inValue(), "人员设置导出Excel失败!"); } }
Playwright是新兴的自动化测试工具,拥有丰富的功能和API,隐藏在众多的爬虫和自动化工具背后,而多模LLM的出现让Playwright可以如虎添翼,自动化智能化的RPA工具预计将会井喷般出现。