forked from go-ole/go-ole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel.go
96 lines (81 loc) · 2.86 KB
/
excel.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// +build windows
package main
import (
"fmt"
"log"
"os"
ole "github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
func writeExample(excel, workbooks *ole.IDispatch, filepath string) {
// ref: https://msdn.microsoft.com/zh-tw/library/office/ff198017.aspx
// http://stackoverflow.com/questions/12159513/what-is-the-correct-xlfileformat-enumeration-for-excel-97-2003
const xlExcel8 = 56
workbook := oleutil.MustCallMethod(workbooks, "Add", nil).ToIDispatch()
defer workbook.Release()
worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch()
defer worksheet.Release()
cell := oleutil.MustGetProperty(worksheet, "Cells", 1, 1).ToIDispatch()
oleutil.PutProperty(cell, "Value", 12345)
cell.Release()
activeWorkBook := oleutil.MustGetProperty(excel, "ActiveWorkBook").ToIDispatch()
defer activeWorkBook.Release()
os.Remove(filepath)
// ref: https://msdn.microsoft.com/zh-tw/library/microsoft.office.tools.excel.workbook.saveas.aspx
oleutil.MustCallMethod(activeWorkBook, "SaveAs", filepath, xlExcel8, nil, nil).ToIDispatch()
//time.Sleep(2 * time.Second)
// let excel could close without asking
// oleutil.PutProperty(workbook, "Saved", true)
// oleutil.CallMethod(workbook, "Close", false)
}
func readExample(fileName string, excel, workbooks *ole.IDispatch) {
workbook, err := oleutil.CallMethod(workbooks, "Open", fileName)
if err != nil {
log.Fatalln(err)
}
defer workbook.ToIDispatch().Release()
sheets := oleutil.MustGetProperty(excel, "Sheets").ToIDispatch()
sheetCount := (int)(oleutil.MustGetProperty(sheets, "Count").Val)
fmt.Println("sheet count=", sheetCount)
sheets.Release()
worksheet := oleutil.MustGetProperty(workbook.ToIDispatch(), "Worksheets", 1).ToIDispatch()
defer worksheet.Release()
for row := 1; row <= 2; row++ {
for col := 1; col <= 5; col++ {
cell := oleutil.MustGetProperty(worksheet, "Cells", row, col).ToIDispatch()
val, err := oleutil.GetProperty(cell, "Value")
if err != nil {
break
}
fmt.Printf("(%d,%d)=%+v toString=%s\n", col, row, val.Value(), val.ToString())
cell.Release()
}
}
}
func showMethodsAndProperties(i *ole.IDispatch) {
n, err := i.GetTypeInfoCount()
if err != nil {
log.Fatalln(err)
}
tinfo, err := i.GetTypeInfo()
if err != nil {
log.Fatalln(err)
}
fmt.Println("n=", n, "tinfo=", tinfo)
}
func main() {
log.SetFlags(log.Flags() | log.Lshortfile)
ole.CoInitialize(0)
unknown, _ := oleutil.CreateObject("Excel.Application")
excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
oleutil.PutProperty(excel, "Visible", true)
workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
cwd, _ := os.Getwd()
writeExample(excel, workbooks, cwd+"\\write.xls")
readExample(cwd+"\\excel97-2003.xls", excel, workbooks)
showMethodsAndProperties(workbooks)
workbooks.Release()
// oleutil.CallMethod(excel, "Quit")
excel.Release()
ole.CoUninitialize()
}