crm项目中导出excel文件功能实现(前端layui+后端SpringBoot+Mybatis)

1.首先在ftl文件中添加一个容器,然后通过table.render()方法指定该容器。

<!-- 在页面放置一个元素 <table id="demo"></table>,
然后通过 table.render() 方法指定该容器 -->
<table id="roleList" class="layui-table" lay-filter="roles"></table>

2.在头部工具栏处,添加导出excel按钮

<a class="layui-btn layui-btn-normal excelNews_btn" lay-event="excel" id="exportBtn">
    <i class="layui-icon">&#xe608;</i>
        导出excel表
</a>

 3.在layui中导出excel文件

if (data.event == "excel") {
    // 监听导出按钮点击事件
    $('#exportBtn').click(function () {
        // 执行导出操作
        exportExcel();
    });

    // 导出Excel
    function exportExcel() {
        // 发送请求,下载Excel文件
        window.location.href = ctx + '/excel/export'; // 后端接口
    }

4.后端建立service类

@Service
public class ExcelService {
    public ByteArrayInputStream exportDataToExcel(List<Role> roleList) throws IOException {
        // 创建一个新的Excel工作簿
        Workbook workbook = new XSSFWorkbook();

        // 创建一个工作表
        Sheet sheet = workbook.createSheet("数据表");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("角色id");
        headerRow.createCell(1).setCellValue("角色名称");
        headerRow.createCell(2).setCellValue("权限备注");
        // 添加更多的列

        // 填充数据
        int rowNum = 1;
        for (Role role : roleList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(role.getId());
            row.createCell(1).setCellValue(role.getRoleName());
            row.createCell(2).setCellValue(role.getRemark());
            // 添加更多的列
        }

        // 将工作簿写入字节数组输出流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);

        // 将字节数组输出流转换为字节数组
        byte[] bytes = outputStream.toByteArray();

        // 创建一个ByteArrayInputStream对象
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);

        return inputStream;
    }
}

5. 建立Controller类,建立接口

package com.five.controller;

import com.five.dao.RoleMapper;
import com.five.service.ExcelService;
import com.five.vo.Role;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("excel")
public class ExcelController {

    @Resource
    private ExcelService excelService;
    @Resource
    private RoleMapper roleMapper;

    @GetMapping("export")
    public ResponseEntity<InputStreamResource> exportExcel() throws IOException {
        // 获取需要导出的数据列表(示例)
        List<Role> roleList = getRoleList();

        // 调用ExcelService的导出方法
        ByteArrayInputStream inputStream = excelService.exportDataToExcel(roleList);

        // 设置HTTP响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename=data.xlsx");

        // 创建一个InputStreamResource对象,包装输入流
        InputStreamResource resource = new InputStreamResource(inputStream);

        // 返回带有Excel文件的响应实体
        return ResponseEntity.ok()
                .headers(headers)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(resource);
    }

    // 获取需要导出的数据
    private List<Role> getRoleList() {
        // 所有角色列表
        List<Role> roleList = roleMapper.queryAllRoles();
        return roleList;
    }
}

6. 页面展示

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值