添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
开朗的花生  ·  Microchip Classic·  11 月前    · 
开朗的茄子  ·  Diffie-Hellman算法_diffi ...·  1 年前    · 
逃课的手套  ·  mac安装php7的intl,Error ...·  1 年前    · 

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I have installed latest version of exceljs, however I'm still getting error workbook.addImage is not a function when i'm trying to add any image to the workbook.
PS: I have 0.4.10 version

Problem is when we create a workbook using writer stream then it doesn't find function addImage() . However you can also use the following to add an image.

let workbook = Excel.stream.xlsx.WorkbookWriter()
workbook.media.push({
    filename: './Picture1.png',
    extension: 'png',
worksheet.addImage(0, 'B1:E6')
// where 0 is the index of the image you added in workbook, basically its an index of the image you pushed to media array

Otherwise simply use below to create a worksheet and it works,
let workbook = new Excel.Workbook()

@guyonroche @ishantiw i'm using the above code but i'm getting "worksheet.addImage is not a function".here is the code

var workbook = Excel.stream.xlsx.WorkbookWriter();
workbook.media.push({
filename: './Picture1.png',
extension: 'png',
var worksheet = workbook.addWorksheet();
worksheet.addImage(0, 'B1:E6')

@guyonroche If you will create a worksheet from stream.xlsx.WorkbookWriter() then you will have to use
workbook.media.push({ filename: './Picture1.png', extension: 'png', })
You can get these images using index workbook.media[0]. This is just a hack and its better not to use this one.

Recommended usage,
let workbook = new Excel.Workbook() to create a workbook and then workbook.addImage will work. Cheers!

@ishantiw @guyonroche
I follow the docs but add images are not working

let workbook = new Excel.Workbook()
const imageId1 = workbook.addImage({
filename: './Picture1.png',
extension: 'png',
worksheet.addBackgroundImage(imageId1);

error: imageId1 returns 0

Please help

@qiandongyq
workbook.addImage({}) returns index of the image you pushed into the image array of workbook. Since it is your first image so you got index as 0 (imageId1). Now you have to use, worksheet.addImage(0, 'A1:B6') to add it over specified cells. But if you are using addBackgroundImage function you just need to pass the index. In my case it is working. Can you please make sure your image is getting added without any error. Try to check using worksheet.addBackgroundImage(0)

@ishantiw I think the issue is that i'm using buffer to download the excel file. Everything is working before add images. It is browser, can't see any errors

 this.workbook.xlsx.writeBuffer().then(data => {
      const blob = new Blob([data], {
        type:
          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
      FileSaver.saveAs(blob, "pshsa.xlsx");
          

@ishantiw the workaround you posted earlier doesn't work:

let workbook = Excel.stream.xlsx.WorkbookWriter()
workbook.media.push({
    filename: './Picture1.png',
    extension: 'png',
worksheet.addImage(0, 'B1:E6') <--- addImage is not a function

This will throw worksheet.addImage is not a function (Note: sheet, not workbook).

Using the recommended let workbook = new Excel.Workbook() is not option, because we need to use a stream. Do you know if it is possible to add images with the Excel.stream.xlsx.WorkbookWriter?
Thanks!

Anyone know if this is getting fixed anytime soon?
I have had a look at the code, but it's a bit to complex for me at this stage, for me to try and fix.
Like @kkor said my problem is worksheet.addImage is not a function when the file is getting streamed.
Any solution or fixes will be very appreciated. I will still have a look at the code and try and fix it.
Thanks in advance

This issue is a major release blocker. Surprised that, its not being addressed..
Right now, its impossible to use addImage using the WorkbookWriter.
Any workaround mentioned here won't work.

Guys, do you know any other library that supports all functionality in WorkbookWriter and easy to switch from exceljs(semantically similar)?

Got this working by converting my image to base64 via https://www.base64-image.de/
and then adding it in. Please see code below. Work Great!

var myBase64Image = "data:image/jpeg;base64,/9.......................";
var logo = workbook.addImage({
base64: myBase64Image,
extension: 'jpeg',
worksheet.addImage(logo, 'N2:O2');