-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Create a new Excel file, insert an image into one cell of one sheet, create another sheet, insert another image into one cell of another sheet, as the result, causes a event that pasted two images on both cells of both sheets.
It doesn't occur when you finish inserted an image into one cell of a sheet, or inserted images into two or more cells of the same sheet.
func main () {
xlsx: = excelize.NewFile ()
// Create a worksheet
index: = xlsx.NewSheet ("Sheet2")
// Insert image1.jpg into A2 cell of sheet 1
// Still okay at this point
err: = xlsx.AddPicture ("Sheet1", "A2", "./image1.jpg", "")
if err! = nil {
fmt.Println (err)
return err
}
// Insert image2.jpg into A20 cell of sheet 2
// If this is executed, image1.jpg is pasted A2 cells of Sheet 1 and Sheet 2
// and image2.jpg is pasted A20 cells of Sheet 1 and Sheet 2.
err: = xlsx.AddPicture ("Sheet2", "A20", "./image2.jpg", "")
if err! = nil {
fmt.Println (err)
return err
}
// Save Excel
err: = xlsx.SaveAs ("./Book1.xlsx")
if err! = nil {
fmt.Println (err)
return err
}
return nil
}When I extracted the Excel file and saw it, the value of the name held inside of drawing1.xml was the same (Picture 1), so it is considered to be due to that.
If SaveAS is executed first and then reopened and the image is executed only after Save is executed, the stored information will disappear or it will operate normally.
Of course, there is no problem if you insert image1.jpg into one cell of one sheet, execute SaveAS, reopen it, and insert image2.jpg into another cell of another sheet.
func main () {
xlsx: = excelize.NewFile ()
// Create a worksheet
index: = xlsx.NewSheet ("Sheet2")
// Save Excel
err: = xlsx.SaveAs ("./Book1.xlsx")
if err! = nil {
fmt.Println (err)
return err
}
// Reopen
xlsx, err: = excelize.OpenFile ("./Book1.xlsx")
if err! = nil {
fmt.Println (err)
return err
}
// Insert image1.jpg into A2 cell of sheet 1
err: = xlsx.AddPicture ("Sheet1", "A2", "./image1.jpg", "")
if err! = nil {
fmt.Println (err)
return err
}
// Save Excel
err: = xlsx.Save ()
if err! = nil {
fmt.Println (err)
return err
}
// Insert image2.jpg into A20 cell of sheet 2
err: = xlsx.AddPicture ("Sheet2", "A20", "./image2.jpg", "")
if err! = nil {
fmt.Println (err)
return err
}
// Save Excel
err: = xlsx.Save ()
if err! = nil {
fmt.Println (err)
return err
}
return nil
}When this is executed, Book1.xlsx is generated in which image1.jpg is inserted into only A2 cell of sheet 1 and image2.jpg is inserted into only A20 cell of sheet 2.