excelize/file.go

116 lines
3.4 KiB
Go
Raw Normal View History

2019-12-29 16:02:31 +08:00
// Copyright 2016 - 2020 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
// 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.10 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
// example:
//
// xlsx := NewFile()
//
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)
f := newFile()
f.SheetCount, f.XLSX = 1, file
f.CalcChain = f.calcChainReader()
f.Comments = make(map[string]*xlsxComments)
f.ContentTypes = f.contentTypesReader()
f.Drawings = make(map[string]*xlsxWsDr)
f.Styles = f.stylesReader()
f.DecodeVMLDrawing = make(map[string]*decodeVmlDrawing)
f.VMLDrawing = make(map[string]*vmlDrawing)
f.WorkBook = f.workbookReader()
f.Relationships = make(map[string]*xlsxRelationships)
f.Relationships["xl/_rels/workbook.xml.rels"] = f.relsReader("xl/_rels/workbook.xml.rels")
f.Sheet["xl/worksheets/sheet1.xml"], _ = f.workSheetReader("Sheet1")
f.sheetMap["Sheet1"] = "xl/worksheets/sheet1.xml"
f.Theme = f.themeReader()
return f
}
2018-08-06 10:21:24 +08:00
// Save provides a function to override the xlsx file 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
}
2018-08-06 10:21:24 +08:00
// SaveAs provides a function to create or update to an xlsx file at the
// provided path.
func (f *File) SaveAs(name string) error {
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()
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 {
_, err := f.WriteTo(w)
return err
}
// WriteTo implements io.WriterTo to write the file.
func (f *File) WriteTo(w io.Writer) (int64, error) {
buf, err := f.WriteToBuffer()
if err != nil {
return 0, err
}
return buf.WriteTo(w)
}
// WriteToBuffer provides a function to get bytes.Buffer from the saved file.
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)
f.calcChainWriter()
f.commentsWriter()
f.contentTypesWriter()
f.drawingsWriter()
f.vmlDrawingWriter()
f.workBookWriter()
f.workSheetWriter()
f.relsWriter()
f.styleSheetWriter()
for path, content := range f.XLSX {
2017-02-17 02:25:55 +08:00
fi, err := zw.Create(path)
2016-08-30 11:51:31 +08:00
if err != nil {
zw.Close()
return buf, err
2016-08-30 11:51:31 +08:00
}
_, err = fi.Write(content)
2016-08-30 11:51:31 +08:00
if err != nil {
zw.Close()
return buf, err
2016-08-30 11:51:31 +08:00
}
}
return buf, zw.Close()
2016-08-30 11:51:31 +08:00
}