excel.js 1.2 KB

123456789101112131415161718192021222324252627282930
  1. exceltoxlxstream = async function (file) {
  2. return new Promise(resolve => {
  3. if (file) {
  4. const reader = new FileReader();
  5. reader.onload = async function (e) {
  6. const data = new Uint8Array(e.target.result);
  7. const workbook = XLSX.read(data, { type: 'array', codepage: 936 });
  8. const fileStream = XLSX.write(workbook, { type: 'array', bookType: 'xlsx', codepage: 936 });
  9. const blob = new Blob([fileStream], { type: 'application/vnd.ms-excel;charset=utf-8' });
  10. const url = URL.createObjectURL(blob);
  11. // 创建隐藏的下载链接并点击
  12. const downloadLink = document.createElement('a');
  13. downloadLink.href = url;
  14. downloadLink.download = file.name;
  15. downloadLink.style.display = 'none';
  16. document.body.appendChild(downloadLink);
  17. downloadLink.click();
  18. document.body.removeChild(downloadLink);
  19. // 释放资源
  20. URL.revokeObjectURL(url);
  21. resolve(fileStream);
  22. };
  23. reader.readAsArrayBuffer(file);
  24. }
  25. });
  26. }