GO·NOTE

一份 Go 开发工程师的学习笔记

0%

github-tealeg-xlsx-读取excel文件

项目功能

对 excel 文件进行解析,生成。

项目地址

github.com/tealeg/xlsx

项目demo

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
package main

import (
"fmt"
"github.com/tealeg/xlsx"
)

func main() {
excelFileName := "1.xlsx"
xlFile, err := xlsx.OpenFile(excelFileName)
if err != nil {
panic(err)
}
for s, sheet := range xlFile.Sheets {
if s == 1 {
break
}
for _, row := range sheet.Rows {
for j, cell := range row.Cells {
if j == 0 {
fmt.Printf("\n")
}
switch cell.Type() {
case xlsx.CellTypeString:
fmt.Printf("str %d %s\t", j, cell.String())
break
case xlsx.CellTypeStringFormula:
fmt.Printf("formula %d %s\t", j, cell.Formula())
break
case xlsx.CellTypeNumeric:
x, _:= cell.Int64()
fmt.Printf("int %d %d\t", j, x)
break
case xlsx.CellTypeBool:
fmt.Printf("bool %d %v\t", j, cell.Bool())
break
case xlsx.CellTypeDate:
t, _ := cell.GetTime(false)
fmt.Printf("date %d %v\t", j, t)
break
}
}
}
}
}