#分页和一键导出功能从前端到后端的完整案例
具体页面样式
前端代码
-
页面
<template> <div class="dbxlList"> <el-row style="margin-top: 20px"> <el-col :span="22" :offset="1"> <el-row> <el-col> <el-collapse v-model="activeNames"> <div class="zdhBackgroundColor"> <el-collapse-item name="1"> <template slot="title"> <span style="margin-left: 20px;color: #F2F6FC;font-size: 14px;font-weight: bold">查询条件</span> </template> <div style="margin-left: 13px"> <el-form :inline="true" size="small"> <el-form-item label="线路编号: " style="margin-left: 13px"> <el-input v-model="dbzxlTpfxWeekTj.dbzxlXlbh" style="width: 200px"/> </el-form-item> <el-form-item label="线路名称: " style="margin-left: 60px"> <el-input v-model="dbzxlTpfxWeekTj.dbzxlXlmc" style="width: 180px"/> </el-form-item> <el-form-item label="线路状态: " style="margin-left: 100px"> <el-select v-model="dbzxlTpfxWeekTj.xlzt" clearable placeholder="请选择" style="width: 150px"> <el-option v-for="item in xlztOption" :key="item.value" :label="item.label" :value="item.label"> </el-option> </el-select> </el-form-item> <el-form-item label="日期范围: " style="margin-left: 30px"> <span style="margin-right: 15px">{{dataTimeArr}}</span> <el-date-picker v-model="dataTime" type="week" format="第WW周" value-format="yyyy-MM-dd" @change="dataRange()" placeholder="选择周" style="width: 120px" :picker-options="pickerOption"> </el-date-picker> </el-form-item> </el-form> </div> <div style="margin-left: 13px"> <el-form :inline="true" size="small"> <el-form-item label="线损状态: " style="margin-left: 13px"> <el-select v-model="dbzxlTpfxWeekTj.xszt" clearable placeholder="请选择" style="width: 200px"> <el-option v-for="item in xsLxOptions" :key="item.value" :label="item.label" :value="item.label"> </el-option> </el-select> </el-form-item> <el-form-item label="是否有开关动作: " style="margin-left: 18px"> <el-select v-model="xlInfo.sfkg" clearable placeholder="请选择" style="width: 180px"> <el-option v-for="item in kgOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select> </el-form-item> <el-form-item label="线变关系是否一致: " style="margin-left: 44px"> <el-select v-model="dbzxlTpfxWeekTj.xbgxsfyz" clearable placeholder="请选择" style="width: 150px"> <el-option v-for="item in xbgxOptions" :key="item.value" :label="item.label" :value="item.label"> </el-option> </el-select> </el-form-item> <el-form-item style="margin-left: 30px"> <el-button @click="searchBtn" type="warning" style="width: 80px;background-color:#ea7346" :disabled="disabledBtnFlag">查询 </el-button> </el-form-item> <el-form-item style="margin-left: 20px"> <el-button @click="exportBtn" type="success" icon="el-icon-bottom" style="width: 80px;background-color:#03958d;color:white;" :disabled="disabledBtnExport">导出 </el-button> </el-form-item> </el-form> </div> </el-collapse-item> </div> </el-collapse> </el-col> </el-row> <el-row style="margin-top: 20px;line-height: 44px"> <el-col :span="24" style="background-color: #03958d"> <span style="margin-left: 10px;font-weight: bolder;color: white">打包线路列表</span> </el-col> </el-row> <el-row style="margin-top: 1px"> <el-col> <div class="dbxlList"> <el-table :data="dkxLineloseVO" :header-cell-style="tableHeaderGroundColor" :row-style="rowStyle" style="width: 100%" size="mini" border @expand-change="getBdbkxList"> <el-table-column type="index" label="序号" align="center" width="50" :index="indexMethod"/> <el-table-column prop="ywdwmc" label="单位名称" min-width="12%" align="center"/> <el-table-column prop="dbzxlXlbh" label="线路编号" min-width="12%" align="center"/> <el-table-column prop="dbzxlXlmc" label="线路名称" min-width="10%" align="center"/> <el-table-column prop="dataWeekDesc" label="日期范围" min-width="20%" align="center"/> <el-table-column prop="xbgxsfyz" label="线变关系是否一致" min-width="12%" align="center"/> <el-table-column prop="xlzt" label="线路状态" min-width="8%" align="center"/> <el-table-column prop="xszt" label="线损状态" min-width="8%" align="center"/> <el-table-column prop="cwgbsl" label="公变拓扑错误数量" min-width="12%" align="center"/> <el-table-column prop="cwzbsl" label="专变拓扑错误数量" min-width="12%" align="center"/> <el-table-column label="操作" min-width="5%" align="center"> <template slot-scope="scope"> <el-button @click="toDbDetails(scope.row)" type="text" size="small" style="color: #d08b21"> 查看 </el-button> </template> </el-table-column> <el-table-column type="expand" expand-change="getBdbkxList()"> <template slot-scope="scope"> <div class="dbxlList" align="center" style="margin-outside: 8px"> <el-table :data="bdbkxList" :header-cell-style="tableHeaderGroundColor" :row-style="rowStyle" size="mini" border style="width: 96%"> <el-table-column type="index" label="序号" align="center" width="50"/> <el-table-column prop="ywdwmc" label="单位名称" min-width="12%" align="center"/> <el-table-column prop="bdbkxbh" label="线路编号" min-width="13%" align="center"/> <el-table-column prop="dkxmc" label="线路名称" min-width="10%" align="center"/> <el-table-column prop="bdzmc" label="变电站名称" min-width="10%" align="center"/> <el-table-column prop="dataWeekDesc" label="日期范围" min-width="20%" align="center"/> <el-table-column prop="xbgxsfyz" label="线变关系是否一致" min-width="12%" align="center"> <template slot-scope="scope"> <el-button type="text" style="text-decoration: underline;" @click="xbgxsfyzBtn(scope.row)"> {{scope.row.xbgxsfyz}} </el-button> </template> </el-table-column> <el-table-column prop="xlzt" label="线路状态" min-width="8%" align="center"/> <el-table-column prop="xszt" label="线损状态" min-width="8%" align="center"/> <el-table-column prop="cwgbsl" label="公变拓扑错误数量" min-width="12%" align="center"> <template slot-scope="scope"> <el-button type="text" style="color:#d08b21" size="small" v-if="scope.row.cwgbsl!=0" @click="cwgzbsl(scope.row,'公变')"> {{scope.row.cwgbsl}} </el-button> <span type="text" v-if="scope.row.cwgbsl==0"> 0 </span> </template> </el-table-column> <el-table-column prop="cwzbsl" label="专变拓扑错误数量" min-width="12%" align="center"> <template slot-scope="scope"> <el-button type="text" style="color:#d08b21" size="small" v-if="scope.row.cwzbsl!=0" @click="cwgzbsl(scope.row,'专变')"> {{scope.row.cwzbsl}} </el-button> <span type="text" v-if="scope.row.cwzbsl==0"> 0 </span> </template> </el-table-column> <el-table-column label="操作" min-width="5%" align="center"> <template slot-scope="scope"> <el-button @click="toKxDetails(scope.row)" type="text" size="small" style="color: #d08b21"> 查看 </el-button> </template> </el-table-column> </el-table> </div> </template> </el-table-column> </el-table> </div> </el-col> </el-row> <el-row style="margin-top:15px" v-if="page.total!=null&&page.total!=0"> <el-col :span="24"> <div class="pagination" style="float: right"> <el-pagination background @current-change="dbzxlTpfxWeekTjChange" layout="total,prev, pager, next" :total="page.total" :page-size="page.size" :current-page="page.current"> </el-pagination> </div> </el-col> </el-row>
-
js
import system from '../../common/main'; import vXbgxIsSame from '../../../../src/components/page/XbgxIsSame'; export default { data() { return { pickerOption: { firstDayOfWeek: 1 }, dkxId: '', ssds: '', activeNames: ['1'], dataTimeArr: '', startTime: '', endTime: '', dataTime: '', xlInfo: {}, dkxLineloseVO: [], bdbkxList: [], daterange: '', dkxTpfxWeekTj: {}, dbzxlTpfxWeekTj: {}, dbzxlTpfxWeekTjVo: {}, dkxTpfxWeekTjVo: {}, page: { total: 0, size: 7, current: 1 }, dkxLineloseVOReq: { data: {} }, kgOptions: [{ value: 1, label: '全部' }, { value: 2, label: '是' }, { value: 3, label: '否' }], xsLxOptions: [{ value: 1, label: '全部' }, { value: 2, label: '负损' }, { value: 3, label: '高损' }, { value: 4, label: '正常' }], xbgxOptions: [{ value: 1, label: '全部' }, { value: 2, label: '一致' }, { value: 3, label: '准一致' }, { value: 4, label: '不一致' } ], xlztOption: [ { value: 1, label: '全部' }, { value: 2, label: '疑似异常' }, { value: 3, label: '疑似转供' }, { value: 4, label: '正常' }], disabledBtn: false, disabledBtnFlag: false, disabledBtnExport: false, cwgzbslDialog: false, showHeader: false, xbgxsfyzDialog: false, pwWeekDyxgxfxVoList: [], pwWeekDyxgxfxVo: {} } }, methods: { indexMethod(index) { //计算行序号 return index + 1 + 7 * (this.page.current - 1); }, searchBtn() { this.disabledBtnFlag = true; this.getZhxlList() this.disabledBtnFlag = false; }, //导出功能 exportBtn() { this.disabledBtnExport = true;//这个开关是控制导出按钮可用不可用的 this.dbzxlTpfxWeekTjVo.ssds = this.ssds; //传值,需要变电站名称,大馈线名称,日期范围 if (this.dbzxlTpfxWeekTj.dbzxlXlbh == null || this.dbzxlTpfxWeekTj.dbzxlXlbh == 'undefined') { this.dbzxlTpfxWeekTjVo.dbzxlXlbh = '' } else { this.dbzxlTpfxWeekTjVo.dbzxlXlbh = this.dbzxlTpfxWeekTj.dbzxlXlbh } if (this.dbzxlTpfxWeekTj.dbzxlXlmc == null || this.dbzxlTpfxWeekTj.dbzxlXlmc == 'undefined') { this.dbzxlTpfxWeekTjVo.dbzxlXlmc = '' } else { this.dbzxlTpfxWeekTjVo.dbzxlXlmc = this.dbzxlTpfxWeekTj.dbzxlXlmc } if (this.dbzxlTpfxWeekTj.xbgxsfyz == null || this.dbzxlTpfxWeekTj.xbgxsfyz == '全部') { this.dbzxlTpfxWeekTjVo.xbgxsfyz = '' } else { this.dbzxlTpfxWeekTjVo.xbgxsfyz = this.dbzxlTpfxWeekTj.xbgxsfyz } if (this.dbzxlTpfxWeekTj.xszt == null || this.dbzxlTpfxWeekTj.xszt == '全部') { this.dbzxlTpfxWeekTjVo.xszt = '' } else { this.dbzxlTpfxWeekTjVo.xszt = this.dbzxlTpfxWeekTj.xszt } if (this.dbzxlTpfxWeekTj.sfkg == null) { this.dbzxlTpfxWeekTjVo.sfkg = '' } else { this.dbzxlTpfxWeekTjVo.sfkg = this.xlInfo.sfkg } if (this.startTime == null) { this.dbzxlTpfxWeekTjVo.startTime = '' } else { this.dbzxlTpfxWeekTjVo.startTime = this.dbzxlTpfxWeekTj.startTime } if (this.dbzxlTpfxWeekTj.xlzt == null || this.dbzxlTpfxWeekTj.xlzt == 'undefined' || this.dbzxlTpfxWeekTj.xlzt == '全部') { this.dbzxlTpfxWeekTjVo.xlzt = '' } else { this.dbzxlTpfxWeekTjVo.xlzt = this.dbzxlTpfxWeekTj.xlzt } /*这里是导出的时候用到的一个方法,用这个方法导出的时候是这样的:当你点击导出按钮的时候会从当前页面跳到另外一个页面,这个时候是打开了一个新的页面,然后在这个新打开的页面加载数据,加载完成后,立马自动关闭这个页面,回到原先的页面,这个过程可能会短暂,这样就造成一个页面一闪而过的效果*/ // window.open("/pwtpfxData/dbzxlTpfxWeekTj/exportDbzxlList?ssds=" + this.dbzxlTpfxWeekTjVo.dbzxlTpfxWeekTj.ssds + // "&dbzxlXlbh=" + this.dbzxlTpfxWeekTjVo.dbzxlTpfxWeekTj.dbzxlXlbh + // "&dbzxlXlmc=" + this.dbzxlTpfxWeekTjVo.dbzxlTpfxWeekTj.dbzxlXlmc + // "&dataWeekDesc=" + this.daterange + // "&xbgxsfyz=" + this.dbzxlTpfxWeekTjVo.dbzxlTpfxWeekTj.xbgxsfyz + // "&xszt=" + this.dbzxlTpfxWeekTjVo.dbzxlTpfxWeekTj.xszt + //"&xlzt=" + this.dkxTpfxWeekTjVo.dbzxlTpfxWeekTj.xlzt + // "&sfkg=" + this.dbzxlTpfxWeekTjVo.sfkg, '_blank') /*使用这个方法来导出就不会有一闪而过的效果,因为它所有的操作都是在当前页面完成的,不会打开新的页面*/ let elemIF = document.createElement("iframe"); elemIF.src = "/pwtpfxData/dbzxlTpfxWeekTj/exportDbzxlList?ssds=" + this.dbzxlTpfxWeekTjVo.ssds + "&dbzxlXlbh=" + this.dbzxlTpfxWeekTjVo.dbzxlXlbh + "&dbzxlXlmc=" + this.dbzxlTpfxWeekTjVo.dbzxlXlmc + "&dataWeekDesc=" + this.daterange + "&xbgxsfyz=" + this.dbzxlTpfxWeekTjVo.xbgxsfyz + "&xlzt=" + this.dbzxlTpfxWeekTjVo.xlzt + "&xszt=" + this.dbzxlTpfxWeekTjVo.xszt + "&sfkg=" + this.dbzxlTpfxWeekTjVo.sfkg; elemIF.style.display = 'none'; document.body.appendChild(elemIF); this.disabledBtnExport = false }, dataRange() { if (this.dataTime != null) { var dd = new Date(this.dataTime) var day = dd.getDay(); var d = this.getMonDate(dd); this.startTime = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate(); d.setDate(d.getDate() + 6); this.endTime = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() this.dataTimeArr = this.startTime + '~' + this.endTime } else { this.startTime = ''; this.endTime = ''; this.dataTimeArr = ''; } }, getMonDate(d) { var day = d.getDay(), date = d.getDate(); if (day == 1) return d; if (day == 0) d.setDate(date - 6); else d.setDate(date - day + 1); return d; }, dbzxlTpfxWeekTjChange(val) { this.page.current = val this.getZhxlList() }, //控制表头行背景颜色 tableHeaderGroundColor(val) { if (val.rowIndex === 0) { return 'background-color: #e5b54a;color: #ffffff;'; } else { return ''; } }, //控制表格的背景颜色 rowStyle(val) { if (val.row.xlzt == '错误') { return "background-color: #fccfcd"; } else if (val.row.xlzt == '疑似转供') { return "background-color: #d8fbe8"; } else if (val.row.xlzt == '疑似异常') { return "background-color: #fde8b6"; } else { return ''; } }, //错误公专变信息 cwgzbsl(info, type) { this.pwWeekDyxgxfxVoList = [] if (type == '公变') { this.pwWeekDyxgxfxVo.dkxId = info.dkxId this.pwWeekDyxgxfxVo.startTime = new Date(this.startTime) this.pwWeekDyxgxfxVo.sblx = '公变' this.pwWeekDyxgxfxVo.tpljzt = '错误' } else { this.pwWeekDyxgxfxVo.dkxId = info.dkxId this.pwWeekDyxgxfxVo.startTime = new Date(this.startTime) this.pwWeekDyxgxfxVo.sblx = '专变' this.pwWeekDyxgxfxVo.tpljzt = '错误' } this.$http.post(system.url.pwtpfx + 'pwWeekDyxgxfx/getPwWeekDyxgxfxList', this.pwWeekDyxgxfxVo).then(res => { this.pwWeekDyxgxfxVoList = res.data this.cwgzbslDialog = true }).catch(err => { this.$message.error({message: '数据异常', center: true}) }) }, //跳转到打包线路列表详情页面 toDbDetails(row) { // var page = this.$router.resolve({ // path: '/pwtpfx/ds/dbxllbxq/' + row.dbzxlId + '/' + this.dataTime // }) // window.open(page.href, '_blank') this.$router.push({path: '/pwtpfx/ds/dbxllbxq/' + row.dbzxlId + '/' + this.dataTime}) }, //跳转到馈线列表详情页面 toKxDetails(row) { this.$router.push({path: '/pwtpfx/ds/xllbxq/' + row.dkxId + '/' + this.dataTime}) }, //查询打包组合线路列表 getZhxlList() { if (this.dbzxlTpfxWeekTj.dbzxlXlbh != null && this.dbzxlTpfxWeekTj.dbzxlXlbh != '' && this.dbzxlTpfxWeekTj.dbzxlXlbh.substr(0, 1) == ' ') { this.$message.error({message: '线路编号开头存在空格', center: true}) return } if (this.dbzxlTpfxWeekTj.dbzxlXlmc != null && this.dbzxlTpfxWeekTj.dbzxlXlmc != '' && this.dbzxlTpfxWeekTj.dbzxlXlmc.substr(0, 1) == ' ') { this.$message.error({ message: '线路名称开头存在空格', center: true }) return } if (this.dbzxlTpfxWeekTj.xszt == '全部') { this.dbzxlTpfxWeekTj.xszt = ''; } if (this.dbzxlTpfxWeekTj.xlzt == '全部') { this.dbzxlTpfxWeekTj.xlzt = ''; } if (this.dbzxlTpfxWeekTj.xbgxsfyz == '全部') { this.dbzxlTpfxWeekTj.xbgxsfyz = ''; } this.dbzxlTpfxWeekTj.ssds = this.ssds; this.dbzxlTpfxWeekTj.startTime = new Date(this.startTime) this.$http.post(system.url.pwtpfx + 'dbzxlTpfxWeekTj/getDbzhxlList', { page: this.page, dbzxlTpfxWeekTj: this.dbzxlTpfxWeekTj }).then(res => { this.dkxLineloseVO = res.data.records; this.page.total = res.data.total this.page.current = res.data.current }).catch(err => { console.log(err) this.$message.error({message: '数据异常', center: true}) }) }, //查询被打包线路列表 getBdbkxList(row, val) { if (val.length <= 0) { return } this.dbzxlTpfxWeekTj.dbzxlId = row.dbzxlId; this.$http.post(system.url.pwtpfx + 'dkxTpfxWeekTj/getBdbxlList', this.dbzxlTpfxWeekTj).then(res => { this.bdbkxList = res.data; }).catch(err => { console.log(err) this.$message.error({message: '数据异常', center: true}) }) }, //线变关系是否一致 xbgxsfyzBtn(xsInfo) { this.xbgxsfyzDialog = true setTimeout(() => { let val = { kxId: xsInfo.kxId, dataTime: this.endTime } this.$root.eventHub.$emit("getXbgxisSame", val) }, 100); }, init() { this.getZhxlList() }, }, mounted() { this.dataTime = '2019-08-27' this.dataRange() this.$http.post(system.url.pwtpfx + 'login/getSsds').then(res => { this.ssds = res.data.ssds this.init() }).catch(err => { console.log(err) this.$message.error({message: '数据异常', center: true}) }) }, created() { this.$root.eventHub.$on("setNode", (node) => { if (node.level == 2) { this.dbzxlTpfxWeekTj.ywdwmc = node.data.name this.init() } }) }, components: { vXbgxIsSame } }
后端代码
-
controller
package org.jsepc.pwtpfx.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import io.swagger.annotations.ApiOperation; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.jsepc.pwtpfx.entity.*; import org.jsepc.pwtpfx.service.DbzxlTpfxWeekTjService; import org.springframework.web.bind.annotation.*; import io.swagger.annotations.Api; import org.springframework.beans.factory.annotation.Autowired; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URLEncoder; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping("${project.name}/dbzxlTpfxWeekTj") public class DbzxlTpfxWeekTjController { @Autowired private DbzxlTpfxWeekTjService dbzxlTpfxWeekTjService; //这里分页用的是 mybatis 自带的分页方法,这种方法分页是每次只返回一页数据,不是一次性返回所有页的数据,每查看一页都要调一次后台进行查询,然后返回当前这一页数据 @PostMapping("getDbzhxlList") public IPage<DbzxlTpfxWeekTj> getDbzhxlList(@RequestBody DbzxlTpfxWeekTjVo dbzxlTpfxWeekTjVo) { IPage<DbzxlTpfxWeekTj> dbzhxlList = dbzxlTpfxWeekTjService.getDbzhxlList(dbzxlTpfxWeekTjVo); return dbzhxlList; } @RequestMapping("exportDbzxlList") public void exportDbzxlList(DbzxlTpfxWeekTjVo dbzxlTpfxWeekTjVo, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, ParseException { if (dbzxlTpfxWeekTjVo.getDbzxlTpfxWeekTj() != null) { if(dbzxlTpfxWeekTjVo.getDbzxlTpfxWeekTj().getDataWeekDesc()!=null) { DateFormat format=new SimpleDateFormat("yyyy-MM-dd"); String startTime=dbzxlTpfxWeekTjVo.getDbzxlTpfxWeekTj().getDataWeekDesc().split("~")[0]; Timestamp timestamp=new Timestamp(format.parse(startTime).getTime()); dbzxlTpfxWeekTjVo.getDbzxlTpfxWeekTj().setStartTime(timestamp); dbzxlTpfxWeekTjVo.getDbzxlTpfxWeekTj().setDataWeekDesc(""); } } //真实数据 List<DbzxlTpfxWeekTjVo> dbzxlTpfxWeekTjVoList = dbzxlTpfxWeekTjService.getDbzhxlListExport(dbzxlTpfxWeekTjVo); String[] titles = {"序号", "单位名称","线路编号","线路名称", "日期范围", "线变关系是否一致", "线路状态", "线损状态", "公变拓扑错误数量", "专变拓扑错误数量"}; String[] pro = {"Xuhao", "Ywdwmc", "DbzxlXlbh", "DbzxlXlmc", "DataWeekDesc", "Xbgxsfyz", "Xlzt", "Xszt", "Cwgbsl", "Cwzbsl"};//注意:这一行是查询返回结果的实体类中的属性(实体类中的属性首字母大写,其他字母大小写都要保持一致,否则导出的时候值和列名就对应不上),跟上一行中将要导出的表格的列名要对应, //1,在代码新建一个Excel文件对象 HSSFWorkbook workbook = new HSSFWorkbook(); //2,在文件对象中新建一张表对象 HSSFSheet sheet = workbook.createSheet("打包组合线路列表"); //3,表对象中创建一行数据 写标题栏数据 HSSFRow titleRow = sheet.createRow(0); //3.1写入标题数据 for (int i = 0; i < titles.length; i++) { //创建单元格 HSSFCell cell = titleRow.createCell(i); cell.setCellValue(titles[i]); } //4,写入数据 for (int i = 0; i < dbzxlTpfxWeekTjVoList.size(); i++) { DbzxlTpfxWeekTjVo dbzxlTpfxWeekTjVos = dbzxlTpfxWeekTjVoList.get(i); dbzxlTpfxWeekTjVos.setXuhao(i + 1); HSSFRow rows = sheet.createRow(i + 1); Class<? extends DbzxlTpfxWeekTjVo> dbzxlTpfxWeekTjVoClass = dbzxlTpfxWeekTjVo.getClass(); Method[] methods = dbzxlTpfxWeekTjVoClass.getMethods(); //新建list集合,用来存放get方法 List<Method> methodList = new ArrayList<>(); for (int j = 0; j < pro.length; j++) { for (Method method : methods) { //得到方法名,通过方法名可以检验出来,是不是get方法 boolean getdbzxlTpfxWeekTjVo = method.getName().contains("get"); boolean getClass = method.getName().contains("getClass"); boolean getPro = method.getName().contains(pro[j]); if (getdbzxlTpfxWeekTjVo && !getClass && getPro) { //methodList中就是我们要的get方法 methodList.add(method); } } } for (int j = 0; j < methodList.size(); j++) { HSSFCell cell = rows.createCell(j); Method method = methodList.get(j); Object invoke = method.invoke(dbzxlTpfxWeekTjVos); //在单元格中写入数据 if (invoke.toString() != null) { cell.setCellValue(invoke.toString()); } else { cell.setCellValue(""); } } } try { //这个地方是设置导出的表格名称的 response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("打包主线路拓扑分析.xls", "utf-8")); response.setContentType("application/vnd.ms-excel"); workbook.write(response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); }
}
}
2. service
public interface DbzxlTpfxWeekTjService extends IService<DbzxlTpfxWeekTj>{
//查询打包组合线路列表(分页)
IPage<DbzxlTpfxWeekTj> getDbzhxlList(DbzxlTpfxWeekTjVo dbzxlTpfxWeekTjVo);
//查询打包组合线路列表(导出的数据不分页)
List<DbzxlTpfxWeekTjVo> getDbzhxlListExport(DbzxlTpfxWeekTjVo dbzxlTpfxWeekTjVo);
}
3. serviceImpl
@Service("dbzxlTpfxWeekTjService")
public class DbzxlTpfxWeekTjServiceImpl extends ServiceImpl<DbzxlTpfxWeekTjDao, DbzxlTpfxWeekTj> implements DbzxlTpfxWeekTjService {
@Autowired
private DbzxlTpfxWeekTjDao dbzxlTpfxWeekTjDao;
//查询打包组合线路列表
@Override
public IPage<DbzxlTpfxWeekTj> getDbzhxlList(DbzxlTpfxWeekTjVo dbzxlTpfxWeekTjVo) {
IPage<DbzxlTpfxWeekTj> dbzhxlList = dbzxlTpfxWeekTjDao.selectDbzhxlList(dbzxlTpfxWeekTjVo.getPage(), dbzxlTpfxWeekTjVo.getDbzxlTpfxWeekTj());
return dbzhxlList;
}
//查询打包组合线路列表(导出的数据不分页)
@Override
public List<DbzxlTpfxWeekTjVo> getDbzhxlListExport(DbzxlTpfxWeekTjVo dbzxlTpfxWeekTjVo) {
return dbzxlTpfxWeekTjDao.selectDbzhxlListExport(dbzxlTpfxWeekTjVo);
}
}
4. dao
public interface DbzxlTpfxWeekTjDao extends BaseMapper<DbzxlTpfxWeekTj> {
//查询打包组合线路列表
IPage<DbzxlTpfxWeekTj> selectDbzhxlList(Page page, @Param("dbzxlTpfxWeekTj") DbzxlTpfxWeekTj dbzxlTpfxWeekTj);
//查询打包组合线路列表(导出的数据不分页)
List<DbzxlTpfxWeekTjVo> selectDbzhxlListExport(@Param("dbzxlTpfxWeekTjVo") DbzxlTpfxWeekTjVo dbzxlTpfxWeekTjVo);
}
-
dao.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.jsepc.pwtpfx.dao.DbzxlTpfxWeekTjDao"> <resultMap type="org.jsepc.pwtpfx.entity.DbzxlTpfxWeekTj" id="DbzxlTpfxWeekTjMap"> <result property="dbzxlId" column="DBZXL_ID"/> <result property="ssds" column="SSDS"/> <result property="ssdsmc" column="SSDSMC"/> <result property="ywdw" column="YWDW"/> <result property="ywdwmc" column="YWDWMC"/> <result property="dbzxlXlbh" column="DBZXL_XLBH"/> <result property="dbzxlXlmc" column="DBZXL_XLMC"/> <result property="dataYear" column="DATA_YEAR"/> <result property="dataWeekNum" column="DATA_WEEK_NUM"/> <result property="startTime" column="START_TIME"/> <result property="endTime" column="END_TIME"/> <result property="dataWeekDesc" column="DATA_WEEK_DESC"/> <result property="xbgxsfyz" column="XBGXSFYZ"/> <result property="xlzt" column="XLZT"/> <result property="xszt" column="XSZT"/> <result property="gbsl" column="GBSL"/> <result property="zbsl" column="ZBSL"/> <result property="ztqsl" column="ZTQSL"/> <result property="zqtqsl" column="ZQTQSL"/> <result property="yszgtqsl" column="YSZGTQSL"/> <result property="ysyctqsl" column="YSYCTQSL"/> <result property="cwtqsl" column="CWTQSL"/> <result property="cwgbsl" column="CWGBSL"/> <result property="cwzbsl" column="CWZBSL"/> </resultMap> <resultMap type="org.jsepc.pwtpfx.entity.DbzxlTpfxWeekTjVo" id="DbzxlTpfxWeekTjVoMap"> <result property="dbzxlId" column="DBZXL_ID"/> <result property="ssds" column="SSDS"/> <result property="ssdsmc" column="SSDSMC"/> <result property="ywdw" column="YWDW"/> <result property="ywdwmc" column="YWDWMC"/> <result property="dbzxlXlbh" column="DBZXL_XLBH"/> <result property="dbzxlXlmc" column="DBZXL_XLMC"/> <result property="dataYear" column="DATA_YEAR"/> <result property="dataWeekNum" column="DATA_WEEK_NUM"/> <result property="startTime" column="START_TIME"/> <result property="endTime" column="END_TIME"/> <result property="dataWeekDesc" column="DATA_WEEK_DESC"/> <result property="xbgxsfyz" column="XBGXSFYZ"/> <result property="xlzt" column="XLZT"/> <result property="xszt" column="XSZT"/> <result property="gbsl" column="GBSL"/> <result property="zbsl" column="ZBSL"/> <result property="ztqsl" column="ZTQSL"/> <result property="zqtqsl" column="ZQTQSL"/> <result property="yszgtqsl" column="YSZGTQSL"/> <result property="ysyctqsl" column="YSYCTQSL"/> <result property="cwtqsl" column="CWTQSL"/> <result property="cwgbsl" column="CWGBSL"/> <result property="cwzbsl" column="CWZBSL"/> </resultMap> <!--查询打包组合线路列表(分页)--> <select id="selectDbzhxlList" resultMap="DbzxlTpfxWeekTjMap"> SELECT DBZXL_ID, YWDWMC, DBZXL_XLBH, DBZXL_XLMC, DATA_WEEK_DESC, XBGXSFYZ, XLZT, XSZT, CWGBSL, CWZBSL FROM PWTPFX.DBZXL_TPFX_WEEK_TJ <where> <if test="dbzxlTpfxWeekTj.ssds != null and dbzxlTpfxWeekTj.ssds != ''"> AND SSDS =#{dbzxlTpfxWeekTj.ssds} </if> <if test="dbzxlTpfxWeekTj.startTime != null"> AND START_TIME =#{dbzxlTpfxWeekTj.startTime} </if> <if test="dbzxlTpfxWeekTj.ywdwmc != null and dbzxlTpfxWeekTj.ywdwmc != ''"> AND YWDWMC =#{dbzxlTpfxWeekTj.ywdwmc} </if> <if test="dbzxlTpfxWeekTj.dbzxlXlbh != null and dbzxlTpfxWeekTj.dbzxlXlbh != ''"> AND DBZXL_XLBH = #{dbzxlTpfxWeekTj.dbzxlXlbh} </if> <if test="dbzxlTpfxWeekTj.dbzxlXlmc!=null and dbzxlTpfxWeekTj.dbzxlXlmc!=''"> AND DBZXL_XLMC LIKE concat('%', #{dbzxlTpfxWeekTj.dbzxlXlmc}, '%') </if> <if test="dbzxlTpfxWeekTj.xbgxsfyz!=null and dbzxlTpfxWeekTj.xbgxsfyz!=''"> AND XBGXSFYZ = #{dbzxlTpfxWeekTj.xbgxsfyz} </if> <if test="dbzxlTpfxWeekTj.xszt!=null and dbzxlTpfxWeekTj.xszt!=''"> AND XSZT = #{dbzxlTpfxWeekTj.xszt} </if> <if test="dbzxlTpfxWeekTj.xlzt!=null and dbzxlTpfxWeekTj.xlzt!=''"> AND XLZT = #{dbzxlTpfxWeekTj.xlzt} </if> </where> ORDER BY DBZXL_ID </select> <!--查询打包组合线路列表(导出)--> <select id="selectDbzhxlListExport" resultMap="DbzxlTpfxWeekTjVoMap"> SELECT DBZXL_ID, YWDWMC, DBZXL_XLBH, DBZXL_XLMC, DATA_WEEK_DESC, XBGXSFYZ, XLZT, XSZT, CWGBSL, CWZBSL FROM PWTPFX.DBZXL_TPFX_WEEK_TJ <where> <if test="dbzxlTpfxWeekTjVo.ssds!=null and dbzxlTpfxWeekTjVo.ssds!=''"> AND SSDS =#{dbzxlTpfxWeekTjVo.ssds} </if> <if test="dbzxlTpfxWeekTjVo.ywdwmc!=null and dbzxlTpfxWeekTjVo.ywdwmc!=''"> AND YWDWMC =#{dbzxlTpfxWeekTjVo.ywdwmc} </if> <if test="dbzxlTpfxWeekTjVo.startTime!=null"> AND START_TIME =#{dbzxlTpfxWeekTjVo.startTime} </if> <if test="dbzxlTpfxWeekTjVo.dbzxlXlbh!=null and dbzxlTpfxWeekTjVo.dbzxlXlbh!=''"> AND DBZXL_XLBH = #{dbzxlTpfxWeekTjVo.dbzxlXlbh} </if> <if test="dbzxlTpfxWeekTjVo.dbzxlXlmc!=null and dbzxlTpfxWeekTjVo.dbzxlXlmc!=''"> AND DBZXL_XLMC LIKE concat('%', #{dbzxlTpfxWeekTjVo.dbzxlXlmc}, '%') </if> <if test="dbzxlTpfxWeekTjVo.xbgxsfyz!=null and dbzxlTpfxWeekTjVo.xbgxsfyz!=''"> AND XBGXSFYZ = #{dbzxlTpfxWeekTjVo.xbgxsfyz} </if> <if test="dbzxlTpfxWeekTjVo.xszt!=null and dbzxlTpfxWeekTjVo.xszt!=''"> AND XSZT = #{dbzxlTpfxWeekTjVo.xszt} </if> <if test="dbzxlTpfxWeekTjVo.xlzt!=null and dbzxlTpfxWeekTjVo.xlzt!=''"> AND XLZT = #{dbzxlTpfxWeekTjVo.xlzt} </if> <!-- <if test="dbzxlTpfxWeekTjVo.sfkg!=null and dbzxlTpfxWeekTjVo.sfkg!=''">--> <!-- AND SFKG = #{dbzxlTpfxWeekTjVo.sfkg}--> <!-- </if>--> </where> ORDER BY DBZXL_ID </select> </mapper>
注意事项
1. 分页的时候是用的 mybatis 自带的分页方法,这个用起来很简单,但是它每次只会返回一页数据,而不是一次性返回所有页的数据,这样就不能直接调用该方法来进行导出,因为一般导出是需要导出所有页的数据,而不是一页的数据,所以这个时候要重新写一个方法来查出所有数据然后导出。当然,你也可以在后台一次性把所有数据返回给前台,然后在前台进行分页,这样就只会访问一次后台,但是这样前台实现起来会比较复杂。
2. 我们在使用 easycode 生成代码的时候,数据库表跟实体类是对应的,如果涉及到多张表的查询,需要在实体类中添加属性,最好不要在 easycode 生成的实体类中直接加,因为这样会造成 mybatisplus 自带的方法 QueryWrapper 没法使用的,最好是重新建一个实体类添加属性,专门用来传值。
千万注意
我是一只刚入 java 门的菜鸟,这是刚工作的时候做的一个功能,仅供参考,希望能帮到更多的跟我一样的小菜鸟。