const CRC_TABLE = (() => {
const table = new Uint32Array(256);
for (let i = 0; i < 256; i += 1) {
let c = i;
for (let k = 0; k < 8; k += 1) {
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
}
table[i] = c >>> 0;
}
return table;
})();
function crc32(bytes) {
let crc = 0xffffffff;
for (let i = 0; i < bytes.length; i += 1) {
crc = CRC_TABLE[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8);
}
return (crc ^ 0xffffffff) >>> 0;
}
function u16(n) {
return Uint8Array.of(n & 0xff, (n >>> 8) & 0xff);
}
function u32(n) {
return Uint8Array.of(n & 0xff, (n >>> 8) & 0xff, (n >>> 16) & 0xff, (n >>> 24) & 0xff);
}
function utf8Bytes(str) {
if (typeof TextEncoder === 'function') {
return new TextEncoder().encode(str);
}
const encoded = unescape(encodeURIComponent(str));
const out = new Uint8Array(encoded.length);
for (let i = 0; i < encoded.length; i += 1) {
out[i] = encoded.charCodeAt(i);
}
return out;
}
function concatBytes(parts) {
let len = 0;
for (let i = 0; i < parts.length; i += 1) len += parts[i].length;
const out = new Uint8Array(len);
let offset = 0;
for (let i = 0; i < parts.length; i += 1) {
out.set(parts[i], offset);
offset += parts[i].length;
}
return out;
}
function colLetter(index) {
let n = index + 1;
let s = '';
while (n > 0) {
const rem = (n - 1) % 26;
s = String.fromCharCode(65 + rem) + s;
n = Math.floor((n - 1) / 26);
}
return s;
}
function xmlEscape(value) {
return String(value)
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"');
}
function createSharedStringTable() {
const indexByValue = Object.create(null);
const values = [];
return {
intern(text) {
const key = String(text);
if (Object.prototype.hasOwnProperty.call(indexByValue, key)) {
return indexByValue[key];
}
const idx = values.length;
indexByValue[key] = idx;
values.push(key);
return idx;
},
toXml() {
const items = values
.map((v) => `${xmlEscape(v)}`)
.join('');
return (
`` +
`` +
`${items}`
);
},
};
}
function cellXml(col, row, value, sst) {
const ref = `${colLetter(col)}${row}`;
if (value == null || value === '') return ``;
if (typeof value === 'number' && Number.isFinite(value)) {
return `${value}`;
}
return `${sst.intern(value)}`;
}
function sheetXml(headers, rows, sst) {
const width = Math.max(headers.length, 1);
const headerRow = `${headers.map((h, i) => cellXml(i, 1, h, sst)).join('')}
`;
const body = rows
.map((row, idx) => {
const r = idx + 2;
const cells = [];
for (let c = 0; c < width; c += 1) cells.push(cellXml(c, r, row[c], sst));
return `${cells.join('')}
`;
})
.join('');
const lastCol = colLetter(width - 1);
const lastRow = rows.length + 1;
return (
`` +
`` +
`` +
`${headerRow}${body}` +
``
);
}
function zipStore(files) {
const locals = [];
const centrals = [];
let offset = 0;
for (let i = 0; i < files.length; i += 1) {
const nameBytes = utf8Bytes(files[i].name);
const data = files[i].data;
const crc = crc32(data);
const local = concatBytes([
u32(0x04034b50),
u16(20),
u16(0),
u16(0),
u16(0),
u16(0),
u32(crc),
u32(data.length),
u32(data.length),
u16(nameBytes.length),
u16(0),
nameBytes,
data,
]);
const central = concatBytes([
u32(0x02014b50),
u16(20),
u16(20),
u16(0),
u16(0),
u16(0),
u16(0),
u32(crc),
u32(data.length),
u32(data.length),
u16(nameBytes.length),
u16(0),
u16(0),
u16(0),
u16(0),
u32(0),
u32(offset),
nameBytes,
]);
locals.push(local);
centrals.push(central);
offset += local.length;
}
const centralDir = concatBytes(centrals);
const eocd = concatBytes([
u32(0x06054b50),
u16(0),
u16(0),
u16(files.length),
u16(files.length),
u32(centralDir.length),
u32(offset),
u16(0),
]);
return concatBytes([...locals, centralDir, eocd]);
}
function buildSimpleXlsxBytes(sheetName, headers, rows) {
const safeName = String(sheetName || 'Sheet1')
.replace(/[\\/*?:\[\]]/g, '_')
.slice(0, 31) || 'Sheet1';
const sst = createSharedStringTable();
const sheet = sheetXml(headers, rows, sst);
const workbook =
`` +
`` +
`` +
``;
const workbookRels =
`` +
`` +
`` +
`` +
``;
const rootRels =
`` +
`` +
`` +
``;
const contentTypes =
`` +
`` +
`` +
`` +
`` +
`` +
`` +
``;
return zipStore([
{ name: '[Content_Types].xml', data: utf8Bytes(contentTypes) },
{ name: '_rels/.rels', data: utf8Bytes(rootRels) },
{ name: 'xl/workbook.xml', data: utf8Bytes(workbook) },
{ name: 'xl/_rels/workbook.xml.rels', data: utf8Bytes(workbookRels) },
{ name: 'xl/sharedStrings.xml', data: utf8Bytes(sst.toXml()) },
{ name: 'xl/worksheets/sheet1.xml', data: utf8Bytes(sheet) },
]);
}
module.exports = { buildSimpleXlsxBytes };