This repository has been archived by the owner on Jun 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
workbook.go
58 lines (46 loc) · 1.2 KB
/
workbook.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package goxlsxwriter
import (
"errors"
)
/*
#cgo LDFLAGS: -L. -lxlsxwriter
#include <xlsxwriter.h>
*/
import "C"
import "unsafe"
// Workbook represents an Excel workbook.
type Workbook struct {
CWorkbook *C.struct_lxw_workbook
}
// WorkbookOptions contains options to be set when creating a new Workbook.
type WorkbookOptions struct {
ConstantMemory int
TmpDir string
}
// NewWorkbook create and returns a new instance of Workbook.
func NewWorkbook(filename string, options *WorkbookOptions) *Workbook {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
var cOptions *C.lxw_workbook_options
if options != nil {
cTmpDir := C.CString(options.TmpDir)
defer C.free(unsafe.Pointer(cTmpDir))
cOptions = &C.lxw_workbook_options{
constant_memory: (C.uint8_t)(options.ConstantMemory),
tmpdir: cTmpDir,
}
}
cWorkbook := C.new_workbook_opt(cFilename, cOptions)
workbook := &Workbook{
CWorkbook: cWorkbook,
}
return workbook
}
// Close closes the workbook and writes the XLSX file to disk.
func (w *Workbook) Close() error {
err := C.workbook_close(w.CWorkbook)
if err != C.LXW_NO_ERROR {
return errors.New(C.GoString(C.lxw_strerror(err)))
}
return nil
}