Vue请求SSM的后台接口获得要导出Excel的数据

前言

Vue实现导出Excel,所需要的数据时通过异步请求后台获取,

后台将查询到的数据返回给前端,然后前端实现导出Excel。

Vue实现导出Excel:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/83789067

现在来看怎样请求后端并获取数据的部分。

前端

点击按钮调用方法

<el-button type="primary" class="mt_0 ml_1em  bg_gray_777" @click="exportClick()">导出账单</el-button>

请求后台的方法

 //报表导出前的查询
      exportClick(){
        this.loading = true;
        exportMerchantBill(this.billForm).then(response => {
          debugger;
          this.loading = false;
          const data = response.data;
          this.exportList = data;
          //开始执行导出方法
          this.export2Excel()
        }).catch(error => {
          debugger;
          this.loading = false
          console.log(error)
        })
        },

在data中的billForm:

 billForm: {
          startDate: '',
          endDate: '',
          currentPage: 1,
          pageSize: 10,
        },

引入 exportMerchantBill

import { searchMerchantBillPageResult,exportMerchantBill} from '@/api/account'

发起请求的方法

export function exportMerchantBill(data) {
  return fetch({
    headers: {"Content-Type": "application/json"},
    url: '/merchant/bill/exportMerchantBill',
    method: 'post',
    data: {
      billForm: data
    }
  })
}

后台

action

@Controller
@RequestMapping("merchant/bill")
public class MerchantBillSerialAction {
 
 private BusMerchantBillserialService service;

 @Autowired
 public void setServce(BusMerchantBillserialService service) {
  this.service = service;
 }

@ResponseBody
 @RequestMapping("/exportMerchantBill") 
 public Json<List<BusMerchantBillserialExt>> exportMerchantBill(@RequestBody Map map){
  try {
   ParamBean reqJson = JSON.parseObject(JSON.toJSONString(map.get("billForm")),
     ParamBean.class);
   reqJson.setCurrentPage(null);
   reqJson.setPageSize(null);
   // 获取当前用户
   JwtUserModel userModel = ResourceUtil.getCurrentUser();
   PageResult<BusMerchantBillserialExt> pageResult = new  PageResult<BusMerchantBillserialExt>();
   Map<String,Object> param = pageResult.getParam();
   if(reqJson.getStartDate()!=null) {
    reqJson.setStartDateStr(DateConvert.formatDateToString(reqJson.getStartDate(), DateStyle.YYYY_MM_DD) +" 00:00:00" );
    param.put("startTime", reqJson.getStartDateStr());
   }
   if(reqJson.getEndDate()!=null) {
    reqJson.setEndDateStr(DateConvert.formatDateToString(reqJson.getEndDate(), DateStyle.YYYY_MM_DD) +" 23:59:59" );
    param.put("endTime", reqJson.getEndDateStr());
   }
  
   param.put("merchantId", userModel.getUserId());
   param.put("status",Constants.IS_DATA_STATUS);
   param.put("orderField","recordTime");
   param.put("orderDirection","desc");
   pageResult.setParam(param);
   List<BusMerchantBillserialExt> data= service.searchPageMerchantBillForExportByParam(param);
   return new Json<List<BusMerchantBillserialExt>>().success(data);
  } catch (Exception e) {
   LogService.getInstance(this).error("查询账单信息失败",e);
   return new Json<List<BusMerchantBillserialExt>>().fail();
  }
 }

serviceImpl

@Override
 public List<BusMerchantBillserialExt> searchPageMerchantBillForExportByParam(Map<String, Object> param) {
  if (!"".equals(param.get("orderField")))
  {
   param.put("orderColumn", param.get("orderField"));
  }
  param.put("orderTurn",  param.get("orderDirection"));
  List<BusMerchantBillserialExt> data = dao.getMerchantBillByParam(param);
  return data;
 }

相应的mapper以及model不再提供,这里只是展示大概的思想,具体代码要根据自己的业务去编写。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值