import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@RestController
public class ExportController {
@PostMapping("/export")
public void export(HttpServletResponse response, @RequestBody Task task) throws IOException {
List<Task1> List1 = task.getTask1List();
List<Task2> List2 = task.getTask2List();
String fileName = "exportedData";
String sheetName = "Data";
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
Sheet sheet = new Sheet(1, 0);
sheet.setSheetName(sheetName);
// Add Task, Head, and Line data to the same sheet
excelWriter.write(List1 , sheet);
excelWriter.write(List12, sheet);
excelWriter.finish();
// Send success message to frontend
response.getWriter().write("Export successful");
}
}
Frontend (JavaScript/Vue.js):
handleExport() {
this.$axios.post('system/export', this.queryParams, { responseType: 'blob' })
.then(response => {
const blob = new Blob([response.data], { type: 'application/vnd.ms-excel' })
const link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = `Task_${new Date().getTime()}.xlsx`
link.click()
this.$message.success('Export successful')
})
.catch(error => {
this.$message.error('Export failed')
})
}
【EasyExcel 教程】详解填充Excel -- 填充_forcenewrow-CSDN博客
easypoi模板导出、一张sheet有多个不同表格、带一张或多张echars图表_easypoi实现多个table表格-CSDN博客springboot+easypoi模板导出Excel 动态表头+多表格(一个sheet)_springboot导出excel文件时如何设置成动态表头-CSDN博客