1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| import { ZipReader, BlobReader, TextWriter, ZipWriter, BlobWriter } from '@zip.js/zip.js'; import XLSX from 'xlsx';
class KeepZero { public getKeepZeroXlsx = (blob, password, path) => { this.extractAndDecryptXlsx(blob, password, path).then(zipContent => { if (zipContent) { console.log('解密成功,文件已经处理', zipContent); const xlsxBlob = this.convertToXLSX(zipContent); console.log('xlsxBlob', xlsxBlob); const nowBolb = this.encryptAndDownload(xlsxBlob, password, path); } }); };
public extractAndDecryptXlsx = async (blob, password, path) => { try { const reader = new ZipReader(new BlobReader(blob), { password }); const entries = await reader.getEntries(); if (entries.length === 1 && !entries[0].directory) { const content = await entries[0].getData?.(new TextWriter()); await reader.close(); return { filename: path, content }; } else { console.error('ZIP 文件包含多个文件或没有文件'); return null; } } catch (err) { console.error('解密失败:', err); return null; } };
public convertToXLSX = (fileContent) => { console.log('XLSX?.utils', XLSX.utils); const workbook = XLSX.utils.book_new(); const csvData = fileContent.content.split('\n').map(line => line.split(',')); const worksheet = XLSX.utils.aoa_to_sheet(csvData); Object.keys(worksheet).forEach(cellAddress => { const cell = worksheet[cellAddress]; if (cell && typeof cell.v === 'string' && /^[0-9]+$/.test(cell.v)) { cell.z = '@'; } }); XLSX?.utils?.book_append_sheet(workbook, worksheet, 'Sheet1'); const xlsxArray = XLSX?.write?.(workbook, { bookType: 'xlsx', type: 'array' }); const xlsxBlob = new Blob([xlsxArray], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); return xlsxBlob; };
public readAndPrintXLSX = (xlsxBlob) => { const reader = new FileReader(); reader.onload = function(e) { const data = new Uint8Array(e.target.result); const workbook = XLSX.read(data, { type: 'array' }); const firstSheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[firstSheetName]; const sheetData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); console.log('打印 XLSX 内容:', sheetData); }; reader.readAsArrayBuffer(xlsxBlob); };
public encryptAndDownload = async (xlsxBlob, password, path) => { try { const zipWriter = new ZipWriter(new BlobWriter('application/zip'), { password }); await zipWriter.add(`${path}.xlsx`, new BlobReader(xlsxBlob)); const encryptedZipBlob = await zipWriter.close(); const url = URL.createObjectURL(encryptedZipBlob); const a = document.createElement('a'); a.href = url; a.download = `${path || ''}.zip`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); } catch (err) { console.error('加密失败:', err); } }; }
const keepZero = new KeepZero(); const name = action.payload.excelName; const realName = name.slice(-4) === '.zip' ? name.substring(0, name.length - 4) : name; console.log('realName', realName); keepZero.getKeepZeroXlsx(res.data, '123455', realName);
|