diff options
Diffstat (limited to 'picture.go')
-rw-r--r-- | picture.go | 89 |
1 files changed, 68 insertions, 21 deletions
@@ -1,3 +1,12 @@ +// Copyright 2016 - 2018 The excelize Authors. All rights reserved. Use of +// this source code is governed by a BSD-style license that can be found in +// the LICENSE file. +// +// Package excelize providing a set of functions that allow you to write to +// and read from XLSX files. Support reads and writes XLSX file generated by +// Microsoft Excelâ„¢ 2007 and later. Support save file without losing original +// charts of XLSX. This library needs Go version 1.8 or later. + package excelize import ( @@ -78,27 +87,66 @@ func parseFormatPictureSet(formatSet string) (*formatPicture, error) { // positioning is move and size with cells. func (f *File) AddPicture(sheet, cell, picture, format string) error { var err error - var drawingHyperlinkRID int - var hyperlinkType string // Check picture exists first. if _, err = os.Stat(picture); os.IsNotExist(err) { return err } ext, ok := supportImageTypes[path.Ext(picture)] if !ok { - return errors.New("Unsupported image extension") + return errors.New("unsupported image extension") } - readFile, err := os.Open(picture) - if err!=nil{ - return err + file, _ := ioutil.ReadFile(picture) + _, name := filepath.Split(picture) + return f.AddPictureFromBytes(sheet, cell, format, name, ext, file) +} + +// AddPictureFromBytes provides the method to add picture in a sheet by given +// picture format set (such as offset, scale, aspect ratio setting and print +// settings), file base name, extension name and file bytes. For example: +// +// package main +// +// import ( +// "fmt" +// _ "image/jpeg" +// "io/ioutil" +// +// "github.com/360EntSecGroup-Skylar/excelize" +// ) +// +// func main() { +// xlsx := excelize.NewFile() +// +// file, err := ioutil.ReadFile("./image1.jpg") +// if err != nil { +// fmt.Println(err) +// } +// err = xlsx.AddPictureFromBytes("Sheet1", "A2", "", "Excel Logo", ".jpg", file) +// if err != nil { +// fmt.Println(err) +// } +// err = xlsx.SaveAs("./Book1.xlsx") +// if err != nil { +// fmt.Println(err) +// } +// } +// +func (f *File) AddPictureFromBytes(sheet, cell, format, name, extension string, file []byte) error { + var err error + var drawingHyperlinkRID int + var hyperlinkType string + ext, ok := supportImageTypes[extension] + if !ok { + return errors.New("unsupported image extension") } - defer readFile.Close() - image, _, _ := image.DecodeConfig(readFile) - _, file := filepath.Split(picture) formatSet, err := parseFormatPictureSet(format) if err != nil { return err } + image, _, err := image.DecodeConfig(bytes.NewReader(file)) + if err != nil { + return err + } // Read sheet data. xlsx := f.workSheetReader(sheet) // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder. @@ -114,8 +162,8 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error { } drawingHyperlinkRID = f.addDrawingRelationships(drawingID, SourceRelationshipHyperLink, formatSet.Hyperlink, hyperlinkType) } - f.addDrawingPicture(sheet, drawingXML, cell, file, image.Width, image.Height, drawingRID, drawingHyperlinkRID, formatSet) - f.addMedia(picture, ext) + f.addDrawingPicture(sheet, drawingXML, cell, name, image.Width, image.Height, drawingRID, drawingHyperlinkRID, formatSet) + f.addMedia(file, ext) f.addContentTypePart(drawingID, "drawings") return err } @@ -137,7 +185,7 @@ func (f *File) addSheetRelationships(sheet, relType, target, targetMode string) _, ok = f.XLSX[rels] if ok { ID.Reset() - _ = xml.Unmarshal([]byte(f.readXML(rels)), &sheetRels) + _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &sheetRels) rID = len(sheetRels.Relationships) + 1 ID.WriteString("rId") ID.WriteString(strconv.Itoa(rID)) @@ -163,7 +211,7 @@ func (f *File) deleteSheetRelationships(sheet, rID string) { } var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels" var sheetRels xlsxWorkbookRels - _ = xml.Unmarshal([]byte(f.readXML(rels)), &sheetRels) + _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &sheetRels) for k, v := range sheetRels.Relationships { if v.ID == rID { sheetRels.Relationships = append(sheetRels.Relationships[:k], sheetRels.Relationships[k+1:]...) @@ -280,7 +328,7 @@ func (f *File) addDrawingRelationships(index int, relType, target, targetMode st _, ok := f.XLSX[rels] if ok { ID.Reset() - _ = xml.Unmarshal([]byte(f.readXML(rels)), &drawingRels) + _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &drawingRels) rID = len(drawingRels.Relationships) + 1 ID.WriteString("rId") ID.WriteString(strconv.Itoa(rID)) @@ -309,12 +357,11 @@ func (f *File) countMedia() int { } // addMedia provides a function to add picture into folder xl/media/image by -// given file name and extension name. -func (f *File) addMedia(file, ext string) { +// given file and extension name. +func (f *File) addMedia(file []byte, ext string) { count := f.countMedia() - dat, _ := ioutil.ReadFile(file) media := "xl/media/image" + strconv.Itoa(count+1) + ext - f.XLSX[media] = dat + f.XLSX[media] = file } // setContentTypePartImageExtensions provides a function to set the content @@ -401,7 +448,7 @@ func (f *File) getSheetRelationshipsTargetByID(sheet, rID string) string { } var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels" var sheetRels xlsxWorkbookRels - _ = xml.Unmarshal([]byte(f.readXML(rels)), &sheetRels) + _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &sheetRels) for _, v := range sheetRels.Relationships { if v.ID == rID { return v.Target @@ -441,7 +488,7 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte) { return "", nil } decodeWsDr := decodeWsDr{} - _ = xml.Unmarshal([]byte(f.readXML(drawingXML)), &decodeWsDr) + _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(drawingXML)), &decodeWsDr) cell = strings.ToUpper(cell) fromCol := string(strings.Map(letterOnlyMapF, cell)) @@ -476,7 +523,7 @@ func (f *File) getDrawingRelationships(rels, rID string) *xlsxWorkbookRelation { return nil } var drawingRels xlsxWorkbookRels - _ = xml.Unmarshal([]byte(f.readXML(rels)), &drawingRels) + _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &drawingRels) for _, v := range drawingRels.Relationships { if v.ID == rID { return &v |