How to use the stream function from xlsx

Find comprehensive JavaScript xlsx.stream code examples handpicked from public code repositorys.

xlsx.stream is a module in the xlsx package that provides streaming APIs for reading and writing Excel files in Node.js.

17
18
19
20
21
22
23
24
25
26
const dataWriter = await Datatable.create();

const worksheet = workbook.Sheets[args.sheetname || workbook.SheetNames[0]];
const range = (isInteger(args.range) && `A${args.range}:`) || args.range || undefined;

const stream = XLSX.stream.to_json(
  worksheet,
  {
    range,
    raw: false,
fork icon0
star icon0
watch icon1

234
235
236
237
238
239
240
241
242
243
    //返回配置
    let ret_sheet = XLSX.utils.json_to_sheet(ret);
    ret_sheet['!cols'] = [{wpx:100},{wpx:100},{wpx:100},{wpx:200},{wpx:60},{wpx:60}];
    XLSX.utils.book_append_sheet(wb, ret_sheet, '返回配置');

    return XLSX.stream.to_json(wb);
},

/**
 * 根据api配置组装markdown格式文档
fork icon0
star icon0
watch icon1

+ 2 other calls in file

How does xlsx.stream work?

xlsx.stream is a module in the xlsx package that provides streaming APIs for reading and writing Excel files in Node.js. It provides a way to read and write Excel files in smaller, more manageable chunks, rather than loading the entire file into memory at once. This can be particularly useful when working with large Excel files, as it allows you to process the data in the file without running into memory constraints. The xlsx.stream module provides several classes for working with streaming data in Excel files, including: ReadStream: A readable stream that reads data from an Excel file. WriteStream: A writable stream that writes data to an Excel file. WorksheetReader: A class that reads a single worksheet from an Excel file. WorksheetWriter: A class that writes data to a single worksheet in an Excel file. To use xlsx.stream, you would typically create an instance of one of the stream classes, and then pipe data to or from that stream as needed. For example, you might create a ReadStream instance to read data from an Excel file, and then pipe that data to a WorksheetReader instance to extract the data from a specific worksheet in the file. Similarly, you might create a WriteStream instance to write data to an Excel file, and then pipe that data from a WorksheetWriter instance to write the data to a specific worksheet in the file. Overall, xlsx.stream provides a powerful and flexible way to work with Excel files in Node.js, particularly when dealing with large or complex files that would be difficult to work with using traditional in-memory APIs.

766
767
768
769
770
771
772
773
774
775
// let jdata = JSON.stringify(res.locals.data);
// jdata = JSON.parse(jdata);

const csvContent = xlsx.utils.sheet_to_csv(xlsx.utils.json_to_sheet(res.locals.data));
// var stream = xlsx.stream.to_csv(xlsx.utils.json_to_sheet(jdata));
// var stream = xlsx.stream.to_csv(csvContent);

// res.writeHead(200, {
//     'Content-Type': 'application/octet-stream', //告诉浏览器这是一个二进制文件
//     'Content-Disposition': 'attachment; filename=test.csv', //告诉浏览器这是一个需要下载的文件
fork icon0
star icon0
watch icon0

+ 3 other calls in file

269
270
271
272
273
274
275
276
277
278
  doit(function (ws) { return JSON.stringify(X.utils.sheet_to_json(ws, jso)); });
  break;

default:
  if (!program.book) {
    var stream = X.stream.to_csv(ws, { FS: program.fieldSep, RS: program.rowSep });
    if (program.output) stream.pipe(fs.createWriteStream(program.output));
    else stream.pipe(process.stdout);
  } else doit(function (ws) { return X.utils.sheet_to_csv(ws, { FS: program.fieldSep, RS: program.rowSep }); });
  break;
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const XLSX = require("xlsx");
const fs = require("fs");
const { ReadStream, WorksheetReader } = require("xlsx-stream");

// Create a read stream for the Excel file
const fileStream = fs.createReadStream("example.xlsx");

// Create a worksheet reader for the sheet we want to read
const reader = new WorksheetReader({ sheet: "Sheet1" });

// Pipe the file stream to the worksheet reader
fileStream.pipe(reader);

// Listen for data events from the worksheet reader
reader.on("data", (data) => {
  console.log(data);
});

// Listen for the end event from the worksheet reader
reader.on("end", () => {
  console.log("Finished reading data");
});

In this example, we first create a ReadStream instance using the built-in fs module to read from an Excel file called example.xlsx. We then create a WorksheetReader instance that is configured to read from the first sheet in the workbook (named 'Sheet1'). We pipe the fileStream to the reader, which starts reading data from the sheet. We then listen for data events from the reader, which are emitted whenever a row of data is read from the sheet. We log each row of data to the console. Finally, we listen for the end event from the reader, which is emitted when all data has been read from the sheet. We log a message indicating that we have finished reading the data. This is just a basic example, but it demonstrates how you can use xlsx.stream to read data from an Excel file in Node.js. You can also use the WriteStream and WorksheetWriter classes to write data to an Excel file using a similar streaming approach.