You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
excelize-formula/file.go

79 lines
1.7 KiB

9 years ago
package excelize
import (
"archive/zip"
"bytes"
"os"
)
// CreateFile provide function to create new file by default template.
9 years ago
// For example:
// xlsx := CreateFile()
func CreateFile() *File {
file := make(map[string]string)
file["_rels/.rels"] = templateRels
file["docProps/app.xml"] = templateDocpropsApp
file["docProps/core.xml"] = templateDocpropsCore
file["xl/_rels/workbook.xml.rels"] = templateWorkbookRels
file["xl/theme/theme1.xml"] = templateTheme
file["xl/worksheets/sheet1.xml"] = templateSheet
file["xl/styles.xml"] = templateStyles
file["xl/workbook.xml"] = templateWorkbook
file["[Content_Types].xml"] = templateContentTypes
return &File{
XLSX: file,
}
}
// Save provide function override the xlsx file with origin path.
func (f *File) Save() error {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
for path, content := range f.XLSX {
f, err := w.Create(path)
if err != nil {
return err
}
_, err = f.Write([]byte(content))
if err != nil {
return err
}
}
err := w.Close()
if err != nil {
return err
}
file, err := os.OpenFile(f.Path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return err
}
buf.WriteTo(file)
return err
9 years ago
}
// WriteTo provide function create or update to an xlsx file at the provided path.
func (f *File) WriteTo(name string) error {
9 years ago
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
for path, content := range f.XLSX {
f, err := w.Create(path)
9 years ago
if err != nil {
return err
9 years ago
}
_, err = f.Write([]byte(content))
9 years ago
if err != nil {
return err
}
}
err := w.Close()
if err != nil {
return err
}
file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
9 years ago
if err != nil {
return err
}
buf.WriteTo(file)
9 years ago
return err
}