ExcelJS in coldfusion Project

766 views Asked by At

Requirement is to export an excel file(*.xlsx) from ui-grid with some formatting. I am able to export the excel using angularjs, ui-grid and js-xlsx, however I am unable to format the excel cells (ex. bold, text color) with js-xlsx.

I found Exceljs and it looks promising for my requirement. The difficulty is that it is available as npm package and the server I am working on do not have node/npm installed. Alternatively I am unable to find a download/cdn for the same. So I have installed the package on local system and copied the folder exceljs from node_modules and tried the following:

<script src="dist/exceljs.js"></script>

<script>
        var workbook = new Excel.Workbook();
        console.log(workbook);
</script>

It's throwing an error "Uncaught ReferenceError: Excel is not defined".

I am guessing its because it should be included as node modules for the "Excel" object to be available.

var Excel = require('exceljs');

Is there any way I can include and create it without require('').

If it can't be done, I Would Appreciate any other solution/recommendation.

2

There are 2 answers

1
Adam Jędrzejowski On

After quick searching in code, I found this. This library is using file reading/writing so u are unable to use it i browser. In the other hand Excel file is zip archive of xml's, so u will never achieve what u want without server-side help or 3rd part service.

0
soumyadityac On

What you want is a bundled build of exceljs, that globally exposes the standalone ExcelJS object (via window). That is precisely what you have too, except the fact that you're referring to it by an invalid name.

var workbook = new ExcelJS.Workbook();

Additionally, since you're not really using a module bundler in your javascript project, instead of getting the exceljs bundled distributable via an npm install and extracting it from node_modules, a better approach would be to download from a list of their releases and build the project yourself -

$ cd exceljs
$ npm install
$ npm run build

following which you can get both exceljs.js and it's minified counterpart exceljs.min.js available under the dist directiory.

EDIT: Also, I would disagree with Adam here. ExcelJS can indeed be used for generating excelsheets in the browser side, and is perfect for the use case you mentioned. One of the approaches we used was to construct the ExcelJS.Workbook dynamically using the data we have, write the workbook to a buffer, convert it to a blob using -

new Blob([buffer], { type: buffer.type })

and then download it using ObjectURLs.