当前位置:   article > 正文

VUE如何下载表格,自动下载以及让用户选择路径下载(弹窗windows文件夹自动写入资源)_vue文件下载弹出目录选择框

vue文件下载弹出目录选择框
  1. const download = async function () {
  2. const { code, endTime, startTime, dataType, type, tabValue, timeType } = form
  3. const params = {
  4. code,
  5. endTime,
  6. startTime,
  7. dataType,
  8. type: tabValue === '计算数据' ? null : type[0],
  9. timeType: timeType[0] === 'all' ? null : timeType[0] <= 2 ? timeType[0] : null
  10. }
  11. const res = await queryData(params)
  12. let downData = []
  13. let max = 0
  14. for (const key in res) {
  15. if (key !== 'columnVO') {
  16. if (res[key].length > max) {
  17. max = res[key].length
  18. downData = res[key]
  19. }
  20. }
  21. }
  22. const csvContent = convertArrayOfObjectsToCSV(downData)
  23. let blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' })
  24. // 将数据转换为工作簿
  25. const worksheet = XLSX.utils.json_to_sheet(downData);
  26. const workbook = XLSX.utils.book_new();
  27. XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
  28. // 生成二进制字符串并触发下载
  29. const wbout = XLSX.write(workbook, {bookType:'xlsx', type:'binary'});
  30. blob = new Blob([s2ab(wbout)], {type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
  31. const url = URL.createObjectURL(blob);
  32. const a = document.createElement("a");
  33. a.href = url;
  34. a.download = "data.xlsx"; // 设置下载的文件名
  35. document.body.appendChild(a);
  36. a.click();
  37. document.body.removeChild(a);
  38. URL.revokeObjectURL(url);
  39. //todo ue下载表格
  40. console.log('等待ue进行下载操作', blob)
  41. console.log("拿到的json数据======",res)
  42. // 自定义写入资源
  43. try {
  44. if ('showSaveFilePicker' in window) {
  45. const handle = await window.showSaveFilePicker();
  46. const writable = await handle.createWritable();
  47. await writable.write(blob);
  48. await writable.close();
  49. console.log('文件已成功写入');
  50. } else {
  51. throw new Error('浏览器不支持 File System Access API');
  52. }
  53. } catch (error) {
  54. console.error('写入文件失败:', error);
  55. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/683378
推荐阅读
相关标签
  

闽ICP备14008679号