banner
Hi my new friend!

山河浪漫,
人间温暖。

Scroll down

1、接口返回的是文件流

function download_excel (data) { 
    const aLink = document.createElement('a')
    const blob = new Blob([res], {  type: 'application/vnd.ms-excel,charset=utf-8' })
    aLink.href = URL.createObjectURL(blob)
    aLink.setAttribute('download', 'example' + '.xlsx')
    aLink.click()
}

2、接口返回的是base64

// 方法1 function download_excel (data) { const sliceSize = 512; const byteCharacters = atob(data); const byteArrays = []; for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) { const slice = byteCharacters.slice(offset, offset + sliceSize); const byteNumbers = new Array(slice.length); for (let i = 0; i < slice.length; i++) { byteNumbers[i] = slice.charCodeAt(i); } const byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); } const file = new File(byteArrays, 'example.xls', { type: 'application/vnd.ms-excel,charset=utf-8'}); const aLink = document.createElement('a') aLink.href = URL.createObjectURL(file) aLink.setAttribute('download', 'example' + '.xls') aLink.click() } // 方法2 function download_excel (data) { const raw = atob(dat) const uInt8Array = new Uint8Array(raw.length) for (let i = 0; i < raw.length; i++) { uInt8Array[i] = raw.charCodeAt(i) } const aLink = document.createElement('a') const blob = new Blob([uInt8Array], { type: 'application/vnd.ms-excel,charset=utf-8' }) aLink.href = URL.createObjectURL(blob) aLink.setAttribute('download', 'example' + '.xlsx') aLink.click() }

原文链接:https://blog.csdn.net/qq_43249953/article/details/142137084

我很可爱,请给我钱

昵称
邮箱
0/200
  • 😂
  • 😀
  • 😅
  • 😊
  • 🙂
  • 🙃
  • 😌
  • 😍
  • 😘
  • 😜
  • 😝
  • 😏
  • 😒
  • 🙄
  • 😳
  • 😡
  • 😔
  • 😫
  • 😱
  • 😭
  • 💩
  • 👻
  • 🙌
  • 🖕
  • 👍
  • 👫
  • 👬
  • 👭
  • 🌚
  • 🌝
  • 🙈
  • 💊
  • 😶
  • 🙏
  • 🍦
  • 🍉
  • 😣
  • OωO
  • |´・ω・)ノ
  • ヾ(≧∇≦*)ゝ
  • (☆ω☆)
  • (╯‵□′)╯︵┴─┴
  •  ̄﹃ ̄
  • (/ω\)
  • ∠( ᐛ 」∠)_
  • (๑•̀ㅁ•́ฅ)
  • →_→
  • ୧(๑•̀⌄•́๑)૭
  • ٩(ˊᗜˋ*)و
  • (ノ°ο°)ノ
  • (´இ皿இ`)
  • ⌇●﹏●⌇
  • (ฅ´ω`ฅ)
  • (╯°A°)╯︵○○○
  • φ( ̄∇ ̄o)
  • ヾ(´・ ・`。)ノ"
  • ( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
  • (ó﹏ò。)
  • Σ(っ °Д °;)っ
  • ( ,,´・ω・)ノ"(´っω・`。)
  • ╮(╯▽╰)╭
  • o(*////▽////*)q
  • >﹏<
  • ( ๑´•ω•) "(ㆆᴗㆆ)
  • Emoji
  • 颜文字
0 条评论