How to use the readFile function from xlsx

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

xlsx.readFile is a function in Node.js that allows you to read and parse the data from an Excel file.

43
44
45
46
47
48
49
50
51
52
}

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

52
53
54
55
56
57
58
59
60
61
  ob[sheetname] = t;
  o = ob;
} else {
  o = t;
}
var wb = fs.existsSync(filename) ? xlsx.readFile(filename) : new Workbook();

for (ws_name in o) {
  var sheetdispname = sheetname || ws_name;
  var twodarr = o[ws_name];
fork icon15
star icon23
watch icon3

How does xlsx.readFile work?

xlsx.readFile is a function that reads an Excel file and returns its contents as a JavaScript object, where each sheet of the Excel file is represented as an array of arrays of cell values, and the sheet names are used as object property names.

0
1
2
3
4
5
6
7
8
9
"use strict";
var fs = require('fs');
var process = require('process');
var workingDirectory = process.cwd().slice(2);
var XLSX = require('xlsx');
var workbook = XLSX.readFile(process.argv[2]);
var sheets = workbook.Sheets;
var htmlFile = '';
var rowNumber;
var htmlArray;
fork icon2
star icon6
watch icon2

+ 4 other calls in file

0
1
2
3
4
5
6
7
8
9
"use strict";
let fs = require('fs');
let process = require('process');
let workingDirectory = process.cwd().slice(2);
let XLSX = require('xlsx');
let workbook = XLSX.readFile(process.argv[2]);
let sheets = workbook.Sheets;
let htmlFile = '';
let rowNumber;
let htmlArray;
fork icon0
star icon2
watch icon1

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
const XLSX = require("xlsx");

// Read data from file
const workbook = XLSX.readFile("example.xlsx");
const worksheet = workbook.Sheets["Sheet1"];
const data = XLSX.utils.sheet_to_json(worksheet);

// Print data
console.log(data);

This code reads the data from an Excel file named "example.xlsx" and stores it in the data variable as a JSON object. It then logs the data to the console.

96
97
98
99
100
101
102
103
104
105
106
107
}


function parseStandardTextFile(fileName, doc) {
  let sheet = [];
  try {
    let workbook = XLSX.readFile(fileName);


    // EveryAction export file only has one sheet
    let sheetName = workbook.SheetNames[0];
    let xlsxSheet = workbook.Sheets[sheetName];
fork icon2
star icon0
watch icon1

+ 3 other calls in file

-1
fork icon1
star icon0
watch icon1

+ 14 other calls in file

6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
    }
)
.then(async () => {
    const parseExcel = (filename) => {

        const excelData = XLSX.readFile(filename);


        const maxRow = 6;
        const minRow = 3;
fork icon1
star icon0
watch icon2

+ 12 other calls in file

62
63
64
65
66
67
68
69
70
71

// Main method to read our xls/xlsx file:
const parseExcel = (fileName) => {
    console.log(" [MENU.JS] ======================================================================");
    console.log(" [MENU.JS] ========= Lendo o arquivo: " + fileName + " ===========");
    const excelData = xlsx.readFile(fileName);
    return Object.keys(excelData.Sheets).map(name => ({
        name,
        data: xlsx.utils.sheet_to_json(excelData.Sheets[name])
    }));
fork icon0
star icon7
watch icon2

106
107
108
109
110
111
112
113
114
115
}
async function generatedWebSocket() {
   const fileBase = await File.findById(file).lean();
   const filePath = fileBase.file;
   const pathName = fileBase.fileName.split('_')[0];
   const workbook = xlsx.readFile(`public${filePath}`);
   const worksheet = workbook.Sheets[workbook.SheetNames[0]];
   const rows = xlsx.utils.sheet_to_json(worksheet);
   
   if (rows !== undefined) {
fork icon0
star icon1
watch icon1

+ 7 other calls in file

30
31
32
33
34
35
36
37
38
39
40
const createStaffingEntry = async entryDetails => {
  const newEntry = await db.staffing_details.bulkCreate(entryDetails);
  return newEntry;
};
const parse_xlsx_sheets = fname => {
  const file = reader.readFile(fname);
  const sheets = file.SheetNames;


  var temp = reader.utils.sheet_to_json(file.Sheets[sheets[0]]);
  return Promise.all(
fork icon0
star icon1
watch icon1

254
255
256
257
258
259
260
261
262
263
    ret.image = cv.imencode(".jpg", image).toString("base64");
    return ret;
},
"file-users": async function (path) {
    let users = {};
    let workbook = XLSX.readFile(formatFilename(path));
    let sheet_name_list = workbook.SheetNames;
    let xlData = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]);
    for (let i = 0; i < xlData.length; i++) {
        let keys = Object.keys(xlData[i]);
fork icon0
star icon1
watch icon1

+ 6 other calls in file

4
5
6
7
8
9
10
11
12
13
14
15
router.get('/api/gympopulationdata', async (req, res) => {
    res.json({currentPop: getGymPopData()});
});


// Get data from xlsx file
const workbook = xlsx.readFile('./server/database/occupation-data-xpert-gym.xlsx');
const data = xlsx.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]);


function getGymPopData() {
    // Get current hour
fork icon0
star icon1
watch icon1

15
16
17
18
19
20
21
22
23
24
var configObj = {
  "strChannel": req.body.channel,
  "excelPath": `${appRoot}/` + req.file.path
}

var workbook = XLSX.readFile(`${appRoot}/` + req.file.path);
var sheet_name_list = workbook.SheetNames;
var xlData = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]);

for (var i = 0; i < xlData.length; i++) {
fork icon0
star icon1
watch icon4

98
99
100
101
102
103
104
105
106
107
108
}


// createFile()
//可以读取成功
async function readFile(browser) {
    var workbook = xlsx.readFile('./file/excel/input.xlsx');
    // sheet名
    var first_sheet_name = workbook.SheetNames[0];
    //单元格
    var worksheet = workbook.Sheets[first_sheet_name];
fork icon0
star icon1
watch icon2

229
230
231
232
233
234
235
236
237
238
239
    },
  ]);
};


