2023-01-02 11:47:31 +08:00
|
|
|
// Copyright 2016 - 2023 The excelize Authors. All rights reserved. Use of
|
2018-09-14 00:44:23 +08:00
|
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
//
|
2022-02-17 00:09:11 +08:00
|
|
|
// Package excelize providing a set of functions that allow you to write to and
|
|
|
|
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
|
|
|
|
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
|
|
|
|
// Supports complex components by high compatibility, and provided streaming
|
|
|
|
// API for generating or reading data from a worksheet with huge amounts of
|
2023-01-02 11:47:31 +08:00
|
|
|
// data. This library needs Go version 1.16 or later.
|
2018-09-14 00:58:48 +08:00
|
|
|
|
2016-08-30 11:51:31 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
2021-12-27 23:34:14 +08:00
|
|
|
"encoding/xml"
|
2017-02-17 02:25:55 +08:00
|
|
|
"io"
|
2016-08-30 11:51:31 +08:00
|
|
|
"os"
|
2021-08-15 00:06:40 +08:00
|
|
|
"path/filepath"
|
2021-07-04 12:13:06 +08:00
|
|
|
"sync"
|
2016-08-30 11:51:31 +08:00
|
|
|
)
|
|
|
|
|
2022-07-17 12:18:25 +08:00
|
|
|
// NewFile provides a function to create new file by default template.
|
|
|
|
// For example:
|
2017-01-18 16:05:01 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// f := NewFile()
|
2017-06-28 17:03:20 +08:00
|
|
|
func NewFile() *File {
|
2019-12-20 00:30:48 +08:00
|
|
|
f := newFile()
|
2021-12-27 23:34:14 +08:00
|
|
|
f.Pkg.Store("_rels/.rels", []byte(xml.Header+templateRels))
|
2022-01-09 00:20:42 +08:00
|
|
|
f.Pkg.Store(defaultXMLPathDocPropsApp, []byte(xml.Header+templateDocpropsApp))
|
|
|
|
f.Pkg.Store(defaultXMLPathDocPropsCore, []byte(xml.Header+templateDocpropsCore))
|
2022-11-13 00:40:04 +08:00
|
|
|
f.Pkg.Store(defaultXMLPathWorkbookRels, []byte(xml.Header+templateWorkbookRels))
|
2021-12-27 23:34:14 +08:00
|
|
|
f.Pkg.Store("xl/theme/theme1.xml", []byte(xml.Header+templateTheme))
|
|
|
|
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(xml.Header+templateSheet))
|
|
|
|
f.Pkg.Store(defaultXMLPathStyles, []byte(xml.Header+templateStyles))
|
|
|
|
f.Pkg.Store(defaultXMLPathWorkbook, []byte(xml.Header+templateWorkbook))
|
|
|
|
f.Pkg.Store(defaultXMLPathContentTypes, []byte(xml.Header+templateContentTypes))
|
2021-07-05 00:03:56 +08:00
|
|
|
f.SheetCount = 1
|
2022-11-12 00:02:11 +08:00
|
|
|
f.CalcChain, _ = f.calcChainReader()
|
2019-02-25 00:29:58 +08:00
|
|
|
f.Comments = make(map[string]*xlsxComments)
|
2022-11-13 00:40:04 +08:00
|
|
|
f.ContentTypes, _ = f.contentTypesReader()
|
2021-07-04 12:13:06 +08:00
|
|
|
f.Drawings = sync.Map{}
|
2022-11-12 00:02:11 +08:00
|
|
|
f.Styles, _ = f.stylesReader()
|
2019-02-25 00:29:58 +08:00
|
|
|
f.DecodeVMLDrawing = make(map[string]*decodeVmlDrawing)
|
|
|
|
f.VMLDrawing = make(map[string]*vmlDrawing)
|
2022-11-13 00:40:04 +08:00
|
|
|
f.WorkBook, _ = f.workbookReader()
|
2021-07-04 12:13:06 +08:00
|
|
|
f.Relationships = sync.Map{}
|
2022-11-13 00:40:04 +08:00
|
|
|
rels, _ := f.relsReader(defaultXMLPathWorkbookRels)
|
|
|
|
f.Relationships.Store(defaultXMLPathWorkbookRels, rels)
|
2017-09-13 22:00:33 +08:00
|
|
|
f.sheetMap["Sheet1"] = "xl/worksheets/sheet1.xml"
|
2021-07-05 00:03:56 +08:00
|
|
|
ws, _ := f.workSheetReader("Sheet1")
|
|
|
|
f.Sheet.Store("xl/worksheets/sheet1.xml", ws)
|
2022-11-13 00:40:04 +08:00
|
|
|
f.Theme, _ = f.themeReader()
|
2017-09-13 22:00:33 +08:00
|
|
|
return f
|
2016-09-05 16:37:15 +08:00
|
|
|
}
|
|
|
|
|
2020-11-10 23:48:09 +08:00
|
|
|
// Save provides a function to override the spreadsheet with origin path.
|
2022-09-08 22:20:21 +08:00
|
|
|
func (f *File) Save(opts ...Options) error {
|
2017-02-17 02:25:55 +08:00
|
|
|
if f.Path == "" {
|
2021-12-07 00:26:53 +08:00
|
|
|
return ErrSave
|
2016-09-05 16:37:15 +08:00
|
|
|
}
|
2022-09-08 22:20:21 +08:00
|
|
|
for i := range opts {
|
|
|
|
f.options = &opts[i]
|
2022-05-29 19:37:10 +08:00
|
|
|
}
|
2022-09-08 22:20:21 +08:00
|
|
|
return f.SaveAs(f.Path, *f.options)
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|
|
|
|
|
2022-04-04 00:21:33 +08:00
|
|
|
// SaveAs provides a function to create or update to a spreadsheet at the
|
2018-08-06 10:21:24 +08:00
|
|
|
// provided path.
|
2022-09-08 22:20:21 +08:00
|
|
|
func (f *File) SaveAs(name string, opts ...Options) error {
|
2022-05-16 21:05:22 +08:00
|
|
|
if len(name) > MaxFilePathLength {
|
|
|
|
return ErrMaxFilePathLength
|
2020-05-29 00:26:40 +08:00
|
|
|
}
|
2021-07-09 00:04:58 +08:00
|
|
|
f.Path = name
|
2022-10-14 00:48:16 +08:00
|
|
|
if _, ok := supportedContentTypes[filepath.Ext(f.Path)]; !ok {
|
2022-05-20 20:46:29 +08:00
|
|
|
return ErrWorkbookFileFormat
|
2022-01-23 00:32:34 +08:00
|
|
|
}
|
2022-04-04 00:21:33 +08:00
|
|
|
file, err := os.OpenFile(filepath.Clean(name), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
|
2017-02-17 02:25:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2022-09-08 22:20:21 +08:00
|
|
|
return f.Write(file, opts...)
|
2017-02-17 02:25:55 +08:00
|
|
|
}
|
|
|
|
|
2021-09-18 23:20:24 +08:00
|
|
|
// Close closes and cleanup the open temporary file for the spreadsheet.
|
|
|
|
func (f *File) Close() error {
|
|
|
|
var err error
|
2022-01-11 00:24:24 +08:00
|
|
|
if f.sharedStringTemp != nil {
|
|
|
|
if err := f.sharedStringTemp.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-09-18 23:20:24 +08:00
|
|
|
f.tempFiles.Range(func(k, v interface{}) bool {
|
|
|
|
if err = os.Remove(v.(string)); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2022-10-26 00:04:23 +08:00
|
|
|
for _, stream := range f.streams {
|
|
|
|
_ = stream.rawData.Close()
|
|
|
|
}
|
2021-09-18 23:20:24 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// Write provides a function to write to an io.Writer.
|
2022-09-08 22:20:21 +08:00
|
|
|
func (f *File) Write(w io.Writer, opts ...Options) error {
|
|
|
|
_, err := f.WriteTo(w, opts...)
|
2018-06-14 23:00:00 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo implements io.WriterTo to write the file.
|
2022-09-08 22:20:21 +08:00
|
|
|
func (f *File) WriteTo(w io.Writer, opts ...Options) (int64, error) {
|
|
|
|
for i := range opts {
|
|
|
|
f.options = &opts[i]
|
|
|
|
}
|
|
|
|
if len(f.Path) != 0 {
|
2022-10-14 00:48:16 +08:00
|
|
|
contentType, ok := supportedContentTypes[filepath.Ext(f.Path)]
|
2022-09-08 22:20:21 +08:00
|
|
|
if !ok {
|
|
|
|
return 0, ErrWorkbookFileFormat
|
|
|
|
}
|
2022-11-13 00:40:04 +08:00
|
|
|
if err := f.setContentTypePartProjectExtensions(contentType); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-09-08 22:20:21 +08:00
|
|
|
}
|
2021-06-22 14:06:08 +08:00
|
|
|
if f.options != nil && f.options.Password != "" {
|
|
|
|
buf, err := f.WriteToBuffer()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return buf.WriteTo(w)
|
|
|
|
}
|
|
|
|
if err := f.writeDirectToWriter(w); err != nil {
|
2018-10-08 22:17:33 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-06-22 14:06:08 +08:00
|
|
|
return 0, nil
|
2018-10-08 22:17:33 +08:00
|
|
|
}
|
|
|
|
|
2021-08-15 00:06:40 +08:00
|
|
|
// WriteToBuffer provides a function to get bytes.Buffer from the saved file,
|
|
|
|
// and it allocates space in memory. Be careful when the file size is large.
|
2018-10-08 22:17:33 +08:00
|
|
|
func (f *File) WriteToBuffer() (*bytes.Buffer, error) {
|
2016-08-30 11:51:31 +08:00
|
|
|
buf := new(bytes.Buffer)
|
2017-02-17 02:25:55 +08:00
|
|
|
zw := zip.NewWriter(buf)
|
2021-06-22 14:06:08 +08:00
|
|
|
|
|
|
|
if err := f.writeToZip(zw); err != nil {
|
|
|
|
return buf, zw.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.options != nil && f.options.Password != "" {
|
|
|
|
if err := zw.Close(); err != nil {
|
|
|
|
return buf, err
|
|
|
|
}
|
|
|
|
b, err := Encrypt(buf.Bytes(), f.options)
|
|
|
|
if err != nil {
|
|
|
|
return buf, err
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
buf.Write(b)
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
return buf, zw.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeDirectToWriter provides a function to write to io.Writer.
|
|
|
|
func (f *File) writeDirectToWriter(w io.Writer) error {
|
|
|
|
zw := zip.NewWriter(w)
|
|
|
|
if err := f.writeToZip(zw); err != nil {
|
2021-08-15 00:06:40 +08:00
|
|
|
_ = zw.Close()
|
2021-06-22 14:06:08 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return zw.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeToZip provides a function to write to zip.Writer
|
|
|
|
func (f *File) writeToZip(zw *zip.Writer) error {
|
2019-02-25 00:29:58 +08:00
|
|
|
f.calcChainWriter()
|
|
|
|
f.commentsWriter()
|
2017-04-01 13:56:39 +08:00
|
|
|
f.contentTypesWriter()
|
2019-02-25 22:14:34 +08:00
|
|
|
f.drawingsWriter()
|
2019-02-25 00:29:58 +08:00
|
|
|
f.vmlDrawingWriter()
|
2019-02-26 14:21:44 +08:00
|
|
|
f.workBookWriter()
|
|
|
|
f.workSheetWriter()
|
2019-09-16 01:17:35 +08:00
|
|
|
f.relsWriter()
|
2022-10-24 00:02:22 +08:00
|
|
|
_ = f.sharedStringsLoader()
|
2020-04-06 00:23:27 +08:00
|
|
|
f.sharedStringsWriter()
|
2017-06-26 18:44:19 +08:00
|
|
|
f.styleSheetWriter()
|
2022-10-28 00:31:55 +08:00
|
|
|
f.themeWriter()
|
2019-02-25 00:29:58 +08:00
|
|
|
|
2020-10-05 22:17:11 +08:00
|
|
|
for path, stream := range f.streams {
|
|
|
|
fi, err := zw.Create(path)
|
|
|
|
if err != nil {
|
2021-06-22 14:06:08 +08:00
|
|
|
return err
|
2020-10-05 22:17:11 +08:00
|
|
|
}
|
|
|
|
var from io.Reader
|
|
|
|
from, err = stream.rawData.Reader()
|
|
|
|
if err != nil {
|
2021-08-15 00:06:40 +08:00
|
|
|
_ = stream.rawData.Close()
|
2021-06-22 14:06:08 +08:00
|
|
|
return err
|
2020-10-05 22:17:11 +08:00
|
|
|
}
|
|
|
|
_, err = io.Copy(fi, from)
|
|
|
|
if err != nil {
|
2021-06-22 14:06:08 +08:00
|
|
|
return err
|
2020-10-05 22:17:11 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-05 00:03:56 +08:00
|
|
|
var err error
|
|
|
|
f.Pkg.Range(func(path, content interface{}) bool {
|
2016-08-30 11:51:31 +08:00
|
|
|
if err != nil {
|
2021-07-05 00:03:56 +08:00
|
|
|
return false
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|
2021-07-05 00:03:56 +08:00
|
|
|
if _, ok := f.streams[path.(string)]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
var fi io.Writer
|
|
|
|
fi, err = zw.Create(path.(string))
|
2016-08-30 11:51:31 +08:00
|
|
|
if err != nil {
|
2021-07-05 00:03:56 +08:00
|
|
|
return false
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|
2021-07-05 00:03:56 +08:00
|
|
|
_, err = fi.Write(content.([]byte))
|
|
|
|
return true
|
|
|
|
})
|
2021-12-01 00:10:31 +08:00
|
|
|
f.tempFiles.Range(func(path, content interface{}) bool {
|
2021-12-27 23:34:14 +08:00
|
|
|
if _, ok := f.Pkg.Load(path); ok {
|
|
|
|
return true
|
|
|
|
}
|
2021-12-01 00:10:31 +08:00
|
|
|
var fi io.Writer
|
|
|
|
fi, err = zw.Create(path.(string))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, err = fi.Write(f.readBytes(path.(string)))
|
|
|
|
return true
|
|
|
|
})
|
2021-07-05 00:03:56 +08:00
|
|
|
return err
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|