exceljs: adding image not working.

Hi, I am using exceljs in a html environment. I wanted to use addImage. But as soon as i do that i get an exception.

i used the following code:

// add image to workbook by filename
			var imageId1 = workbook.addImage({
			  filename: 'img/logo.jpeg',
			  extension: 'jpeg',
			});

i am using the following require.js definitions ` //Set the path to jQuery to local path requirejs.config({ paths: { jquery: ‘js/jquery’, Excel: “js/exceljs_2017_07_16”, //Excel: “js/exceljs”, fs: “js/filesaver.min”, // enables download of files within javascript - is used from exceljs jszip: “js/jszip.min”, // jszip is used to generate zip files - is used from exceljs lodash: “js/lodash”, // jszip is used to generate zip files - is used from exceljs es6shim: “js/es6-shim” // jszip is used to generate zip files - is used from exceljs } });

require([‘Excel’, ‘fs’, ‘jszip’, ‘lodash’, ‘es6shim’, ‘jquery’ ], function (Excel, fileSaver) { … } i get the message that fs.readFile is not a function TypeError: fs.readFile is not a function at http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:10749:8 at lib$es6$promise$$internal$$initializePromise (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:14955:9) at Promish.lib$es6$promise$promise$$Promise [as constructor] (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:15246:9) at new Promish (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:35957:108) at fsReadFileAsync (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:10748:10) at http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:11086:18 at Array.map (native) at 85.module.exports.addMedia (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:11082:47) at http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:11311:21 at lib$es6$promise$$internal$$tryCatch (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:14908:16) `

The following code inside of exceljs (line 10749) is used (with a reference to fs. function fsReadFileAsync(filename, options) { return new PromishLib.Promish(function (resolve, reject) { fs.readFile(filename, options, function (error, data) { if (error) { reject(error); } else { resolve(data); } }); }); }

What is the way to attach fs = What library do i have to include ? i am sure i can’t include node.js 😃 Thanks and best regards Manfred

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (1 by maintainers)

Commits related to this issue

Most upvoted comments

You should use “path” library from NodeJS for adding image to workbook:

import * as path from ‘path’;

const IMAGE_PATH = path.resolve(‘public/image.jpg’); // path of our image … const logo = workbook.addImage({ filename: IMAGE_PATH, extension: ‘jpg’, }); … workSheet.addImage(logo, ‘A1:B5’); // set image in range of columns

This working for me

@Mafi78 : There are two interfaces of this library, stream mode and doc mode. Doc mode: var workbook = new Excel.Workbook(); Stream mode: var workBook = new Excel.stream.xlsx.WorkbookWriter({useSharedStrings: true, useStyles: true, stream: res}); Stream mode is used when you have massive data, always recommended by author while doc mode blows up when you have more than 33K rows *100 cols. Your fixes is for doc mode.

In stream mode, addImage function is missing from both workbook-writer and worksheet-writer. Infact, the author has not implemented addImage functionality for stream mode.

The simplest replicator is:

const Excel = require('exceljs');
var workBook = new Excel.stream.xlsx.WorkbookWriter({useSharedStrings: true, useStyles: true, stream: res});// res is the response stream
workBook.addWorksheet('test');
var currentWorksheet= workBook.getWorksheet('test');
var imageId = workBook.addImage({
                            base64: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAH4AAAAsCAMAAACUu/xGAAAAq1BMVEUAAABlZVJlZVKsrJthYU+zs6Grq5ylpZazs6FlZVJfX01lZVJlZVKsrJurq5urq5xlZVKtrZ1lZVJlZVKvr52zs6GysqCoqJeqqpmzs6Grq5xlZVJgYE6zs6Gnp5mrq5yiopRjY1CRkX2rq5yzs6FlZVKRkX2goJKKineRkX2Pj3yrq5yIiHWRkX2RkX2RkX1lZVKRkX2rq5yzs6GoqJdfX02goJKHh3SHh3VrpzVsAAAAMHRSTlMAQIDHx3+Ax0Ag7qBgIA9AEFCPMLOgMO7bYKBQ24+zYNuzkY9wcAXu0oiocPFBMHYlVbK0AAAD3UlEQVRYw6SW7Y6qMBCGB0IkLfKdnB9ocFmjru7HERL03P+VnXY6bdmWjcF9f2inxjydvj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bABrhgdgvhgvS6hIYsPP8MwTVR5SLZA4573xHMpCV7xGZBFmxyProfR64yNCgKh4hygjXIuvpdcbPyEayA2vsEpRHcgl6gtzr8A9ho0RlgQnBPoK4tV45gBfGQZ6KQBDqzRcjdeAqQwHUfYp+SohcQdc1/Ukm4Gw4dV6vqTkM+uQpRv8E2VPF/sPp9xSb2qlGH4AAAAASUVORK5CYII=',
                            extension: 'png'
                        });
                        currentWorksheet.addImage(imageId, {
                            tl: {col: 0, row: 0},
                            br: {col: 9, row: 18},
                            editAs: 'absolute'
                        });
 workBook.commit()
            .then(function () {
                // the stream has been written
                callBack();
            });

Throws exception on workBook.addImage and currentWorksheet.addImage.

Hi James, Thank you very much for this Push request !! Glad i could help. I also did it not get to work … So both of us are happy now. Best regards Manfred

Am 27.11.2017 um 23:30 schrieb James W Mann notifications@github.com:

I was having a terrible time trying to get the filename/path to work and nothing I did actually worked. Probably because I’m not running this on node.js

Base64 images would be super useful and the above fix works very well. I will create the PR for you @Mafi78 https://github.com/mafi78 and submit it.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/guyonroche/exceljs/issues/374#issuecomment-347350586, or mute the thread https://github.com/notifications/unsubscribe-auth/ACLYMYqCwYHSguYKs1Q1sbsThOeXBggTks5s6zfxgaJpZM4OvIfG.