@devath/angular-excel-tools
v0.1.6
Published
XLSX (SheetJS) utilities for Angular + helpers for Angular Material Table
Maintainers
Readme
@devath/angular-excel-tools
XLSX (SheetJS) utilities for Angular apps. Provides a simple ExcelService to import .xlsx files into arrays-of-arrays (AOA) and export data back to Excel. Includes helpers for Angular Material tables. Designed to work with Angular 18.
Why this package
- Minimal API: read, sanitize, align headers, export.
- AOA-first: easy to map to your own models.
- MatTable helpers: import/export with table-friendly shapes.
Live demo
- StackBlitz template: https://stackblitz.com/fork/angular
- After it opens, run:
npm i @devath/angular-excel-tools xlsx - Paste the Quick start example below into
app.component.ts
Install
npm i @devath/angular-excel-tools xlsxPeer dependencies: @angular/common@^18, @angular/core@^18, xlsx@^0.18.5.
Quick start
Import and use the ExcelService in any component or service.
// example.component.ts
import { Component } from '@angular/core';
import { ExcelService, ImportResult } from '@devath/angular-excel-tools';
@Component({
selector: 'app-example',
template: `
<input type="file" accept=".xlsx,.xls" (change)="onFile($event)" />
<button (click)="exportSample()">Export sample</button>
`
})
export class ExampleComponent {
constructor(private excel: ExcelService) {}
async onFile(ev: Event) {
const input = ev.target as HTMLInputElement;
const file = input.files?.[0];
if (!file) return;
const result: ImportResult = await this.excel.readFile(file);
console.log(result.sheetNames); // ["Sheet1", ...]
console.log(result.sheets["Sheet1"]); // AOA data
}
exportSample() {
const aoa = [
['Name', 'Age'],
['Alice', 30],
['Bob', 28]
];
this.excel.exportToXlsx({ data: aoa, filename: 'people.xlsx' });
}
}You can also export multiple sheets by passing an object of sheet names to AOA data:
this.excel.exportToXlsx({
data: {
Summary: [['Metric', 'Value'], ['Count', 2]],
People: [['Name', 'Age'], ['Alice', 30], ['Bob', 28]]
},
filename: 'report.xlsx'
});Angular Material helpers
Helpers convert AOA ↔ MatTable-friendly shapes.
import { aoaToMatTable, matTableToAoa } from '@devath/angular-excel-tools';
const { displayedColumns, rows } = aoaToMatTable(aoa);
// bind: <table mat-table [dataSource]="rows"> ...
const aoaOut = matTableToAoa(displayedColumns, rows);Use cases
- Import user-provided spreadsheets into your app.
- Export table data to Excel for reporting.
- Normalize messy sheets with header alignment.
API
- ExcelService.readFile(file): Promise
- Reads an Excel file and returns
{ sheetNames: string[], sheets: Record<string, AOA> }.
- Reads an Excel file and returns
- ExcelService.exportToXlsx({ data, filename })
data:AOAfor a single sheet orRecord<string, AOA>for multiple sheets.filename: optional, defaults toexport.xlsx.
- ExcelService.sanitizeAOA(aoa)
- Normalizes rows to the same length, trims strings, replaces
undefinedwithnull.
- Normalizes rows to the same length, trims strings, replaces
- ExcelService.alignHeaders(aoa, headers)
- Reorders columns of imported data to match your desired header order.
- aoaToMatTable(aoa)
- Converts AOA (first row is headers) to
{ displayedColumns, rows }.
- Converts AOA (first row is headers) to
- matTableToAoa(displayedColumns, rows)
- Converts MatTable inputs to AOA.
Types:
- AOA:
(string | number | boolean | null | undefined)[][] - ImportResult:
{ sheetNames: string[]; sheets: Record<string, AOA>; }
Tips
- Use
sanitizeAOAright after import to ensure consistent row lengths. - Use
alignHeadersto map user-provided sheets to your required schema.
Angular compatibility
- Built with Angular 18 and Ivy partial compilation. Should work in Angular 16+ projects using Ivy.
License
MIT © Sandesh Ath
