Skip to content

Commit

Permalink
🔧 修改 @RequestExcel 注解的 ignoreEmptyRow 属性默认值
Browse files Browse the repository at this point in the history
  • Loading branch information
Alickx committed Dec 14, 2023
1 parent ecc45a9 commit c620bf3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* 是否跳过空行
* @return 默认跳过
*/
boolean ignoreEmptyRow() default false;
boolean ignoreEmptyRow() default true;

/**
* 工作表名称
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ public List<DemoData> simple(@RequestExcel List<DemoData> list) {
return list;
}

@PostMapping(value = "/ignore-empty-row-enabled")
public List<DemoData> ignoreEmptyRow(@RequestExcel(ignoreEmptyRow = true) List<DemoData> list) {
return list;
}

@PostMapping(value= "/ignore-empty-row-disabled")
public List<DemoData> notIgnoreEmptyRow(@RequestExcel(ignoreEmptyRow = false) List<DemoData> list) {
return list;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* Excel 导入测试类
*
* @author Hccake
*/
@Slf4j
Expand Down Expand Up @@ -85,4 +85,55 @@ void simpleTest() throws Exception {
Assertions.assertEquals("password1", demoDataList.get(1).getPassword());
}

@Test
void ignoreEmptyRowTest() throws Exception {

// 分别写入一个带空行和不带空行的 excel 文件到 各自的multipartFile
List<DemoData> 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<DemoData> demoDataList = objectMapper.readValue(contentAsString,
new TypeReference<List<DemoData>>() {});

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<List<DemoData>>() {});

Assertions.assertEquals(2, demoDataList.size());
Assertions.assertEquals("username0", demoDataList.get(0).getUsername());
Assertions.assertEquals("password2", demoDataList.get(1).getPassword());

}

}

0 comments on commit c620bf3

Please sign in to comment.