qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
17.64k stars 1.69k forks source link

After adding and saving images in Excel by excelize, the GetPictureCells method cannot read any values #1872

Closed lukeyMing closed 3 months ago

lukeyMing commented 3 months ago

In the code, first perform the operation of adding pictures to save to example.xlsx, then read example.xlsx, but what is read through f.GetSheetList is empty。 When I use the computer to open example.xlsx and save, I can retrieve the values by running the code again.

version:

package main

import (
    "fmt"
    "github.com/xuri/excelize/v2"
    _ "image/gif"
    _ "image/jpeg"
    _ "image/png"
)

func main() {
    writeExcel()
    readExcel()
}

func writeExcel() {
    f := excelize.NewFile()
    index, err := f.NewSheet("Sheet")
    if err != nil {
        panic(err)
    }
    f.SetActiveSheet(index)
    defer func() {
        if err := f.Close(); err != nil {
            panic(err)
        }
    }()

    if err := f.AddPicture("Sheet", "A1", "aa.png", &excelize.GraphicOptions{
        AutoFit: true,
    }); err != nil {
        panic(err)
    }

    if err := f.SaveAs("example.xlsx"); err != nil {
        fmt.Println(err)
    }
}

func readExcel() {
    f, err := excelize.OpenFile("./example.xlsx")
    if err != nil {
        panic(err)
    }
    defer func() {
        if err := f.Close(); err != nil {
            panic(err)
        }
    }()

    list := f.GetSheetList()
    imgs, err := f.GetPictureCells(list[0])
    if err != nil {
        panic(err)
    }
    fmt.Println(imgs)
}
xuri commented 3 months ago

Thanks for your issue. You insert the picture in the new worksheet named Sheet, but when you get the picture cells with the GetPictureCells function, the worksheet name is Sheet1, so the result of the function returns is in expectations. Please use the list[1] instead of list[0] in your code. I'll close this, if you have any questions, please let me know, and you can reopen this anytime.