Skip to content

Instantly share code, notes, and snippets.

@ptesser
Last active February 14, 2020 15:58
Show Gist options
  • Save ptesser/1f17f98ef1b0d539bf4387a3863ee31e to your computer and use it in GitHub Desktop.
Save ptesser/1f17f98ef1b0d539bf4387a3863ee31e to your computer and use it in GitHub Desktop.

Excel in JS

SheetJS

A lot of features as add image or style cell are available only on Pro account.

There is a repo that fork the original one and that allow format options but has no well support: npm i fixed-xlsx-style.

ExcelJS

Link: https://github.com/exceljs/exceljs

Version 3.x has not well Angular support. There are some issues related to the use of this library on it, but the first link explain how to use version 1.12 with success.

Basic usage:

import { Workbook } from 'exceljs';

// ...code

const woorkbook = new Workbook();
const worksheet = woorkbook.addWorksheet('SheetName');

worksheet.columns = <someArrayOfObjects>.cols.map((col) => ({ header: col.label, key: col.label, width: 10 }));

// add image inside a sheet

const image = workbook.addImage({
  base64: 'some base64 image',
  extension: 'png',
});
worksheet.addImage(image, {
  editAs: 'absolute',
  tl: { col: 0, row: 20 },
  ext: { width: 400, height: 150 },
});

// download the workbook

const buffer = await workbook.xlsx.writeBuffer();
const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });

saveAs(blob, 'test.xlsx');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment