xlsx-export.ts 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // 小型纯文本 SpreadsheetML 导出;inlineStr 避免公式执行与号码精度变化。
  2. const encoder = new TextEncoder();
  3. const xml = (value: string) => value.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/g, "").replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;");
  4. function crc32(bytes: Uint8Array) {
  5. let crc = 0xffffffff;
  6. for (const byte of bytes) { crc ^= byte; for (let bit = 0; bit < 8; bit++) crc = (crc >>> 1) ^ ((crc & 1) ? 0xedb88320 : 0); }
  7. return (crc ^ 0xffffffff) >>> 0;
  8. }
  9. function zip(files: [string, string][]) {
  10. const chunks: Uint8Array[] = [], central: Uint8Array[] = [];
  11. let offset = 0;
  12. for (const [path, content] of files) {
  13. const name = encoder.encode(path), bytes = encoder.encode(content), crc = crc32(bytes);
  14. const header = new Uint8Array(30 + name.length), h = new DataView(header.buffer);
  15. h.setUint32(0, 0x04034b50, true); h.setUint16(4, 20, true); h.setUint16(6, 0x800, true); h.setUint32(14, crc, true); h.setUint32(18, bytes.length, true); h.setUint32(22, bytes.length, true); h.setUint16(26, name.length, true); header.set(name, 30);
  16. const directory = new Uint8Array(46 + name.length), d = new DataView(directory.buffer);
  17. d.setUint32(0, 0x02014b50, true); d.setUint16(4, 20, true); d.setUint16(6, 20, true); d.setUint16(8, 0x800, true); d.setUint32(16, crc, true); d.setUint32(20, bytes.length, true); d.setUint32(24, bytes.length, true); d.setUint16(28, name.length, true); d.setUint32(42, offset, true); directory.set(name, 46);
  18. chunks.push(header, bytes); central.push(directory); offset += header.length + bytes.length;
  19. }
  20. const size = central.reduce((sum, bytes) => sum + bytes.length, 0), footer = new Uint8Array(22), f = new DataView(footer.buffer);
  21. f.setUint32(0, 0x06054b50, true); f.setUint16(8, files.length, true); f.setUint16(10, files.length, true); f.setUint32(12, size, true); f.setUint32(16, offset, true);
  22. const result = new Uint8Array(offset + size + footer.length); let cursor = 0;
  23. for (const chunk of [...chunks, ...central, footer]) { result.set(chunk, cursor); cursor += chunk.length; }
  24. return result;
  25. }
  26. function column(index: number): string { return index < 26 ? String.fromCharCode(65 + index) : column(Math.floor(index / 26) - 1) + column(index % 26); }
  27. export function textWorkbook(rows: string[][], sheetName = "激活码清单") {
  28. const width = Math.max(1, ...rows.map((row) => row.length));
  29. const sheet = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetViews><sheetView workbookViewId="0"><pane ySplit="2" topLeftCell="A3" activePane="bottomLeft" state="frozen"/></sheetView></sheetViews><cols>${Array.from({ length: width }, (_, i) => `<col min="${i + 1}" max="${i + 1}" width="${i === 1 || i === 2 ? 32 : 23}" customWidth="1"/>`).join("")}</cols><sheetData>${rows.map((row, i) => `<row r="${i + 1}">${row.map((cell, j) => `<c r="${column(j)}${i + 1}" t="inlineStr"><is><t xml:space="preserve">${xml(cell)}</t></is></c>`).join("")}</row>`).join("")}</sheetData><autoFilter ref="A2:${column(width - 1)}${Math.max(2, rows.length)}"/></worksheet>`;
  30. return zip([
  31. ["[Content_Types].xml", '<?xml version="1.0"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/><Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/></Types>'],
  32. ["_rels/.rels", '<?xml version="1.0"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>'],
  33. ["xl/workbook.xml", `<?xml version="1.0"?><workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="${xml(sheetName)}" sheetId="1" r:id="rId1"/></sheets></workbook>`],
  34. ["xl/_rels/workbook.xml.rels", '<?xml version="1.0"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/></Relationships>'],
  35. ["xl/worksheets/sheet1.xml", sheet],
  36. ]);
  37. }