Working with Excel in TypeScript: Libraries, Examples, and Tools

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Excel in TypeScript: Libraries, Examples, and Tools



TypeScript is a powerful language for developing web applications, and there is often a need to programmatically work with Excel files: reading reports, generating spreadsheets, exporting data. In this article, we will look at three main libraries for working with Excel in TypeScript, their capabilities, and provide practical examples.



1. Main Libraries for Working with Excel in TypeScript



There are several popular solutions, each with its own strengths:



  • ExcelJS — the most popular library for reading, writing, and styling Excel files. Supports .xlsx, .xlsm, .csv, as well as working with formulas, conditional formatting, and images.
  • xlsx (SheetJS) — a lightweight and fast library for basic operations: reading and writing tabular data. Ideal for simple tasks.
  • excel4node — a library for creating Excel files with an emphasis on performance and style support.


2. Installation and Setup



Use npm to install the chosen library. Let's look at installing ExcelJS and xlsx:



npm install exceljsnpm install xlsx


For TypeScript, additionally install the types (for xlsx they are built-in):



npm install @types/exceljs --save-devnpm install @types/node --save-dev


3. Working with Excel via ExcelJS



3.1 Creating and Writing a File



Let's create a new Excel file with headers and data:



import * as ExcelJS from 'exceljs';

async function createExcel() { const workbook = new ExcelJS.Workbook(); const worksheet = workbook.addWorksheet('Data');

// Define columns worksheet.columns = [ { header: 'Name', key: 'name', width: 25 }, { header: 'Age', key: 'age', width: 10 }, { header: 'City', key: 'city', width: 20 } ];

// Add rows worksheet.addRow({ name: 'Anna', age: 28, city: 'Moscow' }); worksheet.addRow({ name: 'Ivan', age: 35, city: 'Saint Petersburg' }); worksheet.addRow({ name: 'Olga', age: 22, city: 'Kazan' });

// Styling headers const headerRow = worksheet.getRow(1); headerRow.font = { bold: true, color: { argb: 'FFFFFFFF' } }; headerRow.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF4472C4' } };

// Save the file await workbook.xlsx.writeFile('example.xlsx'); console.log('File created');}

createExcel();


3.2 Reading an Existing File



Let's read data from the previously created file:



async function readExcel() {  const workbook = new ExcelJS.Workbook();  await workbook.xlsx.readFile('example.xlsx');

const worksheet = workbook.getWorksheet('Data'); if (!worksheet) return;

worksheet.eachRow((row, rowNumber) => { const rowData = row.values as any[]; console.log(`Row ${rowNumber}: ${rowData.join(', ')}`); });}

readExcel();


4. Working with Excel via xlsx (SheetJS)



4.1 Reading Data



The xlsx library allows you to quickly read data from a file:



import * as XLSX from 'xlsx';

function readXlsxFile(filePath: string) { const workbook = XLSX.readFile(filePath); const sheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[sheetName]; const data = XLSX.utils.sheet_to_json(worksheet); console.log(data);}

readXlsxFile('example.xlsx');


4.2 Creating and Writing



Let's create a simple Excel file from an array of objects:



function createXlsx() {  const data = [    { Name: 'Anna', Age: 28, City: 'Moscow' },    { Name: 'Ivan', Age: 35, City: 'SPb' }  ];

const worksheet = XLSX.utils.json_to_sheet(data); const workbook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(workbook, wor

Recommendations