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: Library Overview and Practical Examples



TypeScript is a strictly typed extension of JavaScript that is actively used in web development, especially when creating complex enterprise applications. One common task is working with Excel spreadsheets: reading data, generating reports, exporting, and importing. In this article, we will review the main libraries for working with Excel in TypeScript and provide practical code examples.



Popular Libraries for Working with Excel in TypeScript



Today, there are several stable solutions. Let's look at the three most popular ones:



  • ExcelJS — a powerful library for reading, creating, and editing XLSX files. It supports styles, formulas, charts, sheet protection, and much more. It works in the browser and Node.js.
  • xlsx (SheetJS) — a lightweight library for parsing and generating spreadsheets. It is ideal for quick data processing without complex formatting.
  • node-xlsx — a simple wrapper around xlsx, focused on Node.js. It is convenient for quickly exporting data arrays.


In this guide, we will focus on ExcelJS, as it provides the most control over the document structure and styles.



Installation and Setup



First, install the library via npm:



npm install exceljsnpm install @types/node --save-dev  // for Node.js types


Also, make sure TypeScript is configured in your project (the tsconfig.json file).



Creating a Simple Excel File



An example of creating a file with headers and data:



import * as ExcelJS from 'exceljs';

async function createWorkbook() { const workbook = new ExcelJS.Workbook(); const sheet = workbook.addWorksheet('Employees');

// Headers sheet.columns = [ { header: 'ID', key: 'id', width: 10 }, { header: 'Name', key: 'name', width: 25 }, { header: 'Position', key: 'position', width: 20 }, { header: 'Salary', key: 'salary', width: 15 }, ];

// Data const employees = [ { id: 1, name: 'Ivan Petrov', position: 'Developer', salary: 150000 }, { id: 2, name: 'Maria Smirnova', position: 'Designer', salary: 120000 }, { id: 3, name: 'Alexey Kuznetsov', position: 'Manager', salary: 130000 }, ];

employees.forEach(emp => sheet.addRow(emp));

// Save await workbook.xlsx.writeFile('employees.xlsx'); console.log('File created!');}

createWorkbook();


Reading Data from Excel



Reading an existing file and converting data into TypeScript objects:



import * as ExcelJS from 'exceljs';

interface Employee { id: number; name: string; position: string; salary: number;}

async function readWorkbook() { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile('employees.xlsx'); const sheet = workbook.getWorksheet('Employees'); const employees: Employee[] = [];

sheet.eachRow((row, rowNumber) => { if (rowNumber === 1) return; // skip headers employees.push({ id: row.getCell(1).value as number, name: row.getCell(2).value as string, position: row.getCell(3).value as string, salary: row.getCell(4).value as number, }); });

console.log('Employees read:', employees.length); console.log(employees);}

readWorkbook();


Formatting and Styles



ExcelJS allows you to customize the appearance of cells: fonts, fill, borders, alignment. An example of adding styles to headers:



import * as ExcelJS from 'exceljs';

async function styledWorkbook() { const workbook = new ExcelJS.Workbook(); const sheet = workbook.addWorksheet('Report');

// Define style for headers const headerStyle: Partial&

Recommendations