在使用Spring Boot和MyBatis进行报表导出功能开发时,处理大数据量的Excel文件(从几十兆到几个G)的导出是一个常见而又具有挑战性的任务。本文将详细介绍如何高效地处理大数据量的Excel导出,包括如何生成Excel文件并导出,同时通过接口向前端返回进度信息。
1. 使用合适的Excel处理库
对于大数据量的Excel文件,选择合适的Excel处理库至关重要。以下是一些推荐的库:
- Apache POI:适用于较小数据量的Excel处理。
- SXSSF (Streaming Usermodel API):Apache POI提供的流式API,可以处理较大数据量。
- EasyExcel:阿里巴巴开源的Excel处理库,内存占用较小,适合处理大数据量。
在本文中,我们将使用EasyExcel来实现流式写入功能。
2. 分页查询数据
对于大数据量的查询,建议使用分页查询来减小单次查询的数据量,从而减轻内存压力。在MyBatis的Mapper中实现分页查询的方法如下:
@Mapper
public interface DataMapper {
List<Data> selectData(@Param("offset") int offset, @Param("limit") int limit);
}
3. 流式写入Excel
使用EasyExcel进行流式写入,避免一次性将所有数据加载到内存中,具体实现如下:
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ExportService {
@Autowired
private DataMapper dataMapper;
public void exportLargeData(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("大数据导出", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
ExcelWriterBuilder writerBuilder = EasyExcel.write(response.getOutputStream(), Data.class);
ExcelWriterSheetBuilder sheetBuilder = writerBuilder.sheet("Sheet1");
int pageSize = 1000;
int pageNum = 0;
List<Data> dataList;
while (true) {
dataList = dataMapper.selectData(pageNum * pageSize, pageSize);
if (dataList.isEmpty()) {
break;
}
sheetBuilder.doWrite(dataList);
pageNum++;
}
}
}
4. 异步处理
对于非常大的数据导出任务,建议使用异步处理,并通过消息队列或任务调度框架(如Quartz)来管理导出任务。使用Spring的@Async注解可以实现异步处理:
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class ExportService {
@Autowired
private DataMapper dataMapper;
@Async
public void exportLargeDataAsync(HttpServletResponse response) {
exportLargeData(response);
}
public void exportLargeData(HttpServletResponse response) {
// 流式写入逻辑
}
}
在Spring Boot的主配置类中启用异步支持:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5. 实时进度推送
为了在执行大数据量的Excel导出时向前端返回进度信息,我们可以使用WebSocket进行实时通信。下面是具体的实现步骤:
添加WebSocket依赖
在pom.xml中添加WebSocket依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
配置WebSocket
创建一个WebSocket配置类:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new ExportProgressHandler(), "/exportProgress").setAllowedOrigins("*");
}
}
创建WebSocket处理器
创建一个WebSocket处理器类:
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.TextMessage;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
public class ExportProgressHandler extends TextWebSocketHandler {
private static ConcurrentHashMap<String, WebSocketSession> sessions = new ConcurrentHashMap<>();
@Override
public void afterConnectionEstablished(WebSocketSession session) {
sessions.put(session.getId(), session);
}
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
// 处理接收到的消息(如果需要)
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
sessions.remove(session.getId());
}
public static void sendProgress(String sessionId, String progress) throws IOException {
WebSocketSession session = sessions.get(sessionId);
if (session != null && session.isOpen()) {
session.sendMessage(new TextMessage(progress));
}
}
}
修改导出服务
在导出服务中推送进度信息:
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
@Service
public class ExportService {
@Autowired
private DataMapper dataMapper;
@Async
public void exportLargeDataAsync(HttpServletResponse response, String sessionId) {
exportLargeData(response, sessionId);
}
public void exportLargeData(HttpServletResponse response, String sessionId) {
// 初始化Excel写入器
// 设置分页大小
int pageSize = 1000;
int pageNum = 0;
List<Data> dataList;
while (true) {
dataList = dataMapper.selectData(pageNum * pageSize, pageSize);
if (dataList.isEmpty()) {
break;
}
sheetBuilder.doWrite(dataList);
// 计算进度
int progress = (pageNum * pageSize * 100) / totalRows;
try {
ExportProgressHandler.sendProgress(sessionId, "Progress: " + progress + "%");
} catch (IOException e) {
e.printStackTrace();
}
pageNum++;
}
}
}
前端WebSocket接收进度
在前端使用WebSocket接收进度信息,例如在JavaScript中:
let socket = new WebSocket("ws://localhost:8080/exportProgress");
socket.onmessage = function(event) {
let progress = event.data;
console.log("Progress: " + progress);
// 更新前端进度条或其他UI
};
function startExport() {
fetch("/export/start")
.then(response => response.json())
.then(data => {
console.log("Export started");
});
}
至此,通过以上步骤,可以使用Spring Boot和MyBatis实现高效的大数据量Excel导出功能,并通过WebSocket实时向前端推送进度信息,从而提升用户体验。如果对Redis或其他缓存技术更熟悉,也可以通过缓存存储进度信息并定期轮询来实现类似的功能。希望本文对你有所帮助!