From 40f8865edefdc0a5a920b42f3979f25b92030a90 Mon Sep 17 00:00:00 2001 From: Alickx Date: Thu, 14 Dec 2023 17:38:38 +0800 Subject: [PATCH] =?UTF-8?q?:wrench:=20=E4=BF=AE=E6=94=B9=20@RequestExcel?= =?UTF-8?q?=20=E6=B3=A8=E8=A7=A3=E7=9A=84=20ignoreEmptyRow=20=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E9=BB=98=E8=AE=A4=E5=80=BC=20(#284)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :wrench: 修改 @RequestExcel 注解的 ignoreEmptyRow 属性默认值 --------- Co-authored-by: Alickx --- .../easyexcel/annotation/RequestExcel.java | 2 +- .../ExcelImportTestController.java | 10 ++++ .../easyexcel/test/ExcelImportTest.java | 52 ++++++++++++++++++- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/excel/ballcat-spring-boot-starter-easyexcel/src/main/java/org/ballcat/easyexcel/annotation/RequestExcel.java b/excel/ballcat-spring-boot-starter-easyexcel/src/main/java/org/ballcat/easyexcel/annotation/RequestExcel.java index c7551e58d..1e3f9d932 100644 --- a/excel/ballcat-spring-boot-starter-easyexcel/src/main/java/org/ballcat/easyexcel/annotation/RequestExcel.java +++ b/excel/ballcat-spring-boot-starter-easyexcel/src/main/java/org/ballcat/easyexcel/annotation/RequestExcel.java @@ -47,7 +47,7 @@ * 是否跳过空行 * @return 默认跳过 */ - boolean ignoreEmptyRow() default false; + boolean ignoreEmptyRow() default true; /** * 工作表名称 diff --git a/excel/ballcat-spring-boot-starter-easyexcel/src/test/java/org/ballcat/easyexcel/application/ExcelImportTestController.java b/excel/ballcat-spring-boot-starter-easyexcel/src/test/java/org/ballcat/easyexcel/application/ExcelImportTestController.java index 951620347..52e04a2f2 100644 --- a/excel/ballcat-spring-boot-starter-easyexcel/src/test/java/org/ballcat/easyexcel/application/ExcelImportTestController.java +++ b/excel/ballcat-spring-boot-starter-easyexcel/src/test/java/org/ballcat/easyexcel/application/ExcelImportTestController.java @@ -34,4 +34,14 @@ public List simple(@RequestExcel List list) { return list; } + @PostMapping(value = "/ignore-empty-row-enabled") + public List ignoreEmptyRow(@RequestExcel(ignoreEmptyRow = true) List list) { + return list; + } + + @PostMapping(value= "/ignore-empty-row-disabled") + public List notIgnoreEmptyRow(@RequestExcel(ignoreEmptyRow = false) List list) { + return list; + } + } diff --git a/excel/ballcat-spring-boot-starter-easyexcel/src/test/java/org/ballcat/easyexcel/test/ExcelImportTest.java b/excel/ballcat-spring-boot-starter-easyexcel/src/test/java/org/ballcat/easyexcel/test/ExcelImportTest.java index 86fdacf77..e2ccb5c02 100644 --- a/excel/ballcat-spring-boot-starter-easyexcel/src/test/java/org/ballcat/easyexcel/test/ExcelImportTest.java +++ b/excel/ballcat-spring-boot-starter-easyexcel/src/test/java/org/ballcat/easyexcel/test/ExcelImportTest.java @@ -34,6 +34,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -41,7 +42,6 @@ /** * Excel 导入测试类 - * * @author Hccake */ @Slf4j @@ -85,4 +85,54 @@ void simpleTest() throws Exception { Assertions.assertEquals("password1", demoDataList.get(1).getPassword()); } + @Test + void ignoreEmptyRowTest() throws Exception { + + List dataList = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + if (i == 1) { + dataList.add(null); + continue; + } + DemoData data = new DemoData(); + data.setUsername("username" + i); + data.setPassword("password" + i); + dataList.add(data); + } + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + EasyExcel.write(bos, DemoData.class).sheet().doWrite(dataList); + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + MockMultipartFile multipartFile = new MockMultipartFile("file", bis); + MvcResult mvcResult = mockMvc + .perform(MockMvcRequestBuilders.multipart("/import/ignore-empty-row-disabled") + .file(multipartFile) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn(); + + String contentAsString = mvcResult.getResponse().getContentAsString(); + List demoDataList = objectMapper.readValue(contentAsString, + new TypeReference>() {}); + + Assertions.assertEquals(3, demoDataList.size()); + Assertions.assertEquals("username0", demoDataList.get(0).getUsername()); + Assertions.assertEquals("password2", demoDataList.get(2).getPassword()); + Assertions.assertNull(demoDataList.get(1).getUsername()); + Assertions.assertNull(demoDataList.get(1).getPassword()); + + // 忽略空行 + mvcResult = mockMvc.perform( + MockMvcRequestBuilders.multipart("/import/ignore-empty-row-enabled").file(multipartFile) + .accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()) + .andReturn(); + contentAsString = mvcResult.getResponse().getContentAsString(); + demoDataList = objectMapper.readValue(contentAsString, + new TypeReference>() {}); + + Assertions.assertEquals(2, demoDataList.size()); + Assertions.assertEquals("username0", demoDataList.get(0).getUsername()); + Assertions.assertEquals("password2", demoDataList.get(1).getPassword()); + + } + }