mirror of https://github.com/alibaba/easyexcel
6 changed files with 158 additions and 0 deletions
@ -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> |
@ -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); |
||||
} |
||||
} |
@ -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); |
||||
})); |
||||
} |
||||
|
||||
} |
@ -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; |
||||
} |
||||
|
@ -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; |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue