2017-01-17 19:06:42 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-02-28 12:59:04 +08:00
|
|
|
"encoding/json"
|
2017-01-17 19:06:42 +08:00
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
2017-01-22 16:16:03 +08:00
|
|
|
"image"
|
2017-01-17 19:06:42 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-02-28 12:59:04 +08:00
|
|
|
// parseFormatPictureSet provides function to parse the format settings of the
|
|
|
|
// picture with default value.
|
2017-03-06 12:05:41 +08:00
|
|
|
func parseFormatPictureSet(formatSet string) *formatPicture {
|
|
|
|
format := formatPicture{
|
2017-02-28 12:59:04 +08:00
|
|
|
FPrintsWithSheet: true,
|
|
|
|
FLocksWithSheet: false,
|
|
|
|
NoChangeAspect: false,
|
|
|
|
OffsetX: 0,
|
|
|
|
OffsetY: 0,
|
|
|
|
XScale: 1.0,
|
|
|
|
YScale: 1.0,
|
|
|
|
}
|
|
|
|
json.Unmarshal([]byte(formatSet), &format)
|
|
|
|
return &format
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPicture provides the method to add picture in a sheet by given picture
|
|
|
|
// format set (such as offset, scale, aspect ratio setting and print settings)
|
|
|
|
// and file path. For example:
|
2017-01-17 19:06:42 +08:00
|
|
|
//
|
2017-01-22 19:20:33 +08:00
|
|
|
// package main
|
2017-01-22 16:16:03 +08:00
|
|
|
//
|
2017-01-22 19:20:33 +08:00
|
|
|
// import (
|
|
|
|
// "fmt"
|
|
|
|
// _ "image/gif"
|
|
|
|
// _ "image/jpeg"
|
|
|
|
// _ "image/png"
|
2017-01-22 16:16:03 +08:00
|
|
|
//
|
2017-11-16 11:07:39 +08:00
|
|
|
// "github.com/360EntSecGroup-Skylar/excelize"
|
2017-01-22 19:20:33 +08:00
|
|
|
// )
|
2017-01-22 16:16:03 +08:00
|
|
|
//
|
2017-01-22 19:20:33 +08:00
|
|
|
// func main() {
|
2017-06-28 17:03:20 +08:00
|
|
|
// xlsx := excelize.NewFile()
|
2017-01-22 16:16:03 +08:00
|
|
|
// // Insert a picture.
|
2017-04-30 20:03:43 +08:00
|
|
|
// err := xlsx.AddPicture("Sheet1", "A2", "./image1.jpg", "")
|
2017-02-12 19:03:24 +08:00
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// }
|
2017-01-22 16:16:03 +08:00
|
|
|
// // Insert a picture to sheet with scaling.
|
2017-04-30 20:03:43 +08:00
|
|
|
// err = xlsx.AddPicture("Sheet1", "D2", "./image1.png", `{"x_scale": 0.5, "y_scale": 0.5}`)
|
2017-02-12 19:03:24 +08:00
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// }
|
2017-02-28 12:59:04 +08:00
|
|
|
// // Insert a picture offset in the cell with printing support.
|
2017-04-30 20:03:43 +08:00
|
|
|
// err = xlsx.AddPicture("Sheet1", "H2", "./image3.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`)
|
2017-01-22 16:16:03 +08:00
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// }
|
2017-06-28 17:03:20 +08:00
|
|
|
// err = xlsx.SaveAs("./Workbook.xlsx")
|
2017-01-22 16:16:03 +08:00
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// }
|
2017-01-17 19:06:42 +08:00
|
|
|
// }
|
|
|
|
//
|
2017-02-28 12:59:04 +08:00
|
|
|
func (f *File) AddPicture(sheet, cell, picture, format string) error {
|
2017-01-17 19:06:42 +08:00
|
|
|
var err error
|
|
|
|
// Check picture exists first.
|
|
|
|
if _, err = os.Stat(picture); os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-28 21:18:06 +08:00
|
|
|
ext, ok := supportImageTypes[path.Ext(picture)]
|
2017-01-17 19:06:42 +08:00
|
|
|
if !ok {
|
|
|
|
return errors.New("Unsupported image extension")
|
|
|
|
}
|
2017-01-22 16:16:03 +08:00
|
|
|
readFile, _ := os.Open(picture)
|
|
|
|
image, _, err := image.DecodeConfig(readFile)
|
2017-01-17 19:06:42 +08:00
|
|
|
_, file := filepath.Split(picture)
|
2017-02-28 12:59:04 +08:00
|
|
|
formatSet := parseFormatPictureSet(format)
|
2017-01-17 19:06:42 +08:00
|
|
|
// Read sheet data.
|
2017-03-12 20:38:26 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-01-17 19:06:42 +08:00
|
|
|
// Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
|
|
|
|
drawingID := f.countDrawings() + 1
|
|
|
|
pictureID := f.countMedia() + 1
|
|
|
|
drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
|
2017-05-24 14:17:35 +08:00
|
|
|
drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
|
|
|
|
drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipImage, "../media/image"+strconv.Itoa(pictureID)+ext)
|
2017-04-23 00:10:23 +08:00
|
|
|
f.addDrawingPicture(sheet, drawingXML, cell, file, image.Width, image.Height, drawingRID, formatSet)
|
2017-01-17 19:06:42 +08:00
|
|
|
f.addMedia(picture, ext)
|
2017-05-24 14:17:35 +08:00
|
|
|
f.addContentTypePart(drawingID, "drawings")
|
2017-01-17 19:06:42 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:05:01 +08:00
|
|
|
// addSheetRelationships provides function to add
|
2017-09-13 22:00:33 +08:00
|
|
|
// xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name, relationship
|
|
|
|
// type and target.
|
2017-01-19 14:05:32 +08:00
|
|
|
func (f *File) addSheetRelationships(sheet, relType, target, targetMode string) int {
|
2017-10-20 14:40:57 +08:00
|
|
|
name, ok := f.sheetMap[trimSheetName(sheet)]
|
|
|
|
if !ok {
|
|
|
|
name = strings.ToLower(sheet) + ".xml"
|
|
|
|
}
|
|
|
|
var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
|
2017-01-17 19:06:42 +08:00
|
|
|
var sheetRels xlsxWorkbookRels
|
|
|
|
var rID = 1
|
|
|
|
var ID bytes.Buffer
|
|
|
|
ID.WriteString("rId")
|
|
|
|
ID.WriteString(strconv.Itoa(rID))
|
2017-10-20 14:40:57 +08:00
|
|
|
_, ok = f.XLSX[rels]
|
2017-01-17 19:06:42 +08:00
|
|
|
if ok {
|
|
|
|
ID.Reset()
|
|
|
|
xml.Unmarshal([]byte(f.readXML(rels)), &sheetRels)
|
|
|
|
rID = len(sheetRels.Relationships) + 1
|
|
|
|
ID.WriteString("rId")
|
|
|
|
ID.WriteString(strconv.Itoa(rID))
|
|
|
|
}
|
|
|
|
sheetRels.Relationships = append(sheetRels.Relationships, xlsxWorkbookRelation{
|
2017-01-19 14:05:32 +08:00
|
|
|
ID: ID.String(),
|
|
|
|
Type: relType,
|
|
|
|
Target: target,
|
|
|
|
TargetMode: targetMode,
|
2017-01-17 19:06:42 +08:00
|
|
|
})
|
2017-04-30 20:03:43 +08:00
|
|
|
output, _ := xml.Marshal(sheetRels)
|
2017-01-17 19:06:42 +08:00
|
|
|
f.saveFileList(rels, string(output))
|
|
|
|
return rID
|
|
|
|
}
|
|
|
|
|
2017-07-24 10:26:02 +08:00
|
|
|
// deleteSheetRelationships provides function to delete relationships in
|
2017-09-13 22:00:33 +08:00
|
|
|
// xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name and relationship
|
2017-07-24 10:26:02 +08:00
|
|
|
// index.
|
|
|
|
func (f *File) deleteSheetRelationships(sheet, rID string) {
|
2017-10-20 14:40:57 +08:00
|
|
|
name, ok := f.sheetMap[trimSheetName(sheet)]
|
|
|
|
if !ok {
|
|
|
|
name = strings.ToLower(sheet) + ".xml"
|
|
|
|
}
|
|
|
|
var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
|
2017-07-24 10:26:02 +08:00
|
|
|
var sheetRels xlsxWorkbookRels
|
|
|
|
xml.Unmarshal([]byte(f.readXML(rels)), &sheetRels)
|
|
|
|
for k, v := range sheetRels.Relationships {
|
2017-09-30 17:07:59 +08:00
|
|
|
if v.ID == rID {
|
|
|
|
sheetRels.Relationships = append(sheetRels.Relationships[:k], sheetRels.Relationships[k+1:]...)
|
2017-07-24 10:26:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
output, _ := xml.Marshal(sheetRels)
|
|
|
|
f.saveFileList(rels, string(output))
|
|
|
|
}
|
|
|
|
|
2017-05-13 13:28:21 +08:00
|
|
|
// addSheetLegacyDrawing provides function to add legacy drawing element to
|
2017-09-13 22:00:33 +08:00
|
|
|
// xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
|
2017-05-13 13:28:21 +08:00
|
|
|
func (f *File) addSheetLegacyDrawing(sheet string, rID int) {
|
|
|
|
xlsx := f.workSheetReader(sheet)
|
|
|
|
xlsx.LegacyDrawing = &xlsxLegacyDrawing{
|
|
|
|
RID: "rId" + strconv.Itoa(rID),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:05:01 +08:00
|
|
|
// addSheetDrawing provides function to add drawing element to
|
2017-09-13 22:00:33 +08:00
|
|
|
// xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
|
2017-01-17 19:06:42 +08:00
|
|
|
func (f *File) addSheetDrawing(sheet string, rID int) {
|
2017-03-12 20:38:26 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-01-17 19:06:42 +08:00
|
|
|
xlsx.Drawing = &xlsxDrawing{
|
|
|
|
RID: "rId" + strconv.Itoa(rID),
|
|
|
|
}
|
2017-01-24 18:29:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// addSheetPicture provides function to add picture element to
|
2017-09-13 22:00:33 +08:00
|
|
|
// xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
|
2017-01-24 18:29:02 +08:00
|
|
|
func (f *File) addSheetPicture(sheet string, rID int) {
|
2017-03-12 20:38:26 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-01-24 18:29:02 +08:00
|
|
|
xlsx.Picture = &xlsxPicture{
|
|
|
|
RID: "rId" + strconv.Itoa(rID),
|
|
|
|
}
|
2017-01-17 19:06:42 +08:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:05:01 +08:00
|
|
|
// countDrawings provides function to get drawing files count storage in the
|
|
|
|
// folder xl/drawings.
|
2017-01-17 19:06:42 +08:00
|
|
|
func (f *File) countDrawings() int {
|
|
|
|
count := 0
|
|
|
|
for k := range f.XLSX {
|
|
|
|
if strings.Contains(k, "xl/drawings/drawing") {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2017-04-23 00:10:23 +08:00
|
|
|
// addDrawingPicture provides function to add picture by given sheet,
|
|
|
|
// drawingXML, cell, file name, width, height relationship index and format
|
2017-05-24 14:17:35 +08:00
|
|
|
// sets.
|
2017-04-23 00:10:23 +08:00
|
|
|
func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, height, rID int, formatSet *formatPicture) {
|
2017-01-22 16:16:03 +08:00
|
|
|
cell = strings.ToUpper(cell)
|
|
|
|
fromCol := string(strings.Map(letterOnlyMapF, cell))
|
|
|
|
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
|
|
|
|
row := fromRow - 1
|
2017-06-27 17:53:06 +08:00
|
|
|
col := TitleToNumber(fromCol)
|
2017-02-28 12:59:04 +08:00
|
|
|
width = int(float64(width) * formatSet.XScale)
|
|
|
|
height = int(float64(height) * formatSet.YScale)
|
|
|
|
colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height)
|
2017-04-30 20:03:43 +08:00
|
|
|
content := xlsxWsDr{}
|
|
|
|
content.A = NameSpaceDrawingML
|
|
|
|
content.Xdr = NameSpaceDrawingMLSpreadSheet
|
2017-05-25 11:15:38 +08:00
|
|
|
cNvPrID := f.drawingParser(drawingXML, &content)
|
2017-04-30 20:03:43 +08:00
|
|
|
twoCellAnchor := xdrCellAnchor{}
|
2017-01-17 19:06:42 +08:00
|
|
|
twoCellAnchor.EditAs = "oneCell"
|
|
|
|
from := xlsxFrom{}
|
2017-01-22 16:16:03 +08:00
|
|
|
from.Col = colStart
|
2017-02-28 12:59:04 +08:00
|
|
|
from.ColOff = formatSet.OffsetX * EMU
|
2017-01-22 16:16:03 +08:00
|
|
|
from.Row = rowStart
|
2017-02-28 12:59:04 +08:00
|
|
|
from.RowOff = formatSet.OffsetY * EMU
|
2017-01-17 19:06:42 +08:00
|
|
|
to := xlsxTo{}
|
2017-01-22 16:16:03 +08:00
|
|
|
to.Col = colEnd
|
|
|
|
to.ColOff = x2 * EMU
|
|
|
|
to.Row = rowEnd
|
|
|
|
to.RowOff = y2 * EMU
|
2017-01-17 19:06:42 +08:00
|
|
|
twoCellAnchor.From = &from
|
|
|
|
twoCellAnchor.To = &to
|
|
|
|
pic := xlsxPic{}
|
2017-02-28 12:59:04 +08:00
|
|
|
pic.NvPicPr.CNvPicPr.PicLocks.NoChangeAspect = formatSet.NoChangeAspect
|
2017-04-30 20:03:43 +08:00
|
|
|
pic.NvPicPr.CNvPr.ID = f.countCharts() + f.countMedia() + 1
|
2017-01-17 19:06:42 +08:00
|
|
|
pic.NvPicPr.CNvPr.Descr = file
|
|
|
|
pic.NvPicPr.CNvPr.Name = "Picture " + strconv.Itoa(cNvPrID)
|
|
|
|
pic.BlipFill.Blip.R = SourceRelationship
|
|
|
|
pic.BlipFill.Blip.Embed = "rId" + strconv.Itoa(rID)
|
|
|
|
pic.SpPr.PrstGeom.Prst = "rect"
|
|
|
|
|
|
|
|
twoCellAnchor.Pic = &pic
|
2017-04-30 20:03:43 +08:00
|
|
|
twoCellAnchor.ClientData = &xdrClientData{
|
2017-02-28 12:59:04 +08:00
|
|
|
FLocksWithSheet: formatSet.FLocksWithSheet,
|
|
|
|
FPrintsWithSheet: formatSet.FPrintsWithSheet,
|
2017-01-17 19:06:42 +08:00
|
|
|
}
|
2017-04-30 20:03:43 +08:00
|
|
|
content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
|
|
|
|
output, _ := xml.Marshal(content)
|
|
|
|
f.saveFileList(drawingXML, string(output))
|
2017-01-17 19:06:42 +08:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:05:01 +08:00
|
|
|
// addDrawingRelationships provides function to add image part relationships in
|
|
|
|
// the file xl/drawings/_rels/drawing%d.xml.rels by given drawing index,
|
|
|
|
// relationship type and target.
|
2017-06-12 18:05:09 +08:00
|
|
|
func (f *File) addDrawingRelationships(index int, relType, target string) int {
|
2017-01-17 19:06:42 +08:00
|
|
|
var rels = "xl/drawings/_rels/drawing" + strconv.Itoa(index) + ".xml.rels"
|
|
|
|
var drawingRels xlsxWorkbookRels
|
|
|
|
var rID = 1
|
|
|
|
var ID bytes.Buffer
|
|
|
|
ID.WriteString("rId")
|
|
|
|
ID.WriteString(strconv.Itoa(rID))
|
|
|
|
_, ok := f.XLSX[rels]
|
|
|
|
if ok {
|
|
|
|
ID.Reset()
|
|
|
|
xml.Unmarshal([]byte(f.readXML(rels)), &drawingRels)
|
|
|
|
rID = len(drawingRels.Relationships) + 1
|
|
|
|
ID.WriteString("rId")
|
|
|
|
ID.WriteString(strconv.Itoa(rID))
|
|
|
|
}
|
|
|
|
drawingRels.Relationships = append(drawingRels.Relationships, xlsxWorkbookRelation{
|
|
|
|
ID: ID.String(),
|
|
|
|
Type: relType,
|
|
|
|
Target: target,
|
|
|
|
})
|
2017-04-30 20:03:43 +08:00
|
|
|
output, _ := xml.Marshal(drawingRels)
|
2017-01-17 19:06:42 +08:00
|
|
|
f.saveFileList(rels, string(output))
|
|
|
|
return rID
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:05:01 +08:00
|
|
|
// countMedia provides function to get media files count storage in the folder
|
|
|
|
// xl/media/image.
|
2017-01-17 19:06:42 +08:00
|
|
|
func (f *File) countMedia() int {
|
|
|
|
count := 0
|
|
|
|
for k := range f.XLSX {
|
|
|
|
if strings.Contains(k, "xl/media/image") {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:05:01 +08:00
|
|
|
// addMedia provides function to add picture into folder xl/media/image by given
|
|
|
|
// file name and extension name.
|
2017-06-12 18:05:09 +08:00
|
|
|
func (f *File) addMedia(file, ext string) {
|
2017-01-17 19:06:42 +08:00
|
|
|
count := f.countMedia()
|
|
|
|
dat, _ := ioutil.ReadFile(file)
|
|
|
|
media := "xl/media/image" + strconv.Itoa(count+1) + ext
|
|
|
|
f.XLSX[media] = string(dat)
|
|
|
|
}
|
|
|
|
|
2017-02-28 12:59:04 +08:00
|
|
|
// setContentTypePartImageExtensions provides function to set the content type
|
|
|
|
// for relationship parts and the Main Document part.
|
2017-01-24 18:29:02 +08:00
|
|
|
func (f *File) setContentTypePartImageExtensions() {
|
2017-01-22 16:16:03 +08:00
|
|
|
var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false}
|
2017-04-01 13:56:39 +08:00
|
|
|
content := f.contentTypesReader()
|
2017-01-17 19:06:42 +08:00
|
|
|
for _, v := range content.Defaults {
|
|
|
|
_, ok := imageTypes[v.Extension]
|
|
|
|
if ok {
|
|
|
|
imageTypes[v.Extension] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range imageTypes {
|
|
|
|
if !v {
|
|
|
|
content.Defaults = append(content.Defaults, xlsxDefault{
|
|
|
|
Extension: k,
|
|
|
|
ContentType: "image/" + k,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 18:29:02 +08:00
|
|
|
}
|
|
|
|
|
2017-05-13 13:28:21 +08:00
|
|
|
// setContentTypePartVMLExtensions provides function to set the content type
|
|
|
|
// for relationship parts and the Main Document part.
|
|
|
|
func (f *File) setContentTypePartVMLExtensions() {
|
|
|
|
vml := false
|
|
|
|
content := f.contentTypesReader()
|
|
|
|
for _, v := range content.Defaults {
|
|
|
|
if v.Extension == "vml" {
|
|
|
|
vml = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !vml {
|
|
|
|
content.Defaults = append(content.Defaults, xlsxDefault{
|
|
|
|
Extension: "vml",
|
|
|
|
ContentType: "application/vnd.openxmlformats-officedocument.vmlDrawing",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 14:17:35 +08:00
|
|
|
// addContentTypePart provides function to add content type part relationships
|
|
|
|
// in the file [Content_Types].xml by given index.
|
|
|
|
func (f *File) addContentTypePart(index int, contentType string) {
|
|
|
|
setContentType := map[string]func(){
|
|
|
|
"comments": f.setContentTypePartVMLExtensions,
|
|
|
|
"drawings": f.setContentTypePartImageExtensions,
|
|
|
|
}
|
|
|
|
partNames := map[string]string{
|
|
|
|
"chart": "/xl/charts/chart" + strconv.Itoa(index) + ".xml",
|
|
|
|
"comments": "/xl/comments" + strconv.Itoa(index) + ".xml",
|
|
|
|
"drawings": "/xl/drawings/drawing" + strconv.Itoa(index) + ".xml",
|
|
|
|
"table": "/xl/tables/table" + strconv.Itoa(index) + ".xml",
|
|
|
|
}
|
|
|
|
contentTypes := map[string]string{
|
|
|
|
"chart": "application/vnd.openxmlformats-officedocument.drawingml.chart+xml",
|
|
|
|
"comments": "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
|
|
|
|
"drawings": "application/vnd.openxmlformats-officedocument.drawing+xml",
|
|
|
|
"table": "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml",
|
|
|
|
}
|
|
|
|
s, ok := setContentType[contentType]
|
|
|
|
if ok {
|
|
|
|
s()
|
2017-01-17 19:06:42 +08:00
|
|
|
}
|
2017-05-13 13:28:21 +08:00
|
|
|
content := f.contentTypesReader()
|
|
|
|
for _, v := range content.Overrides {
|
2017-05-24 14:17:35 +08:00
|
|
|
if v.PartName == partNames[contentType] {
|
2017-05-13 13:28:21 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
content.Overrides = append(content.Overrides, xlsxOverride{
|
2017-05-24 14:17:35 +08:00
|
|
|
PartName: partNames[contentType],
|
|
|
|
ContentType: contentTypes[contentType],
|
2017-05-13 13:28:21 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:05:01 +08:00
|
|
|
// getSheetRelationshipsTargetByID provides function to get Target attribute
|
2017-09-13 22:00:33 +08:00
|
|
|
// value in xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name and
|
2017-01-18 16:05:01 +08:00
|
|
|
// relationship index.
|
2017-06-12 18:05:09 +08:00
|
|
|
func (f *File) getSheetRelationshipsTargetByID(sheet, rID string) string {
|
2017-10-31 16:33:36 +08:00
|
|
|
name, ok := f.sheetMap[trimSheetName(sheet)]
|
|
|
|
if !ok {
|
|
|
|
name = strings.ToLower(sheet) + ".xml"
|
|
|
|
}
|
|
|
|
var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
|
2017-01-17 19:06:42 +08:00
|
|
|
var sheetRels xlsxWorkbookRels
|
|
|
|
xml.Unmarshal([]byte(f.readXML(rels)), &sheetRels)
|
|
|
|
for _, v := range sheetRels.Relationships {
|
|
|
|
if v.ID == rID {
|
|
|
|
return v.Target
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2017-03-28 21:18:06 +08:00
|
|
|
|
|
|
|
// GetPicture provides function to get picture base name and raw content embed
|
|
|
|
// in XLSX by given worksheet and cell name. This function returns the file name
|
|
|
|
// in XLSX and file contents as []byte data types. For example:
|
|
|
|
//
|
2017-04-30 20:03:43 +08:00
|
|
|
// xlsx, err := excelize.OpenFile("./Workbook.xlsx")
|
2017-03-28 21:18:06 +08:00
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
2017-10-20 14:40:57 +08:00
|
|
|
// return
|
2017-03-28 21:18:06 +08:00
|
|
|
// }
|
|
|
|
// file, raw := xlsx.GetPicture("Sheet1", "A2")
|
|
|
|
// if file == "" {
|
2017-10-20 14:40:57 +08:00
|
|
|
// return
|
2017-03-28 21:18:06 +08:00
|
|
|
// }
|
2017-03-30 11:06:51 +08:00
|
|
|
// err := ioutil.WriteFile(file, raw, 0644)
|
|
|
|
// if err != nil {
|
2017-03-28 21:18:06 +08:00
|
|
|
// fmt.Println(err)
|
2017-03-30 11:06:51 +08:00
|
|
|
// }
|
2017-03-28 21:18:06 +08:00
|
|
|
//
|
|
|
|
func (f *File) GetPicture(sheet, cell string) (string, []byte) {
|
|
|
|
xlsx := f.workSheetReader(sheet)
|
|
|
|
if xlsx.Drawing == nil {
|
|
|
|
return "", []byte{}
|
|
|
|
}
|
|
|
|
target := f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
|
|
|
|
drawingXML := strings.Replace(target, "..", "xl", -1)
|
|
|
|
|
|
|
|
_, ok := f.XLSX[drawingXML]
|
|
|
|
if !ok {
|
|
|
|
return "", []byte{}
|
|
|
|
}
|
|
|
|
decodeWsDr := decodeWsDr{}
|
|
|
|
xml.Unmarshal([]byte(f.readXML(drawingXML)), &decodeWsDr)
|
|
|
|
|
|
|
|
cell = strings.ToUpper(cell)
|
|
|
|
fromCol := string(strings.Map(letterOnlyMapF, cell))
|
|
|
|
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
|
|
|
|
row := fromRow - 1
|
2017-06-27 17:53:06 +08:00
|
|
|
col := TitleToNumber(fromCol)
|
2017-03-28 21:18:06 +08:00
|
|
|
|
|
|
|
drawingRelationships := strings.Replace(strings.Replace(target, "../drawings", "xl/drawings/_rels", -1), ".xml", ".xml.rels", -1)
|
|
|
|
|
|
|
|
for _, anchor := range decodeWsDr.TwoCellAnchor {
|
|
|
|
decodeTwoCellAnchor := decodeTwoCellAnchor{}
|
|
|
|
xml.Unmarshal([]byte("<decodeTwoCellAnchor>"+anchor.Content+"</decodeTwoCellAnchor>"), &decodeTwoCellAnchor)
|
2017-09-30 17:07:59 +08:00
|
|
|
if decodeTwoCellAnchor.From != nil && decodeTwoCellAnchor.Pic != nil {
|
|
|
|
if decodeTwoCellAnchor.From.Col == col && decodeTwoCellAnchor.From.Row == row {
|
|
|
|
xlsxWorkbookRelation := f.getDrawingRelationships(drawingRelationships, decodeTwoCellAnchor.Pic.BlipFill.Blip.Embed)
|
|
|
|
_, ok := supportImageTypes[filepath.Ext(xlsxWorkbookRelation.Target)]
|
|
|
|
if ok {
|
|
|
|
return filepath.Base(xlsxWorkbookRelation.Target), []byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target, "..", "xl", -1)])
|
|
|
|
}
|
2017-03-28 21:18:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDrawingRelationships provides function to get drawing relationships from
|
|
|
|
// xl/drawings/_rels/drawing%s.xml.rels by given file name and relationship ID.
|
|
|
|
func (f *File) getDrawingRelationships(rels, rID string) *xlsxWorkbookRelation {
|
|
|
|
_, ok := f.XLSX[rels]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var drawingRels xlsxWorkbookRels
|
|
|
|
xml.Unmarshal([]byte(f.readXML(rels)), &drawingRels)
|
|
|
|
for _, v := range drawingRels.Relationships {
|
2017-09-30 17:07:59 +08:00
|
|
|
if v.ID == rID {
|
|
|
|
return &v
|
2017-03-28 21:18:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|