Skip to content

Instantly share code, notes, and snippets.

@YuriFontella
Created December 15, 2022 21:26
Show Gist options
  • Save YuriFontella/888eb05cc991f3bc2060de166f00a9aa to your computer and use it in GitHub Desktop.
Save YuriFontella/888eb05cc991f3bc2060de166f00a9aa to your computer and use it in GitHub Desktop.
const xlsx = require('xlsx')
const { Writable, Transform } = require('stream')
const fs = require('fs')
const path = require('path')
console.log('início')
const workbook = xlsx.readFile('data-600.xlsx', { sheetStubs: true })
const sheet_name_list = workbook.SheetNames
const readableStreamFile = xlsx.stream.to_json(workbook.Sheets[sheet_name_list[0]], { raw: false })
const transformToString = new Transform({
writableObjectMode: true,
transform(chunk, encoding, callback) {
callback(null, JSON.stringify(chunk))
}
})
const writableStreamFile = new Writable({
write(chunk, encoding, next) {
const stringifyer = chunk.toString()
const rowData = JSON.parse(stringifyer)
fs.appendFileSync(path.resolve('data.log'), JSON.stringify(rowData) + '\n')
next()
},
})
console.log('Iniciou', Date())
readableStreamFile
.pipe(transformToString)
.pipe(writableStreamFile)
.on('close', () => console.log('Finalizou', Date()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment