webflux 下载excel

webflux 下载excel

方案1

public class DownloadUtil {

    /**
     * 下载 excel
     * @param data 数据列表
     * @param clz 类型
     * @param filename 文件名称
     * @param response
     * @return
     */
    public static <T> Mono<Void> downloadExcel(List<T> data, Class<T> clz,String filename,ServerHttpResponse response) {
        HttpHeaders headers = response.getHeaders();
        headers.set(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=".concat(filename).concat(""));
        //vnd.ms-excel 可以替换为 octet-stream
        MediaType application = new MediaType("application", "vnd.ms-excel", Charset.forName("UTF-8"));
        headers.setContentType(application);
        DefaultDataBuffer dataBuffer = new DefaultDataBufferFactory().allocateBuffer();
        OutputStream outputStream = dataBuffer.asOutputStream();
        EasyExcel.write(outputStream, clz).sheet("sheet1").doWrite(data);
        Flux<DataBuffer> dataBufferFlux = Flux.create((FluxSink<DataBuffer> emitter) -> {
            emitter.next(dataBuffer);
            emitter.complete();
        });
        return response.writeWith(dataBufferFlux);
    }
}


@GetMapping(value = "/download")
public Mono<Void> downloadDept(ServerHttpResponse response) {
    List<ExportDto> result = departmentService.downloadDept(merchantCode);
    return DownloadUtil.downloadExcel(result,ExportDto.class,"download.xlsx", response);
}

方案2

@GetMapping(value = "/downloadFile")
    public Mono<Void> downloadTest(ServerHttpResponse response) {
        File file = new File("test.xlsx");
        //这里为了模拟流 而去读取本地文件
        FileInputStream in =null;
        try {
            in = new FileInputStream(file);
            Flux<DataBuffer> dataBufferFlux = DataBufferUtils.readByteChannel(in::getChannel,new DefaultDataBufferFactory(),4096);
            ZeroCopyHttpOutputMessage zeroCopyHttpOutputMessage = (ZeroCopyHttpOutputMessage)response;
            HttpHeaders headers = zeroCopyHttpOutputMessage.getHeaders();
            headers.set(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=test.xlsx");
            //vnd.ms-excel 可以替换为 octet-stream
            MediaType application = new MediaType("application", "vnd.ms-excel", Charset.forName("UTF-8"));
            headers.setContentType(application);
            return zeroCopyHttpOutputMessage.writeWith(dataBufferFlux);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  Mono.empty();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在WebFlux应用程序中导入Excel文件,可以使用Apache POI库。首先,您需要在Maven或Gradle中添加POI的依赖项。例如,对于Maven,您可以添加以下依赖项: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 然后,您可以编写一个处理Excel文件的类,例如: ``` public class ExcelHandler { public static List<List<String>> readExcel(InputStream inputStream) throws IOException { Workbook workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); List<List<String>> rows = new ArrayList<>(); for (Row row : sheet) { List<String> cells = new ArrayList<>(); for (Cell cell : row) { cells.add(cell.getStringCellValue()); } rows.add(cells); } workbook.close(); inputStream.close(); return rows; } } ``` 在上面的代码中,`readExcel`方法接受一个`InputStream`对象,该对象包含上传的Excel文件的内容。该方法使用Apache POI库读取Excel文件,并返回一个列表,其中包含Excel表格中的所有行和单元格的字符串值。 最后,在您的WebFlux控制器中,您可以使用以下代码获取上传的文件并调用`ExcelHandler`类: ``` @PostMapping("/upload") public Mono<String> handleFileUpload(@RequestParam("file") FilePart file) throws IOException { InputStream inputStream = file.content(); List<List<String>> rows = ExcelHandler.readExcel(inputStream); // 处理Excel文件内容 return Mono.just("File uploaded successfully!"); } ``` 在上面的代码中,`handleFileUpload`方法使用`@RequestParam`注释获取上传的文件。然后,它将文件内容传递给`ExcelHandler`类进行处理。最后,它返回一个Mono对象,该对象包含上传成功的消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值