A js excel template used in browser or nodejs environment.
npm i js-excel-template
- Generate Excel based on Excel template
- (for nodejs)Read excel file as template
- (for nodejs)Write to excel file
- (for nodejs)To a
Buffer
for downloading - (for browser-side js)Take
ArrayBuffer
as template, coming from HTTP downloading, browser uploading or Websocket downloading - (for browser-side js)To a
ArrayBuffer
orBlob
for HTTP uploading, browser-side downloading or Websocket uploading
// nodejs:
import JsExcelTemplate from "js-excel-template";
const excelTemplate = await JsExcelTemplate.fromFile("demo/test.xlsx");
// browser(module):
// import * as FileSaver from "file-saver";
// import JsExcelTemplate from "js-excel-template";
// const response = await fetch('./test.xlsx')
// const arrayBuffer = await response.arrayBuffer()
// const excelTemplate = await JsExcelTemplate.fromArrayBuffer(arrayBuffer)
// browser(script tag):
// <script src="file-saver/FileSaver.min.js"></script>
// <script src="exceljs/dist/exceljs.min.js"></script>
// <script src="js-excel-template/js-excel-template.min.js"></script>
// <script>
// const response = await fetch('./test.xlsx')
// const arrayBuffer = await response.arrayBuffer()
// const excelTemplate = await JsExcelTemplate.fromArrayBuffer(arrayBuffer)
excelTemplate.set("name", "John");
excelTemplate.set("age", 123);
excelTemplate.set("now", new Date());
excelTemplate.set("isBoy", true);
excelTemplate.set('isGirl', {
richText: [
{
text: 'false',
font: {
strike: true,
},
},
],
})
const students = [
{ name: "Tommy", age: 12 },
{ name: "Philips", age: 13 },
{ name: "Sara", age: 14 },
];
for (let i = 1; i <= 5; i++) {
if (i <= students.length) {
excelTemplate.set(`name${i}`, students[i - 1].name);
excelTemplate.set(`age${i}`, students[i - 1].age);
} else {
excelTemplate.set(`name${i}`, "");
excelTemplate.set(`age${i}`, "");
}
}
excelTemplate.set("average", students.reduce((p, c) => p + c.age, 0) / students.length);
excelTemplate.set('fields', [{ name: 'Name' }, { name: 'Age' }], { duplicateCellIfArray: true })
excelTemplate.set("students", students);
// nodejs:
await excelTemplate.saveAs("spec/out.xlsx");
// browser(module):
// const blob = await excelTemplate.toBlob()
// FileSaver.saveAs(blob, "test.xlsx");
// browser(script tag):
// const blob = await excelTemplate.toBlob()
// FileSaver.saveAs(blob, "test.xlsx");
// </script>
// v3
const excelTemplate = await JsExcelTemplate.fromFile("demo/test.xlsx");
const excelTemplate = await JsExcelTemplate.fromArrayBuffer(arrayBuffer);
<script src="exceljs/dist/exceljs.min.js"></script>
await excelTemplate.saveAs("spec/out.xlsx");
const blob = await excelTemplate.toBlob()
// v2
const excelTemplate = JsExcelTemplate.fromFile("demo/test.xlsx");
const excelTemplate = JsExcelTemplate.fromArrayBuffer(arrayBuffer);
<script src="xlsx/dist/xlsx.full.min.js"></script>
excelTemplate.saveAs("spec/out.xlsx");
const blob = excelTemplate.toBlob()
// v2
import JsExcelTemplate from "js-excel-template/nodejs/nodejs";
import JsExcelTemplate from "js-excel-template/browser/browser";
// v1
import { JsExcelTemplate } from "js-excel-template/nodejs";
import { JsExcelTemplate } from "js-excel-template/browser";