diff options
Diffstat (limited to 'file.go')
-rw-r--r-- | file.go | 31 |
1 files changed, 22 insertions, 9 deletions
@@ -1,3 +1,12 @@ +// Copyright 2016 - 2018 The excelize Authors. All rights reserved. Use of +// 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.8 or later. + package excelize import ( @@ -43,7 +52,7 @@ func NewFile() *File { // Save provides a function to override the xlsx file with origin path. func (f *File) Save() error { if f.Path == "" { - return fmt.Errorf("No path defined for file, consider File.WriteTo or File.Write") + return fmt.Errorf("no path defined for file, consider File.WriteTo or File.Write") } return f.SaveAs(f.Path) } @@ -67,6 +76,15 @@ func (f *File) Write(w io.Writer) error { // 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) { buf := new(bytes.Buffer) zw := zip.NewWriter(buf) f.contentTypesWriter() @@ -77,17 +95,12 @@ func (f *File) WriteTo(w io.Writer) (int64, error) { for path, content := range f.XLSX { fi, err := zw.Create(path) if err != nil { - return 0, err + return buf, err } _, err = fi.Write(content) if err != nil { - return 0, err + return buf, err } } - err := zw.Close() - if err != nil { - return 0, err - } - - return buf.WriteTo(w) + return buf, zw.Close() } |