-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiofactory.go
More file actions
33 lines (30 loc) · 829 Bytes
/
iofactory.go
File metadata and controls
33 lines (30 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package gospreadsheet
import (
"fmt"
"path/filepath"
"strings"
)
// OpenFile opens a spreadsheet file, auto-detecting the format by extension.
func OpenFile(filename string) (*Workbook, error) {
ext := strings.ToLower(filepath.Ext(filename))
switch ext {
case ".xlsx":
return NewXLSXReader().Open(filename)
case ".csv":
return NewCSVReader().Open(filename)
default:
return nil, fmt.Errorf("unsupported file format: %s", ext)
}
}
// SaveFile saves a workbook to a file, auto-detecting the format by extension.
func SaveFile(wb *Workbook, filename string) error {
ext := strings.ToLower(filepath.Ext(filename))
switch ext {
case ".xlsx":
return NewXLSXWriter().Save(wb, filename)
case ".csv":
return NewCSVWriter().Save(wb, filename)
default:
return fmt.Errorf("unsupported file format: %s", ext)
}
}