【EasyExcel】导出

本文详细说明了如何在SpringBoot应用中使用EasyExcel进行CSV数据导出,支持多表格和嵌入ECharts图表,前端通过axios进行请求并下载文件。
摘要由CSDN通过智能技术生成
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博客

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值