Skip to content

Commit 7c3877c

Browse files
committed
feat(excel): 支持动态表达式处理导出字段
refactor(handler): 使用StandardCharsets替换硬编码字符集 test(enum): 更新测试数据字段名 docs(readme): 更新版本号至3.4.1
1 parent 8ea4e82 commit 7c3877c

File tree

6 files changed

+42
-67
lines changed

6 files changed

+42
-67
lines changed

README.md

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
| 版本 | 支持版本 |
2222
|-------|-------------------|
23-
| 3.4.0 | 适配 Spring Boot 3.x |
23+
| 3.4.1 | 适配 Spring Boot 3.x |
2424
| 1.2.7 | 适配 Spring Boot 2.x |
2525

2626
`pom.xml` 中添加以下依赖:
@@ -33,64 +33,6 @@
3333
</dependency>
3434
```
3535

36-
## 导入 Excel
37-
38-
### 控制器示例
39-
40-
你可以通过在接口方法中使用 `@RequestExcel` 注解来接收上传的 Excel 文件并将其解析为 Java 对象列表:
41-
42-
```java
43-
@PostMapping("/upload")
44-
public void upload(@RequestExcel List<DemoData> dataList, BindingResult bindingResult) {
45-
// JSR 303 校验通用校验获取失败的数据
46-
List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();
47-
}
48-
```
49-
50-
### 实体类定义
51-
52-
需要先定义与 Excel 表格对应的实体类,并使用 `@ExcelProperty` 注解来标注 Excel 列的索引:
53-
54-
```java
55-
@Data
56-
public class Demo {
57-
@ExcelProperty(index = 0)
58-
private String username;
59-
60-
@ExcelProperty(index = 1)
61-
private String password;
62-
}
63-
```
64-
65-
### 示例表格
66-
67-
下图展示了与上述实体类对应的 Excel 表格:
68-
69-
![Example Excel](https://minio.pigx.top/oss/1618560470.png)
70-
71-
## 导出 Excel
72-
73-
你只需在控制器方法中返回一个 `List`,并使用 `@ResponseExcel` 注解即可将数据导出为 Excel 文件:
74-
75-
```java
76-
@Documented
77-
@Target(ElementType.METHOD)
78-
@Retention(RetentionPolicy.RUNTIME)
79-
public @interface ResponseExcel {
80-
String name() default "";
81-
ExcelTypeEnum suffix() default ExcelTypeEnum.XLSX;
82-
String password() default "";
83-
Sheet[] sheets() default @Sheet(sheetName = "sheet1");
84-
boolean inMemory() default false;
85-
String template() default "";
86-
String[] include() default {};
87-
String[] exclude() default {};
88-
Class<? extends WriteHandler>[] writeHandler() default {};
89-
Class<? extends Converter>[] converter() default {};
90-
Class<? extends HeadGenerator> headGenerator() default HeadGenerator.class;
91-
}
92-
```
93-
9436
## 使用文档
9537

9638
更多详细的使用说明,请参考文档:[https://www.yuque.com/pig4cloud/ogf9nv](https://www.yuque.com/pig4cloud/ogf9nv)

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>com.pig4cloud.excel</groupId>
1212
<artifactId>excel-spring-boot-starter</artifactId>
13-
<version>3.4.0</version>
13+
<version>3.4.1</version>
1414
<name>excel-spring-boot-starter</name>
1515
<description>easy and high performance excel</description>
1616
<url>https://pig4cloud.com</url>

src/main/java/com/pig4cloud/plugin/excel/aop/DynamicNameAspect.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import org.springframework.web.context.request.RequestContextHolder;
1313

1414
import java.time.LocalDateTime;
15+
import java.util.Arrays;
16+
import java.util.List;
1517
import java.util.Objects;
18+
import java.util.stream.Collectors;
1619

1720
/**
1821
* @author lengleng
@@ -24,6 +27,8 @@ public class DynamicNameAspect {
2427

2528
public static final String EXCEL_NAME_KEY = "__EXCEL_NAME_KEY__";
2629

30+
public static final String EXCEL_INCLUDES_KEY = "__EXCEL_INCLUDES_KEY__";
31+
2732
private final NameProcessor processor;
2833

2934
@Before("@annotation(excel)")
@@ -41,6 +46,23 @@ public void around(JoinPoint point, ResponseExcel excel) {
4146

4247
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
4348
Objects.requireNonNull(requestAttributes).setAttribute(EXCEL_NAME_KEY, name, RequestAttributes.SCOPE_REQUEST);
49+
50+
// 针对include 字段的表达式处理
51+
if (excel.include().length == 1) {
52+
String includeFields = processor.doDetermineName(point.getArgs(), ms.getMethod(), excel.include()[0]);
53+
54+
if (!StringUtils.hasText(includeFields)) {
55+
return;
56+
}
57+
58+
List<String> includes = Arrays.stream(includeFields.split(","))
59+
.map(String::trim)
60+
.filter(s -> !s.isEmpty())
61+
.collect(Collectors.toList());
62+
Objects.requireNonNull(requestAttributes)
63+
.setAttribute(EXCEL_INCLUDES_KEY, includes, RequestAttributes.SCOPE_REQUEST);
64+
}
65+
4466
}
4567

4668
}

src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
import java.io.File;
4141
import java.io.IOException;
4242
import java.io.InputStream;
43-
import java.io.UnsupportedEncodingException;
4443
import java.lang.reflect.Modifier;
4544
import java.net.URLEncoder;
45+
import java.nio.charset.StandardCharsets;
4646
import java.util.Arrays;
4747
import java.util.List;
4848
import java.util.Objects;
@@ -77,7 +77,6 @@ public void check(ResponseExcel responseExcel) {
7777
}
7878

7979
@Override
80-
@SneakyThrows(UnsupportedEncodingException.class)
8180
public void export(Object o, HttpServletResponse response, ResponseExcel responseExcel) {
8281
check(responseExcel);
8382
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
@@ -86,7 +85,8 @@ public void export(Object o, HttpServletResponse response, ResponseExcel respons
8685
if (name == null) {
8786
name = UUID.randomUUID().toString();
8887
}
89-
String fileName = String.format("%s%s", URLEncoder.encode(name, "UTF-8"), responseExcel.suffix().getValue());
88+
String fileName = String.format("%s%s", URLEncoder.encode(name, StandardCharsets.UTF_8),
89+
responseExcel.suffix().getValue());
9090
// 根据实际的文件类型找到对应的 contentType
9191
String contentType = MediaTypeFactory.getMediaType(fileName)
9292
.map(MediaType::toString)
@@ -122,7 +122,18 @@ public ExcelWriter getExcelWriter(HttpServletResponse response, ResponseExcel re
122122
}
123123

124124
if (responseExcel.include().length != 0) {
125-
writerBuilder.includeColumnFieldNames(Arrays.asList(responseExcel.include()));
125+
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
126+
Object attribute = Objects.requireNonNull(requestAttributes)
127+
.getAttribute(DynamicNameAspect.EXCEL_INCLUDES_KEY, RequestAttributes.SCOPE_REQUEST);
128+
129+
if (Objects.isNull(attribute)) {
130+
writerBuilder.includeColumnFieldNames(Arrays.asList(responseExcel.include()));
131+
}
132+
else {
133+
// 处理 include 字段的表达式
134+
List<String> spelIncludes = (List<String>) attribute;
135+
writerBuilder.includeColumnFieldNames(spelIncludes);
136+
}
126137
}
127138

128139
if (responseExcel.exclude().length != 0) {

src/test/java/com/pig4cloud/plugin/excel/enums/DemoController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
@RequestMapping("/demo")
2020
public class DemoController {
2121

22-
@ResponseExcel
22+
@ResponseExcel(include = "#str")
2323
@RequestMapping("/test")
24-
public List<IndexOrNameData2> test() {
24+
public List<IndexOrNameData2> test(String str) {
2525
List<IndexOrNameData2> list = new ArrayList<>();
2626
IndexOrNameData2 indexOrNameData2 = new IndexOrNameData2();
2727
indexOrNameData2.setSex("0");

src/test/java/com/pig4cloud/plugin/excel/enums/IndexOrNameData2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class IndexOrNameData2 {
1616
/**
1717
* 读取第一列
1818
*/
19-
@ExcelProperty(value = "列1")
19+
@ExcelProperty(value = "aa")
2020
// 指定对应的枚举类 (字符串)
2121
@DictTypeProperty(enums = SexEnum.class)
2222
private String sex;

0 commit comments

Comments
 (0)