exports.excelToJson = async (filePath) => {
  const wb = XLSX.readFile(filePath);
  const ws = wb.Sheets["Sheet1"];
  const data = XLSX.utils.sheet_to_json(ws);
  // console.log(data);
  return data;
fork icon2
star icon0
watch icon3

+ 2 other calls in file

290
291
292
293
294
295
296
297
298
299
    }
  }
}
async getAllPropertyDetails(name, k, tracks_data) {
  // reading excel file
  const workbook = XLSX.readFile(path.resolve() + '/uploads/' + name);
  const sheet_name_list = workbook.SheetNames;
  const xlData = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]], { defval: "" });

  //taking a map to store the data after grouping.
fork icon0
star icon0
watch icon1

168
169
170
171
172
173
174
175
176
177
178
  }
};


const postMultipleTrabajador = async (req, res, next) => {
  try {
    const workbook = XLSX.readFile("./upload/data.xlsx");
    const workbookSheets = workbook.SheetNames;
    const sheet = workbookSheets[0];
    const dataExcel = XLSX.utils.sheet_to_json(workbook.Sheets[sheet]);
    const result = dataExcel
fork icon0
star icon0
watch icon1

430
431
432
433
434
435
436
437
438
439
 * @param: resultType json,csv,form
 */
Libs.readExcel = function (filePath, resultType) {
	try {
		var xlsx = require("xlsx");
		var workbook = xlsx.readFile(filePath);
		var output = "";
		switch (resultType) {
			case "csv":
				output = to_csv(xlsx, workbook);
fork icon0
star icon0
watch icon1

+ 2 other calls in file

4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435


const uploadXLSXA = async (req, res, next) => {
  try {
    console.log(req.file.path)
    let path = req.file.path;
    var workbook = xlsx.readFile(path);
    var sheet_name_list = workbook.SheetNames;
    let jsonData = xlsx.utils.sheet_to_json(
      workbook.Sheets[sheet_name_list[0]]
    );
fork icon0
star icon0
watch icon1

+ 2 other calls in file

393
394
395
396
397
398
399
400
401
402

    return await xml;
}
else {
    if (entry.importedFile.ext === ".xlsx") {
        const wb = xlsx.readFile(`./public${entry.importedFile.url}`)
        const ws = wb.Sheets['Φύλλο1']
        const data = xlsx.utils.sheet_to_json(ws)
        return data;
    }
fork icon0
star icon0
watch icon1