javascript - How to read xlsx file with ExcelJS?

Javascript - How to read xlsx file with ExcelJS?

To read an XLSX file using the ExcelJS library in JavaScript, you can follow these steps:

  1. Install ExcelJS via npm if you haven't already:
npm install exceljs 
  1. Create a JavaScript file (e.g., read_xlsx.js) and require the exceljs module:
const ExcelJS = require('exceljs'); 
  1. Load your XLSX file using ExcelJS:
const workbook = new ExcelJS.Workbook(); workbook.xlsx.readFile('your_file.xlsx') .then(() => { // Code to handle the loaded workbook // e.g., iterate through worksheets and cells }) .catch(err => { console.log('Error reading file:', err); }); 
  1. Once the file is loaded, you can perform operations like accessing worksheets, cells, rows, etc. For example, to access a specific worksheet and read its data:
const worksheet = workbook.getWorksheet('Sheet1'); // Change 'Sheet1' to your desired worksheet name worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) { // Access each cell in the row row.eachCell({ includeEmpty: true }, function(cell, colNumber) { console.log(`Row ${rowNumber}, Column ${colNumber}, Value = ${cell.value}`); }); }); 
  1. Run your JavaScript file with Node.js:
node read_xlsx.js 

This script will read the specified XLSX file and output the data from each cell in each row of the specified worksheet.

Make sure to replace 'your_file.xlsx' with the path to your actual XLSX file and 'Sheet1' with the name of the worksheet you want to read.

Examples

  1. Read xlsx file using ExcelJS in Node.js

    Description: Learn how to read an xlsx file using ExcelJS library in a Node.js environment.

    // Example code: const ExcelJS = require('exceljs'); async function readXlsxFile(filePath) { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); const worksheet = workbook.getWorksheet(1); const data = []; worksheet.eachRow((row) => { data.push(row.values); }); return data; } 
  2. Read specific sheet from xlsx file with ExcelJS

    Description: Understand how to read a specific sheet from an xlsx file using ExcelJS library in JavaScript.

    // Example code: const ExcelJS = require('exceljs'); async function readSpecificSheet(filePath, sheetName) { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); const worksheet = workbook.getWorksheet(sheetName); const data = []; worksheet.eachRow((row) => { data.push(row.values); }); return data; } 
  3. Read xlsx file with header row using ExcelJS

    Description: Learn how to read an xlsx file with a header row using ExcelJS library in JavaScript.

    // Example code: const ExcelJS = require('exceljs'); async function readXlsxWithHeader(filePath) { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); const worksheet = workbook.getWorksheet(1); const headers = worksheet.getRow(1).values; const data = []; worksheet.eachRow((row, rowNumber) => { if (rowNumber !== 1) { const rowData = {}; row.eachCell((cell, colNumber) => { rowData[headers[colNumber - 1]] = cell.value; }); data.push(rowData); } }); return data; } 
  4. Read xlsx file with specific columns using ExcelJS

    Description: Learn how to read an xlsx file with specific columns using ExcelJS library in JavaScript.

    // Example code: const ExcelJS = require('exceljs'); async function readXlsxWithColumns(filePath, columns) { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); const worksheet = workbook.getWorksheet(1); const data = []; worksheet.eachRow((row) => { const rowData = {}; row.eachCell((cell, colNumber) => { if (columns.includes(colNumber)) { rowData[colNumber] = cell.value; } }); data.push(rowData); }); return data; } 
  5. Read xlsx file with formatting using ExcelJS

    Description: Understand how to read an xlsx file with formatting retained using ExcelJS library in JavaScript.

    // Example code: const ExcelJS = require('exceljs'); async function readXlsxWithFormatting(filePath) { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); const worksheet = workbook.getWorksheet(1); const data = []; worksheet.eachRow((row) => { const rowData = {}; row.eachCell((cell) => { rowData[cell.address] = { value: cell.value, font: cell.font, alignment: cell.alignment, border: cell.border, fill: cell.fill, }; }); data.push(rowData); }); return data; } 
  6. Read xlsx file with specific rows using ExcelJS

    Description: Learn how to read an xlsx file with specific rows using ExcelJS library in JavaScript.

    // Example code: const ExcelJS = require('exceljs'); async function readXlsxWithRows(filePath, startRow, endRow) { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); const worksheet = workbook.getWorksheet(1); const data = []; worksheet.eachRow((row, rowNumber) => { if (rowNumber >= startRow && rowNumber <= endRow) { data.push(row.values); } }); return data; } 
  7. Read xlsx file asynchronously with ExcelJS

    Description: Read an xlsx file asynchronously using ExcelJS library in JavaScript for better performance.

    // Example code: const ExcelJS = require('exceljs'); async function readXlsxFileAsync(filePath) { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); const worksheet = workbook.getWorksheet(1); const data = []; worksheet.eachRow((row) => { data.push(row.values); }); return data; } 
  8. Read xlsx file and convert to JSON using ExcelJS

    Description: Read an xlsx file using ExcelJS library in JavaScript and convert its contents to JSON format.

    // Example code: const ExcelJS = require('exceljs'); async function convertXlsxToJson(filePath) { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); const worksheet = workbook.getWorksheet(1); const data = []; worksheet.eachRow((row) => { const rowData = {}; row.eachCell((cell, colNumber) => { rowData[colNumber] = cell.value; }); data.push(rowData); }); return JSON.stringify(data); } 
  9. Read xlsx file with date formatting using ExcelJS

    Description: Read an xlsx file with date formatting retained using ExcelJS library in JavaScript.

    // Example code: const ExcelJS = require('exceljs'); async function readXlsxWithDateFormatting(filePath) { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); const worksheet = workbook.getWorksheet(1); const data = []; worksheet.eachRow((row) => { const rowData = {}; row.eachCell((cell) => { rowData[cell.address] = { value: cell.value, numFmt: cell.numFmt, }; }); data.push(rowData); }); return data; } 
  10. Read xlsx file with cell formulas using ExcelJS

    Description: Learn how to read an xlsx file with cell formulas retained using ExcelJS library in JavaScript.

    // Example code: const ExcelJS = require('exceljs'); async function readXlsxWithFormulas(filePath) { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); const worksheet = workbook.getWorksheet(1); const data = []; worksheet.eachRow((row) => { const rowData = {}; row.eachCell((cell) => { rowData[cell.address] = { value: cell.value, formula: cell.formula, }; }); data.push(rowData); }); return data; } 

More Tags

confluent-platform formview patch nodes mv spam-prevention multi-select jenkins-email-ext sign scene

More Programming Questions

More Weather Calculators

More Chemical thermodynamics Calculators

More Stoichiometry Calculators

More Internet Calculators