Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the problem with writing a file using streams and not using RAM #2558

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

zlooun
Copy link

@zlooun zlooun commented Oct 17, 2023

Summary

This PR fixes the problem with writing a file using streams and not using RAM. It solves the problem of writing large excel files.

Test plan

import * as fs from 'fs';
import { stream } from 'exceljs';

const output_file_name = "/test.xlsx";

const writeStream = fs.createWriteStream(output_file_name, { flags: 'w' });
const wb = new stream.xlsx.WorkbookWriter({ stream: writeStream });
const worksheet = wb.addWorksheet("test");

const headers = Array.from({length: 256}, (_, i) => i + 1).map((i) => 'test' + i);

for (let i = 0; i < 100000; i++) {
  const row = headers.map((header) => header + '|' + i);
  await worksheet.addRow(row).commit(); // This raw will be immediately written to disk and will not clog RAM.
}

await worksheet.commit(); // This is not necessary because await wb.commit() is used, but you can also write to disk not raw by raw, but worksheet by worksheet.
await wb.commit();

Related to source code (for typings update)

https://github.com/zlooun/exceljs
https://www.npmjs.com/package/@zlooun/exceljs

@Siemienik
Copy link
Member

Thank you for the contribution; I believe it enhances the handling of large file writing 🎉. Please bear with me, as this change breaks backward-compatibility and will need to wait for the next major release (5.0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants