Browse Source

web flux write demo

pull/3014/head
gongxuanzhang 2 years ago
parent
commit
7c1d3a5f60
  1. 33
      easyexcel-webflux-test/pom.xml
  2. 15
      easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/EasyexcelWebfluxApplication.java
  3. 38
      easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/controller/HelloController.java
  4. 21
      easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/entity/DemoData.java
  5. 45
      easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/service/HelloService.java
  6. 6
      pom.xml

33
easyexcel-webflux-test/pom.xml

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel-parent</artifactId>
<version>3.2.1</version>
</parent>
<artifactId>easyexcel-webflux-test</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

15
easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/EasyexcelWebfluxApplication.java

@ -0,0 +1,15 @@
package com.alibaba.easyexcel.test.webflux;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author gxz gongxuanzhangmelt@gmail.com
**/
@SpringBootApplication
public class EasyexcelWebfluxApplication {
public static void main(String[] args) {
SpringApplication.run(EasyexcelWebfluxApplication.class, args);
}
}

38
easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/controller/HelloController.java

@ -0,0 +1,38 @@
package com.alibaba.easyexcel.test.webflux.controller;
import com.alibaba.easyexcel.test.webflux.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
/**
* @author gongxuanzhang
**/
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping(value = "/download")
@ResponseBody
public ResponseEntity<Mono<Resource>> exportExcel() {
String fileName = "export.xlsx";
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
.body(helloService.generateExcel().flatMap(x -> {
Resource resource = new InputStreamResource(x);
return Mono.just(resource);
}));
}
}

21
easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/entity/DemoData.java

@ -0,0 +1,21 @@
package com.alibaba.easyexcel.test.webflux.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.util.Date;
/**
* @author gongxuanzhang
*/
@Data
public class DemoData {
@ExcelProperty("字符串标题")
private String string;
@ExcelProperty("日期标题")
private Date date;
@ExcelProperty("数字标题")
private Double doubleData;
}

45
easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/service/HelloService.java

@ -0,0 +1,45 @@
package com.alibaba.easyexcel.test.webflux.service;
import com.alibaba.easyexcel.test.webflux.entity.DemoData;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.List;
/**
* @author gxz gongxuanzhangmelt@gmail.com
**/
@Component
public class HelloService {
public Mono<ByteArrayInputStream> generateExcel() {
return Mono.fromCallable(() -> {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
EasyExcel.write(stream, DemoData.class)
.sheet("模板")
.doWrite(this::data);
return new ByteArrayInputStream(stream.toByteArray());
}).subscribeOn(Schedulers.boundedElastic());
}
private List<DemoData> data() {
List<DemoData> list = ListUtils.newArrayList();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + i);
data.setDoubleData(1.1);
data.setDate(new Date());
list.add(data);
}
return list;
}
}

6
pom.xml

@ -16,6 +16,7 @@
<module>easyexcel-support</module>
<module>easyexcel-test</module>
<module>easyexcel</module>
<module>easyexcel-webflux-test</module>
</modules>
@ -152,6 +153,11 @@
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

Loading…
Cancel
Save