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"
|
|
|
|
)
|
|
|
|
|
2017-06-28 17:03:20 +08:00
|
|
|
// NewFile provides function to create new file by default template. For
|
2017-01-18 16:05:01 +08:00
|
|
|
// example:
|
|
|
|
//
|
2017-06-28 17:03:20 +08:00
|
|
|
// xlsx := NewFile()
|
2017-01-18 16:05:01 +08:00
|
|
|
//
|
2017-06-28 17:03:20 +08:00
|
|
|
func NewFile() *File {
|
2016-09-05 10:44:32 +08:00
|
|
|
file := make(map[string]string)
|
2017-04-23 00:10:23 +08:00
|
|
|
file["_rels/.rels"] = XMLHeader + templateRels
|
|
|
|
file["docProps/app.xml"] = XMLHeader + templateDocpropsApp
|
|
|
|
file["docProps/core.xml"] = XMLHeader + templateDocpropsCore
|
|
|
|
file["xl/_rels/workbook.xml.rels"] = XMLHeader + templateWorkbookRels
|
|
|
|
file["xl/theme/theme1.xml"] = XMLHeader + templateTheme
|
|
|
|
file["xl/worksheets/sheet1.xml"] = XMLHeader + templateSheet
|
|
|
|
file["xl/styles.xml"] = XMLHeader + templateStyles
|
|
|
|
file["xl/workbook.xml"] = XMLHeader + templateWorkbook
|
|
|
|
file["[Content_Types].xml"] = XMLHeader + templateContentTypes
|
2017-09-13 22:00:33 +08:00
|
|
|
f := &File{
|
|
|
|
sheetMap: make(map[string]string),
|
|
|
|
Sheet: make(map[string]*xlsxWorksheet),
|
|
|
|
SheetCount: 1,
|
|
|
|
XLSX: file,
|
2016-09-05 16:37:15 +08:00
|
|
|
}
|
2017-09-13 22:00:33 +08:00
|
|
|
f.ContentTypes = f.contentTypesReader()
|
|
|
|
f.Styles = f.stylesReader()
|
|
|
|
f.WorkBook = f.workbookReader()
|
|
|
|
f.WorkBookRels = f.workbookRelsReader()
|
|
|
|
f.Sheet["xl/worksheets/sheet1.xml"] = f.workSheetReader("Sheet1")
|
|
|
|
f.sheetMap["Sheet1"] = "xl/worksheets/sheet1.xml"
|
|
|
|
return f
|
2016-09-05 16:37:15 +08:00
|
|
|
}
|
|
|
|
|
2017-01-24 18:29:02 +08:00
|
|
|
// Save provides function to override the xlsx file 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 == "" {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-06-28 17:03:20 +08:00
|
|
|
// SaveAs provides function to create or update to an xlsx file at the provided
|
2017-01-18 16:05:01 +08:00
|
|
|
// path.
|
2017-06-28 17:03:20 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write provides function to write to an io.Writer.
|
|
|
|
func (f *File) Write(w io.Writer) 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)
|
2017-04-01 13:56:39 +08:00
|
|
|
f.contentTypesWriter()
|
|
|
|
f.workbookWriter()
|
|
|
|
f.workbookRelsWriter()
|
|
|
|
f.worksheetWriter()
|
2017-06-26 18:44:19 +08:00
|
|
|
f.styleSheetWriter()
|
2016-09-05 16:37:15 +08:00
|
|
|
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 {
|
2016-09-02 10:28:29 +08:00
|
|
|
return err
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|
2017-02-17 02:25:55 +08:00
|
|
|
_, err = fi.Write([]byte(content))
|
2016-08-30 11:51:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-02-17 02:25:55 +08:00
|
|
|
err := zw.Close()
|
2016-08-30 11:51:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-17 02:25:55 +08:00
|
|
|
|
|
|
|
if _, err := buf.WriteTo(w); err != nil {
|
2016-08-30 11:51:31 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-02-17 02:25:55 +08:00
|
|
|
|
|
|
|
return nil
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|