Browse Source

Merge pull request #67 from M1rt/master

Write File to io.Writer
Geoffrey J. Teale 11 years ago
parent
commit
0e578dc594
1 changed files with 18 additions and 9 deletions
  1. 18 9
      file.go

+ 18 - 9
file.go

@@ -64,21 +64,32 @@ func FileToSlice(path string) ([][][]string, error) {
 
 // Save the File to an xlsx file at the provided path.
 func (f *File) Save(path string) (err error) {
-	var parts map[string]string
 	var target *os.File
-	var zipWriter *zip.Writer
 
-	parts, err = f.MarshallParts()
+	target, err = os.Create(path)
 	if err != nil {
 		return
 	}
 
-	target, err = os.Create(path)
+	err = f.Write(target)
+	if err != nil {
+		return
+	}
+
+	return target.Close()
+}
+
+// Write the File to io.Writer as xlsx
+func (f *File) Write(writer io.Writer) (err error) {
+	var parts map[string]string
+	var zipWriter *zip.Writer
+
+	parts, err = f.MarshallParts()
 	if err != nil {
 		return
 	}
 
-	zipWriter = zip.NewWriter(target)
+	zipWriter = zip.NewWriter(writer)
 
 	for partName, part := range parts {
 		var writer io.Writer
@@ -91,12 +102,10 @@ func (f *File) Save(path string) (err error) {
 			return
 		}
 	}
+
 	err = zipWriter.Close()
-	if err != nil {
-		return
-	}
 
-	return target.Close()
+	return
 }
 
 // Add a new Sheet, with the provided name, to a File