1. Insert picture | picture | excel Guide

Posted by jheitz on Wed, 02 Feb 2022 13:02:53 +0100

Insert picture

func (f *File) AddPicture(sheet, cell, picture, format string) error

Insert a picture on the corresponding cell according to the given worksheet name, cell coordinates, picture address and picture format (such as offset, zoom, print settings, etc.).

For example:

package main

import (
    "fmt"
    _ "image/gif"
    _ "image/jpeg"
    _ "image/png"

    "github.com/xuri/excelize/v2"
)

func main() {
    f := excelize.NewFile()
    // Insert picture
    if err := f.AddPicture("Sheet1", "A2", "image.jpg", ""); err != nil {
        fmt.Println(err)
    }
    // Insert picture with zoom and hyperlink
    if err := f.AddPicture("Sheet1", "D2", "image.png", `{
        "x_scale": 0.5,
        "y_scale": 0.5,
        "hyperlink": "#Sheet2!D8",
        "hyperlink_type": "Location"
    }`); err != nil {
        fmt.Println(err)
    }
    // Insert a picture and set its external hyperlink, print, and location properties
    if err := f.AddPicture("Sheet1", "H2", "image.gif", `{
        "x_offset": 15,
        "y_offset": 10,
        "hyperlink": "https://github.com/xuri/excelize",
        "hyperlink_type": "External",
        "print_obj": true,
        "lock_aspect_ratio": false,
        "locked": false,
        "positioning": "oneCell"
    }`); err != nil {
        fmt.Println(err)
    }
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

The optional parameter autofit specifies whether to automatically fit the picture size to the cell, and its default value is false.

The optional parameter hyperlink is used to specify the hyperlink of the picture.

Optional parameter hyperlink_type specifies the type of picture hyperlink. It supports two types: External link and internal link Location. When using Location to connect to the cell Location, the coordinates need to start with #.

The optional parameter positioning defines the position attribute of the picture in the Excel spreadsheet. It supports two types: oneCell (fixed size and position change with the cell) and absolute (fixed size and position). When this parameter is not set, the default attribute is that the size and position change with the cell.

Optional parameter print_obj specifies whether to print pictures when printing a worksheet. The default value is true.

Optional parameter lock_aspect_ratio specifies whether to lock the aspect ratio of the picture. Its default value is false.

The optional parameter locked specifies whether to lock the picture. Locked objects are not valid unless the worksheet is protected.

Optional parameter x_offset specifies the horizontal offset between the picture and the inserted cell. Its default value is 0.

Optional parameter x_scale specifies the horizontal scale of the picture. The default value is 1.0, which means 100%.

Optional parameter y_offset specifies the vertical offset between the picture and the inserted cell. Its default value is 0.

Optional parameter y_scale specifies the vertical scale of the picture. Its default value is 1.0, which means 100%.

func (f *File) AddPictureFromBytes(sheet, cell, format, name, extension string, file []byte) error

Insert a picture into the corresponding cell according to the given worksheet name, cell coordinates, picture address and picture format (such as offset, zoom and print settings), picture description, picture extension and picture content of [] byte type.

For example:

package main

import (
    _ "image/jpeg"
    "io/ioutil"

    "github.com/xuri/excelize/v2"
)

func main() {
    f := excelize.NewFile()

    file, err := ioutil.ReadFile("image.jpg")
    if err != nil {
        fmt.Println(err)
    }
    if err := f.AddPictureFromBytes("Sheet1", "A2", "", "Excel Logo", ".jpg", file); err != nil {
        fmt.Println(err)
    }
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

This article was first published in LearnKu.com On the website.