spring boot+mybatisplus+vue+elementui+easypoi实现excel导出,用户需要选取列表中的部分字段进行导出操作,列表是一对多形式的列表,请给我前后端详细的步骤和代码

时间: 2024-05-12 13:17:10 浏览: 12
好的,关于您的问题,以下是前后端详细的步骤和代码: 前端代码:使用Vue和ElementUI实现 1. 首先在Vue组件中定义你的表格,要注意的是要使用el-table-column组件定义每一列的数据和显示方式,其中prop属性表示对应的数据字段,label属性表示列名,使用v-model绑定selection数组来记录选中的行: ```html <el-table :data="tableData" v-loading="tableLoading" @selection-change="handleSelectionChange" @sort-change="handleSortChange" style="width: 100%"> <el-table-column type="selection" width="55"></el-table-column> <el-table-column :prop="'id'" label="ID" sortable="custom" width="80"></el-table-column> <el-table-column :prop="'name'" label="姓名" sortable="custom" width="100"></el-table-column> <el-table-column :prop="'age'" label="年龄" sortable="custom" width="80"></el-table-column> <el-table-column :prop="'gender'" label="性别" sortable="custom" width="80"> <template slot-scope="scope"> <span>{{scope.row.gender == 1 ? '男' : '女'}}</span> </template> </el-table-column> <el-table-column :prop="'address'" label="地址"></el-table-column> </el-table> ``` 2. 在模板中添加一个按钮,点击后调用导出方法: ```html <el-button type="primary" @click="handleExport">导出</el-button> ``` 3. 在Vue实例中定义导出方法,首先获取选中行的id数组(selection),然后使用axios发送POST请求到后端,传递ids参数作为选中行id数组,以及fields参数作为要导出的字段数组。注意在请求头中设置Content-Type为application/json,以及Response-Type为arraybuffer,以便正常接收Excel文件流: ```javascript handleExport() { var ids = this.selection.map(item => item.id) var fields = ['id', 'name', 'age', 'gender', 'address'] axios({ method: 'post', url: '/export', data: { ids: ids, fields: fields }, responseType: 'arraybuffer', headers: { 'Content-Type': 'application/json' } }).then(response => { const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' }) const fileName = 'export.xlsx' FileSaver.saveAs(blob, fileName) }).catch(e => { this.$message.error('导出失败') }) } ``` 后端代码:使用Spring Boot和Mybatis-Plus实现 1. 首先创建一个实体类,表示数据库中的一行记录,使用Mybatis-Plus的@TableField注解表示对应的字段,以及@TableId注解表示主键: ```java import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class User { @TableId(value = "id", type = IdType.AUTO) private Integer id; @TableField("name") private String name; @TableField("age") private Integer age; @TableField("gender") private Integer gender; @TableField("address") private String address; } ``` 2. 创建一个Mapper接口,继承Mybatis-Plus的BaseMapper接口,用于对User表进行操作,其中的selectExportList方法返回要导出的数据列表,使用@Param注解传递ids和fields参数: ```java import org.apache.ibatis.annotations.Param; import java.util.List; public interface UserMapper extends BaseMapper<User> { List<User> selectExportList(@Param("ids") List<Integer> ids, @Param("fields") List<String> fields); } ``` 3. 创建一个Controller类,用于处理导出请求,使用@RequestBody接收前端传递过来的ids和fields参数,然后调用UserMapper的selectExportList方法获取要导出的数据列表,最后使用EasyPoi进行Excel导出: ```java import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; @RestController public class ExportController { @Autowired private UserMapper userMapper; @RequestMapping("/export") public void export(@RequestBody ExportRequest request, HttpServletResponse response) throws IOException { List<User> userList = userMapper.selectExportList(request.getIds(), request.getFields()); ExportParams params = new ExportParams(null, "用户信息", ExcelType.XSSF); Workbook workbook = ExcelExportUtil.exportExcel(params, User.class, userList); String fileName = "export.xlsx"; response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); workbook.close(); } } class ExportRequest { private List<Integer> ids; private List<String> fields; // getter, setter, toString省略 } ``` 至此,您可以开始使用以上代码实现您的需求了,希望能对您有所帮助。

相关推荐

最新推荐

recommend-type

使用Vue+Spring Boot实现Excel上传功能

主要介绍了使用Vue+Spring Boot实现Excel上传,需要的朋友可以参考下
recommend-type

spring boot+vue 的前后端分离与合并方案实例详解

主要介绍了spring boot+vue 的前后端分离与合并方案实例详解,需要的朋友可以参考下
recommend-type

vue中后端做Excel导出功能返回数据流前端的处理操作

主要介绍了vue中后端做Excel导出功能返回数据流前端的处理操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

vue+elementUI组件table实现前端分页功能

主要为大家详细介绍了vue+elementUI组件table实现前端分页功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

vue中导出Excel表格的实现代码

项目中我们可能会碰到导出Excel文件的需求,这篇文章主要介绍了vue中导出Excel表格的实现代码,非常具有实用价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。