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

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: A Complete Overview of Tools



TypeScript, as a statically typed superset of JavaScript, provides excellent capabilities for building complex enterprise applications. One common task is working with Microsoft Excel files — reading, creating, and editing spreadsheets. In this article, we will review the best libraries for working with Excel in TypeScript, their features, and provide practical code examples.



Popular Libraries for Working with Excel



Today, there are several mature solutions. Here are the main ones:



  • ExcelJS — the most powerful library with support for styles, formulas, charts, and streaming.
  • xlsx (SheetJS) — a lightweight and fast library, ideal for simple read/write operations.
  • node-excel-export — outdated but still used in legacy projects.
  • Excel4Node — specializes in creating complex reports with formatting.


For most modern TypeScript projects, we recommend ExcelJS and xlsx (SheetJS). Let's take a closer look at them.



Installation and Setup



Both libraries are installed via npm:



npm install exceljsnpm install xlsx


For TypeScript, you need to install types (if not included):



npm install --save-dev @types/node


Note: ExcelJS comes with built-in types, while for xlsx you need to install types separately:



npm install --save-dev @types/xlsx


Example 1: Creating an Excel File with ExcelJS



Let's create a simple table with data and styles:



import ExcelJS from 'exceljs';

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

// Define columns sheet.columns = [ { header: 'ID', key: 'id', width: 10 }, { header: 'Name', key: 'name', width: 25 }, { header: 'Date', key: 'date', width: 15 }, { header: 'Amount', key: 'amount', width: 12 } ];

// Add rows sheet.addRow({ id: 1, name: 'Ivan Petrov', date: '2024-01-15', amount: 1500.50 }); sheet.addRow({ id: 2, name: 'Maria Sidorova', date: '2024-02-20', amount: 2300.75 }); sheet.addRow({ id: 3, name: 'Alexey Ivanov', date: '2024-03-10', amount: 890.25 });

// Style the header const headerRow = sheet.getRow(1); headerRow.font = { bold: true, size: 12, color: { argb: 'FFFFFFFF' } }; headerRow.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF4472C4' } }; headerRow.alignment = { horizontal: 'center' };

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

createWorkbook();


This code creates an output.xlsx file with three rows of data and a styled header.



Example 2: Reading an Excel File with xlsx (SheetJS)



Let's read an existing file and output the data to the console:



import * as XLSX from 'xlsx';

function readExcel(filePath: string) { const workbook = XLSX.readFile(filePath); const sheetName = workbook.SheetNames[0]; // Take the first sheet const worksheet = workbook.Sheets[sheetName];

// Convert to JSON const data: any[] = XLSX.utils.sheet_to_json(worksheet);

console.log('Data from file:'); data.forEach((row, index) => { console.log(`Row ${index + 1}:`, row); });

return data;}

// Usageconst data = readExcel('input.xlsx');


The xlsx library automatically converts the table into an array of objects, where the keys are the column headers.

Recommendations