excelize/file.go

210 lines
5.5 KiB
Go
Raw Normal View History

// Copyright 2016 - 2021 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.
//
// Package excelize providing a set of functions that allow you to write to
2020-06-22 00:14:56 +08:00
// and read from XLSX / XLSM / XLTM files. Supports reading and writing
// spreadsheet documents generated by Microsoft Excel™ 2007 and later. Supports
2020-06-22 00:14:56 +08:00
// complex components by high compatibility, and provided streaming API for
// generating or reading data from a worksheet with huge amounts of data. This
// library needs Go version 1.15 or later.
2018-09-14 00:58:48 +08:00
2016-08-30 11:51:31 +08:00
package excelize
import (
"archive/zip"
"bytes"
2017-02-17 02:25:55 +08:00
"fmt"
"io"
2016-08-30 11:51:31 +08:00
"os"
"path/filepath"
"sync"
2016-08-30 11:51:31 +08:00
)
2018-08-06 10:21:24 +08:00
// NewFile provides a function to create new file by default template. For
// example:
//
// f := NewFile()
//
func NewFile() *File {
f := newFile()
f.Pkg.Store("_rels/.rels", []byte(XMLHeader+templateRels))
f.Pkg.Store("docProps/app.xml", []byte(XMLHeader+templateDocpropsApp))
f.Pkg.Store("docProps/core.xml", []byte(XMLHeader+templateDocpropsCore))
f.Pkg.Store("xl/_rels/workbook.xml.rels", []byte(XMLHeader+templateWorkbookRels))
f.Pkg.Store("xl/theme/theme1.xml", []byte(XMLHeader+templateTheme))
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(XMLHeader+templateSheet))
f.Pkg.Store("xl/styles.xml", []byte(XMLHeader+templateStyles))
f.Pkg.Store("xl/workbook.xml", []byte(XMLHeader+templateWorkbook))
f.Pkg.Store("[Content_Types].xml", []byte(XMLHeader+templateContentTypes))
f.SheetCount = 1
f.CalcChain = f.calcChainReader()
f.Comments = make(map[string]*xlsxComments)
f.ContentTypes = f.contentTypesReader()
f.Drawings = sync.Map{}
f.Styles = f.stylesReader()
f.DecodeVMLDrawing = make(map[string]*decodeVmlDrawing)
f.VMLDrawing = make(map[string]*vmlDrawing)
f.WorkBook = f.workbookReader()
f.Relationships = sync.Map{}
f.Relationships.Store("xl/_rels/workbook.xml.rels", f.relsReader("xl/_rels/workbook.xml.rels"))
f.sheetMap["Sheet1"] = "xl/worksheets/sheet1.xml"
ws, _ := f.workSheetReader("Sheet1")
f.Sheet.Store("xl/worksheets/sheet1.xml", ws)
f.Theme = f.themeReader()
return f
}
// Save provides a function to override the spreadsheet with origin path.
func (f *File) Save() error {
2017-02-17 02:25:55 +08:00
if f.Path == "" {
2018-09-12 15:47:56 +08:00
return fmt.Errorf("no path defined for file, consider File.WriteTo or File.Write")
}
return f.SaveAs(f.Path)
2016-08-30 11:51:31 +08:00
}
// SaveAs provides a function to create or update to an spreadsheet at the
2018-08-06 10:21:24 +08:00
// provided path.
2020-09-06 18:06:59 +08:00
func (f *File) SaveAs(name string, opt ...Options) error {
if len(name) > MaxFileNameLength {
return ErrMaxFileNameLength
}
f.Path = name
file, err := os.OpenFile(filepath.Clean(name), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0600)
2017-02-17 02:25:55 +08:00
if err != nil {
return err
}
defer file.Close()
f.options = nil
for i := range opt {
f.options = &opt[i]
2020-09-06 18:06:59 +08:00
}
2017-02-17 02:25:55 +08:00
return f.Write(file)
}
// Close closes and cleanup the open temporary file for the spreadsheet.
func (f *File) Close() error {
var err error
f.tempFiles.Range(func(k, v interface{}) bool {
if err = os.Remove(v.(string)); err != nil {
return false
}
return true
})
return err
}
2018-08-06 10:21:24 +08:00
// Write provides a function to write to an io.Writer.
2017-02-17 02:25:55 +08:00
func (f *File) Write(w io.Writer) error {
_, err := f.WriteTo(w)
return err
}
// WriteTo implements io.WriterTo to write the file.
func (f *File) WriteTo(w io.Writer) (int64, error) {
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 {
return 0, err
}
return 0, nil
}
// 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.
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)
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 {
_ = zw.Close()
return err
}
return zw.Close()
}
// writeToZip provides a function to write to zip.Writer
func (f *File) writeToZip(zw *zip.Writer) error {
f.calcChainWriter()
f.commentsWriter()
f.contentTypesWriter()
f.drawingsWriter()
f.vmlDrawingWriter()
f.workBookWriter()
f.workSheetWriter()
f.relsWriter()
2020-04-06 00:23:27 +08:00
f.sharedStringsWriter()
f.styleSheetWriter()
for path, stream := range f.streams {
fi, err := zw.Create(path)
if err != nil {
return err
}
var from io.Reader
from, err = stream.rawData.Reader()
if err != nil {
_ = stream.rawData.Close()
return err
}
_, err = io.Copy(fi, from)
if err != nil {
return err
}
_ = stream.rawData.Close()
}
var err error
f.Pkg.Range(func(path, content interface{}) bool {
2016-08-30 11:51:31 +08:00
if err != nil {
return false
2016-08-30 11:51:31 +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 {
return false
2016-08-30 11:51:31 +08:00
}
_, err = fi.Write(content.([]byte))
return true
})
f.tempFiles.Range(func(path, content interface{}) bool {
var fi io.Writer
fi, err = zw.Create(path.(string))
if err != nil {
return false
}
_, err = fi.Write(f.readBytes(path.(string)))
return true
})
return err
2016-08-30 11:51:31 +08:00
}