封装的一些常用的公共方法(1)

116 阅读1分钟

1.导出excel

export function exportData(res, name) {
  const content = res
  const blob = new Blob([content], {type: 'application/ms-excel'})
  const fileName = name
  if ('download' in document.createElement('a')) { // 非IE下载
    const elink = document.createElement('a')
    elink.download = fileName
    elink.style.display = 'none'
    elink.href = URL.createObjectURL(blob)
    document.body.appendChild(elink)
    elink.click()
    URL.revokeObjectURL(elink.href) // 释放URL 对象
    document.body.removeChild(elink)
  } else {
    navigator.msSaveBlob(blob, fileName)
  }
}

2.导出word文档

export function exportWord(res, name) {
  const content = res
  const blob = new Blob([content], {type: 'application/msword'})
  const fileName = name
  if ('download' in document.createElement('a')) { // 非IE下载
    const elink = document.createElement('a')
    elink.download = fileName
    elink.style.display = 'none'
    elink.href = URL.createObjectURL(blob)
    document.body.appendChild(elink)
    elink.click()
    URL.revokeObjectURL(elink.href) // 释放URL 对象
    document.body.removeChild(elink)
  } else {
    navigator.msSaveBlob(blob, fileName)
  }
}

3.将表格中为null或""或undefind 的内容显示为--

export function formatData(value) {
  if (value === undefined || value == '' || value == null || value == 0) {
    return '--'
  } else {
    return value
  }
}