alibaba/easyexcel

关于webflux 分块返回模板文件的问题

Open

#3,929 opened on Aug 13, 2024

View on GitHub
 (0 comments) (0 reactions) (0 assignees)Java (7,599 forks)batch import
help wanted

Repository metrics

Stars
 (33,728 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

建议先去看文档

快速开始常见问题

异常代码

  private Flux<DataBuffer> getData( ) {
	 
		return Flux.create(sink -> {
		 ClassPathResource classPathResource = new ClassPathResource(
					"template.xlsx");
			try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
				InputStream inputStream = classPathResource	.getInputStream();
				ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).build(); ) {
				WriteSheet writeSheet = EasyExcel.writerSheet().build();
				while (dataList.isNotEmpty()) {	
					excelWriter.fill(dataList, writeSheet);			  
					sink.next(convertOutputStreamToDataBuffer(outputStream)); 
					outputStream.reset();					
					dataList=getList();
				}
                                excelWriter.finish();
				sink.complete();
			} catch (Exception e) {
				log.error("发送分片数据异常:{}", e);
				sink.error(e);
			}

		}); 
	}

异常提示

无异常但是使用webflux 返回文件无数据

问题描述

以上代码运行时无法按照所想的结果分批次返回fill的文件块,断点上看似乎是因为excelWriter.finish()时outputStream中才有数据? 假如数据集比较大 需要采用分次fill的形式 我需要在每次fill之后获取有数据的outputStream 应该怎么做?

Contributor guide