2021-02-15 00:09:35 +08:00
|
|
|
// 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
|
2021-03-30 23:02:22 +08:00
|
|
|
// 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
|
2021-04-04 15:29:43 +08:00
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// NewFile provides a function to create new file by default template. For
|
2017-01-18 16:05:01 +08:00
|
|
|
// example:
|
|
|
|
//
|
2020-11-10 23:48:09 +08:00
|
|
|
// f := NewFile()
|
2017-01-18 16:05:01 +08:00
|
|
|
//
|
2017-06-28 17:03:20 +08:00
|
|
|
func NewFile() *File {
|
2018-05-07 16:12:51 +08:00
|
|
|
file := make(map[string][]byte)
|
|
|
|
file["_rels/.rels"] = []byte(XMLHeader + templateRels)
|
|
|
|
file["docProps/app.xml"] = []byte(XMLHeader + templateDocpropsApp)
|
|
|
|
file["docProps/core.xml"] = []byte(XMLHeader + templateDocpropsCore)
|
|
|
|
file["xl/_rels/workbook.xml.rels"] = []byte(XMLHeader + templateWorkbookRels)
|
|
|
|
file["xl/theme/theme1.xml"] = []byte(XMLHeader + templateTheme)
|
|
|
|
file["xl/worksheets/sheet1.xml"] = []byte(XMLHeader + templateSheet)
|
|
|
|
file["xl/styles.xml"] = []byte(XMLHeader + templateStyles)
|
|
|
|
file["xl/workbook.xml"] = []byte(XMLHeader + templateWorkbook)
|
|
|
|
file["[Content_Types].xml"] = []byte(XMLHeader + templateContentTypes)
|
2019-12-20 00:30:48 +08:00
|
|
|
f := newFile()
|
|
|
|
f.SheetCount, f.XLSX = 1, file
|
2019-02-25 00:29:58 +08:00
|
|
|
f.CalcChain = f.calcChainReader()
|
|
|
|
f.Comments = make(map[string]*xlsxComments)
|
2017-09-13 22:00:33 +08:00
|
|
|
f.ContentTypes = f.contentTypesReader()
|
2019-02-25 22:14:34 +08:00
|
|
|
f.Drawings = make(map[string]*xlsxWsDr)
|
2017-09-13 22:00:33 +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)
|
2017-09-13 22:00:33 +08:00
|
|
|
f.WorkBook = f.workbookReader()
|
2019-09-16 01:17:35 +08:00
|
|
|
f.Relationships = make(map[string]*xlsxRelationships)
|
|
|
|
f.Relationships["xl/_rels/workbook.xml.rels"] = f.relsReader("xl/_rels/workbook.xml.rels")
|
2019-04-15 11:22:57 +08:00
|
|
|
f.Sheet["xl/worksheets/sheet1.xml"], _ = f.workSheetReader("Sheet1")
|
2017-09-13 22:00:33 +08:00
|
|
|
f.sheetMap["Sheet1"] = "xl/worksheets/sheet1.xml"
|
2018-07-07 15:59:48 +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.
|
2016-09-05 16:37:15 +08:00
|
|
|
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")
|
2016-09-05 16:37:15 +08:00
|
|
|
}
|
2017-06-28 17:03:20 +08:00
|
|
|
return f.SaveAs(f.Path)
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|
|
|
|
|
2020-11-10 23:48:09 +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 {
|
2020-09-18 22:20:58 +08:00
|
|
|
if len(name) > MaxFileNameLength {
|
2021-05-10 00:09:24 +08:00
|
|
|
return ErrMaxFileNameLength
|
2020-05-29 00:26:40 +08:00
|
|
|
}
|
2017-02-17 02:25:55 +08:00
|
|
|
file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2020-09-22 22:29:43 +08:00
|
|
|
f.options = nil
|
2020-09-06 18:06:59 +08:00
|
|
|
for _, o := range opt {
|
|
|
|
f.options = &o
|
|
|
|
}
|
2017-02-17 02:25:55 +08:00
|
|
|
return f.Write(file)
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-06-14 23:00:00 +08:00
|
|
|
_, err := f.WriteTo(w)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo implements io.WriterTo to write the file.
|
|
|
|
func (f *File) WriteTo(w io.Writer) (int64, error) {
|
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-06-22 14:06:08 +08:00
|
|
|
// WriteToBuffer provides a function to get bytes.Buffer from the saved file. And it allocate 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 {
|
|
|
|
zw.Close()
|
|
|
|
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()
|
2020-04-06 00:23:27 +08:00
|
|
|
f.sharedStringsWriter()
|
2017-06-26 18:44:19 +08:00
|
|
|
f.styleSheetWriter()
|
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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
stream.rawData.Close()
|
|
|
|
}
|
|
|
|
|
2016-09-05 16:37:15 +08:00
|
|
|
for path, content := range f.XLSX {
|
2021-04-03 22:56:02 +08:00
|
|
|
if _, ok := f.streams[path]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2017-02-17 02:25:55 +08:00
|
|
|
fi, err := zw.Create(path)
|
2016-08-30 11:51:31 +08:00
|
|
|
if err != nil {
|
2021-06-22 14:06:08 +08:00
|
|
|
return err
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|
2018-05-26 16:23:15 +08:00
|
|
|
_, err = fi.Write(content)
|
2016-08-30 11:51:31 +08:00
|
|
|
if err != nil {
|
2021-06-22 14:06:08 +08:00
|
|
|
return err
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 18:06:59 +08:00
|
|
|
|
2021-06-22 14:06:08 +08:00
|
|
|
return nil
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|