前后端分离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'
    })
}
相关推荐
AskHarries2 小时前
GitHub 登录配置流程
后端
xuhaoyu_cpp_java4 小时前
SpringBoot学习(四)
java·经验分享·spring boot·笔记·学习
Sinclair4 小时前
安企CMS的安装-常见安装问题排查
运维·后端
Sinclair4 小时前
安企CMS的安装-安装引导与初始化
运维·后端
Sinclair4 小时前
安企CMS的安装-源码编译安装
运维·后端·go
Sinclair4 小时前
安企CMS的安装-同一服务器多站点部署
运维·后端
Sinclair4 小时前
安企CMS的安装-服务器手动部署
运维·后端
Sinclair4 小时前
安企CMS的安装-PHPStudy(小皮面板)部署
前端·后端
Sinclair4 小时前
安企CMS的安装-宝塔面板部署
服务器·后端
会飞的特洛伊4 小时前
IAM身份认证
前端·后端