0

We have an excel report template that has some placeholder text(keys) that will be replaced by certain values among creating the report.

we have searched everywhere but couldn't find any solution with javascript ( i know we can do it with c#/.Net but our project is with AngularCLI and the manipulation is done with javascript).

we have seen/read about exceljs but still it doesn't meet this requirement.

is there any way or any JS library that is able to do that specific task or we are out of luck?

Mahamad Husen
  • 229
  • 1
  • 13
  • Did you find or look at JHXLSX ("a jQuery dependent JavaScript library to download or create Excel XLSX spreadsheets using JavaScript (JSON)"). You might be better off having the template access another workbook that contains only the data, and create the 'data' workbook with Javascript. – Richard Dec 13 '20 at 09:28
  • @Richard No this cannot be useful for us, in our case we wont be creating the excel templates, in our scenario ,the users are able to upload their own custom excel template(for report purposes) with the variable texts be the placeholders (person's name,phone,address..etc) – Mahamad Husen Dec 13 '20 at 11:04

1 Answers1

0

The solution to this that we implemented was to use the docxtemplater , but we did it with word(docx) and not excel.

docxtemplater is a library to generate docx/pptx documents from a docx/pptx template. It can replace {placeholders} with data and also supports loops and conditions. The templates can be edited by non-programmers, for example, your clients

Its available as npm : docxtemplater

sample code taken from docs :

const PizZip = require("pizzip");
const Docxtemplater = require("docxtemplater");

const fs = require("fs");
const path = require("path");

// Load the docx file as binary content
const content = fs.readFileSync(
    path.resolve(__dirname, "input.docx"),
    "binary"
);

const zip = new PizZip(content);

const doc = new Docxtemplater(zip, {
    paragraphLoop: true,
    linebreaks: true,
});

// Render the document (Replace {first_name} by John, {last_name} by Doe, ...)
doc.render({
    first_name: "John",
    last_name: "Doe",
    phone: "0652455478",
    description: "New Website",
});

const buf = doc.getZip().generate({
    type: "nodebuffer",
    // compression: DEFLATE adds a compression step.
    // For a 50MB output document, expect 500ms additional CPU time
    compression: "DEFLATE",
});

// buf is a nodejs Buffer, you can either write it to a
// file or res.send it with express for example.
fs.writeFileSync(path.resolve(__dirname, "output.docx"), buf);
Mahamad Husen
  • 229
  • 1
  • 13