diff --git a/easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/controller/HelloController.java b/easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/controller/HelloController.java index 67b7d53a..a7260728 100644 --- a/easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/controller/HelloController.java +++ b/easyexcel-webflux-test/src/test/java/com/alibaba/easyexcel/test/webflux/controller/HelloController.java @@ -1,29 +1,41 @@ package com.alibaba.easyexcel.test.webflux.controller; +import com.alibaba.easyexcel.test.webflux.entity.DemoData; import com.alibaba.easyexcel.test.webflux.service.HelloService; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.read.listener.PageReadListener; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.http.codec.multipart.FilePart; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import java.io.IOException; +import java.io.InputStream; + /** * @author gongxuanzhang **/ @RestController +@Slf4j public class HelloController { @Autowired private HelloService helloService; @GetMapping(value = "/download") - @ResponseBody public ResponseEntity> exportExcel() { String fileName = "export.xlsx"; return ResponseEntity.ok() @@ -35,4 +47,26 @@ public class HelloController { })); } + @PostMapping("/upload") + public Mono uploadFile(@RequestPart("file") FilePart filePart) { + // 可以直接把上个方法导出的excel导入到这里 + Flux dataBufferFlux = filePart.content(); + Mono monoDataBuffer = DataBufferUtils.join(dataBufferFlux); + return monoDataBuffer.flatMap(dataBuffer -> { + try (InputStream inputStream = dataBuffer.asInputStream()) { + EasyExcel.read(inputStream, DemoData.class, new PageReadListener(dataList -> { + for (DemoData demoData : dataList) { + log.info("读取到一条数据{}", demoData); + } + })).sheet().doRead(); + DataBufferUtils.release(dataBuffer); + return Mono.just("File uploaded successfully"); + } catch (IOException e) { + DataBufferUtils.release(dataBuffer); + return Mono.error(e); + } + }); + } + + }