VUE导出EXCEL

2019-06-21  本文已影响0人  Time大王

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

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 '';
}
上一篇 下一篇

猜你喜欢

热点阅读