前后端分离springboot+vue2查询数据导出为Excel

后端添加POI依赖

xml 复制代码
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.3</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
</dependency>
 

后端接口

复制代码
 // 条件导出流程
    @PostMapping({"/exportActivesByCondition"})
    public void exportActivesByCondition(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) throws IOException {
       processService.exportActivesByCondition(paramMap, response);
    }

后端实现

复制代码
@Override
    public void exportActivesByCondition(Map<String, Object> paramMap, HttpServletResponse response) throws IOException {
        Process process = new Process();
        String formType = (String)paramMap.get("formType");
        process.setTitle(formType);
        process.setOrderId((String)paramMap.get("orderId"));
        String currentUsername = BaseContext.getCurrentUsername();
     
        // 查询数据
        List<Process> processList = processMapper.exportActivesByCondition(process, currentUsername);

        // 2. 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");

        String fileName = URLEncoder.encode("导出数据_" + System.currentTimeMillis(), "UTF-8")
                .replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition",
                "attachment;filename*=utf-8''" + fileName + ".xlsx");

        // 使用POI创建Excel
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Sheet1");

        // 创建表头
        String[] headers = {"订单单号", "表单类型", "申请人", "流程状态"};
        XSSFRow headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据
        for (int i = 0; i < processList.size(); i++) {
            XSSFRow dataRow = sheet.createRow(i + 1);
            Process p = processList.get(i);
            dataRow.createCell(0).setCellValue(p.getOrderId());
            dataRow.createCell(1).setCellValue(p.getTitle());
            dataRow.createCell(2).setCellValue(p.getApplicant());
            dataRow.createCell(3).setCellValue(p.getStage());
        }

        // 输出
        workbook.write(response.getOutputStream());
        workbook.close();
    }

前端

复制代码
<el-button size="mini" @click="exportActives" :disabled="isSubmit">导出流程</el-button>

组件方法,放在 method方法内

复制代码
 // 导出进行中流程
    async exportActives() {
      this.isSubmit = true  // 禁用
      try {
        // console.log(this.Parameters.stage)
        const response = await reqExportActivesByCondition(this.queryParams)
        downloadExcel(response, `流程数据_${new Date().getTime()}.xlsx`)
        this.$message.success('导出成功')
      } catch (error) {
        this.$message.error('导出失败,请重试', error)
        this.isSubmit = false  // 取消禁用
      }
      this.isSubmit = false  // 取消禁用
    },

下载excel方法

复制代码
/**
 * 下载Excel文件
 */
export function downloadExcel(blob, fileName = `export_${Date.now()}.xlsx`) {
    // 创建下载链接
    const link = document.createElement('a')
    const url = URL.createObjectURL(blob)
    link.href = url
    link.download = fileName
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    URL.revokeObjectURL(url)
}

请求接口,需要把相应类型设置为 blob。responseType: 'blob'

复制代码
// 条件导出流程
export function reqExportActivesByCondition(params) {
    return request({
        url: '/process/exportActivesByCondition',
        method: 'post',
        data:  params ,
        responseType: 'blob'
    })
}
相关推荐
小蒜学长5 小时前
基于SpringBoot 框架的自媒体新闻系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·mysql数据库·自媒体新闻管理系统
IT_陈寒5 小时前
JavaScript实战技巧总结:深挖数组遍历的性能陷阱与实战解法
前端·人工智能·后端
余槐i6 小时前
PostgreSQL 加索引后查询反而更慢:优化器弃用索引的 3 种情形
spring boot·redis·mysql·postgresql·事务
学长毕设15 小时前
基于springboot的食品仓库管理系统
java·spring boot·后端
滴滴答答哒15 小时前
ASP.NET Core + FaceRecognitionDotNet 人脸识别实战:DI 单例、特征向量持久化与 1:N 比对
后端·asp.net
captain37618 小时前
MyBatis操作数据库(入门2)
java·数据库·spring boot·后端·spring·mybatis
IT_陈寒19 小时前
Java序列化踩坑记:对象循环引用让我加班两小时
前端·人工智能·后端
泡海椒19 小时前
05-雷达图多维画像-信用评级多维画像
后端
王中阳Go19 小时前
5.27 分钟干完后端和全部测试:一场三小时笔试的五个失分点,四个不在代码上
大数据·数据库·人工智能·后端·大语言模型
大哥430919 小时前
Modbus 的"字序":一个配错就变成天文数字,或者干脆变成 0
后端