How to use the read function from xlsx

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

xlsx.read is a function in the XLSX library that reads data from an Excel file and returns it as a JavaScript object.

44
45
46
47
48
49
50
51
52
53

module.exports = {
  parse: function(mixed, options) {
    var ws;
    if(typeof mixed === 'string') ws = XLSX.readFile(mixed, options);
    else ws = XLSX.read(mixed, options);
    return _.map(ws.Sheets, function(sheet, name) {
      return {name: name, data: XLSX.utils.sheet_to_json(sheet, {header: 1, raw: true})};
    });
  },
fork icon270
star icon0
watch icon2

1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
    data
  }
})

fs.writeFileSync(outputPath, result.content)
const workbook = xlsx.read(result.content)
const sheet = workbook.Sheets[workbook.SheetNames[0]]

should(sheet.C22.v).be.eql(data[0].category)
should(sheet.D22.v).be.eql(data[0].serie1)
fork icon197
star icon0
watch icon56

+ 5 other calls in file

How does xlsx.read work?

xlsx.read works by reading data from an Excel file and returning it as a JavaScript object.

The function takes two arguments: the data to read (in the form of a buffer or a string), and an options object that specifies how the data should be parsed.

The options object can contain properties such as type (which specifies the type of data to read), sheet (which specifies the sheet to read), and cellDates (which indicates whether cell values should be returned as dates).

Once the data has been read and parsed, it is returned as a JavaScript object that represents the contents of the Excel file.

By using xlsx.read, developers can easily read and parse Excel files in their JavaScript applications, which can be useful for working with data that is stored in Excel format.

45
46
47
48
49
50
51
52
53
54
var arr = [];
for (var i = 0; i < file.contents.length; ++i) arr[i] = String.fromCharCode(file.contents[i]);
var bString = arr.join("");

/* Call XLSX */
var workbook = XLSX.read(bString, { type: "binary" });
file.contents = new Buffer(JSON.stringify(toJson(workbook)));


if (options.trace) {
fork icon2
star icon0
watch icon0

140
141
142
143
144
145
146
147
148
149
// at least sourceFile or source has to be defined and have a value
if (!(_config.sourceFile || _config.source)) {
  throw new Error(":: 'sourceFile' or 'source' required for _config :: ");
}

let workbook = XLSX.read(_config.source, {
  type: "array",
});

let sheetsToGet =
fork icon0
star icon3
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const xlsx = require("xlsx");
const fs = require("fs");

const fileData = fs.readFileSync("example.xlsx");
const workbook = xlsx.read(fileData, { type: "buffer" });

const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];

const data = xlsx.utils.sheet_to_json(worksheet, { header: 1 });

console.log(data);

In this example, we use xlsx.read to read data from an Excel file and convert it to a JavaScript object. We first import the necessary modules from xlsx and fs. We then use fs.readFileSync to read the contents of an Excel file into a buffer. We use xlsx.read to parse the Excel data, specifying that the data is in buffer format. We get the name of the first sheet in the workbook using workbook.SheetNames[0], and then get a reference to the worksheet using workbook.Sheets[sheetName]. We use xlsx.utils.sheet_to_json to convert the worksheet data to a JavaScript object, specifying that the first row contains headers. Finally, we log the resulting data to the console. By using xlsx.read and related functions from the xlsx library, we can easily read and parse Excel files in our JavaScript applications, which can be useful for working with data that is stored in Excel format.

