美文网首页
VUE导出EXCEL

VUE导出EXCEL

作者: Time大王 | 来源:发表于2019-06-21 15:51 被阅读0次

前台接收后台二进制数据的方法:

export function downExcel(query,url,fileName) {
  return request({
    url:url,
    method:'get',
    responseType: 'blob',
    params: query
  }).then((response) => {
    if (!response) {
      return
    }
    let url = window.URL.createObjectURL(new Blob([response.data]));
    let link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.setAttribute('download',fileName+getNowFormatDate()+'.xls');
    document.body.appendChild(link);
    link.click()
  })
}

文件名采用的是自定义名称加上时间戳的方式,避免文件名重复,格式为年月日时分秒

/**
 * 日期格式化
 */
export function dateFormat(date) {
  let format = 'yyyy-MM-dd hh:mm:ss';
  if (date != 'Invalid Date') {
    var o = {
      "M+": date.getMonth() + 1, //month
      "d+": date.getDate(), //day
      "h+": date.getHours(), //hour
      "m+": date.getMinutes(), //minute
      "s+": date.getSeconds(), //second
      "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
      "S": date.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
          RegExp.$1.length == 1 ? o[k] :
            ("00" + o[k]).substr(("" + o[k]).length));
    return format;
  }
  return '';
}

相关文章

网友评论

      本文标题:VUE导出EXCEL

      本文链接:https://www.haomeiwen.com/subject/wjzyqctx.html