950
951
952
953
954
955
956
957
958
959
try {

    uploadPerguntas(req, res, async (err) => {
        let file = fs.readFileSync(req.file.path)

        const workbook = xlsx.read(file, { type: 'array' })

        const firstSheetName = workbook.SheetNames[0]

        const worksheet = workbook.Sheets[firstSheetName]
fork icon0
star icon2
watch icon2

+ 14 other calls in file

2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
let fileName = data.fileName;
let fileId = _req.params.fileId;
let collectionName = 'userMgmt.users';
getSheetDataFromGridFS(fileId, mongoose.connection.db, collectionName)
	.then((bufferData) => {
		let wb = XLSX.read(bufferData, {
			type: 'buffer',
			cellDates: true,
			cellNF: false,
			cellText: true,
fork icon3
star icon0
watch icon4

417
418
419
420
421
422
423
424
425
426
var arr = new Array();
for (var i = 0; i != data.length; ++i)
  arr[i] = String.fromCharCode(data[i]);
var bstr = arr.join("");
/* Call XLSX */
var workbook = XLSX.read(bstr, {
  type: "binary",
});
/* Get the work sheet name */
var first_sheet_name = workbook.SheetNames[0];
fork icon0
star icon0
watch icon1

573
574
575
576
577
578
579
580
581
582
const onChangeHandler = (event) => {
  if (excelOrCsv.target.files) {
    const reader = new FileReader();
    reader.onload = (excelOrCsv) => {
      const bstr = excelOrCsv.target.result;
      const wb = XLSX.read(bstr, {type: 'binary'});
      console.log('wb', wb);

      const productsSheet = wb.Sheets['PRODUCTOS'];
      const productsData = XLSX.utils.sheet_to_json(productsSheet, {
fork icon0
star icon0
watch icon1

41
42
43
44
45
46
47
48
49
50
    }
})
/* POST Schedules; stole this from cameron and gian, needs modifications*/
router.post('/', upload.single('data'), async (req, res, next) => {
    
    let workbook = XLSX.read(req.file.buffer);
    //let workbook = XLSX.readFile("./Example Absences (Fall 2017-018).xlsx")
  
    let data = formatSchedule(workbook);
    let errors = [];
fork icon0
star icon0
watch icon1

+ 2 other calls in file

376
377
378
379
380
381
382
383
384
385
const event = req.body.event;
console.log(req.body);
const eventSheetName = event.replace(/\s+/g, ''); // Remove whitespace from the event name
const excelFile = req.files.excel_file;

/*const workbook = xlsx.read(file.data);
const sheet = workbook.Sheets[workbook.SheetNames[0]];
const data = xlsx.utils.sheet_to_json(sheet);   

//check if the format is correct
fork icon0
star icon0
watch icon1

+ 3 other calls in file

99
100
101
102
103
104
105
106
107
108
var reader = new FileReader();
// 读取上传文件为二进制
reader.readAsBinaryString(file);
reader.onload = function(event) {
  var data = event.target.result;
  var workbook = XLSX.read(data, { type: 'binary' });
  workbook.SheetNames.forEach(function(sheetName) {
    var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
    if (XL_row_object.length > 0) {
      callback(XL_row_object);
fork icon0
star icon0
watch icon1

+ 3 other calls in file

39
40
41
42
43
44
45
46
47
48
let fileReader = new FileReader();
let excelData;
      fileReader.readAsBinaryString(excelFile);
      fileReader.onload = (event)=>{
       let data = event.target.result;
       let workbook = XLSX.read(data,{type:"binary"});
       console.log(workbook);
       workbook.SheetNames.forEach(sheet => {
            let rowObject = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheet]);
            console.log(rowObject);
fork icon0
star icon0
watch icon1

+ 3 other calls in file

67
68
69
70
71
72
73
74
75
76
file.on('data', (data) => {
  buffers.push(data)
})
file.on('end', async () => {
  let buffer = Buffer.concat(buffers)
  let workbook = XLSX.read(buffer, {
    type: 'buffer',
  })
  let promo = new Promotion({ annee: req.body.promotion })
  const firstSheetName = workbook.SheetNames[0]
fork icon0
star icon0
watch icon1

17
18
19
20
21
22
23
24
25
26
function readStreamExcel(stream/*:ReadStream*/, cb/*:(wb:Workbook)=>void*/)/*:void*/ {
    var buffers = [];
    stream.on('data', function (data) { buffers.push(data); });
    stream.on('end', function () {
        let buffer = Buffer.concat(buffers);
        const workbook = XLSX.read(buffer, {
            type: "buffer",
            WTF: true
        });
        const sheetNames = workbook.SheetNames;
fork icon0
star icon0
watch icon0

24
25
26
27
28
29
30
31
32
33
var f = Object.values(files)[0];
if(!f) {
	context.res = { status: 400, body: "Must submit a file for processing!" };
} else {
	/* file is stored in a temp directory, so we can point to that and read it */
	const wb = XLSX.read(f.path, {type:"file"});

	/* convert to specified output type -- default CSV */
	const ext = (fields.bookType || "csv").toLowerCase();
	const out = XLSX.write(wb, {type:"string", bookType:ext});
fork icon0
star icon0
watch icon0