package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
)
//读取excel文件,返回行列数组
func ReadExcel(filename string) []map[string]string {
ret := []map[string]string{}
f, err := excelize.OpenFile(filename)
if err != nil {
fmt.Println("读取excel文件出错", err.Error())
return ret
}
sheets := f.GetSheetMap()
fmt.Println(sheets)
sheet1 := sheets[1]
fmt.Println("第一个工作表", sheet1)
rows := f.GetRows(sheet1)
cols := []string{}
for i, row := range rows {
if i == 0 { //取得第一行的所有数据---execel表头
for _, colCell := range row {
cols = append(cols, colCell)
}
fmt.Println("列信息", cols)
} else {
theRow := map[string]string{}
for j, colCell := range row {
k := cols[j]
theRow[k] = colCell
}
ret = append(ret, theRow)
}
}
return ret
}
func main() {
//excel := ReadExcel("测试excel.xlsx")
excel := ReadExcel("sx1test.xlsx")
fmt.Println(excel)
/*查询结果:
[map[flag:983xwgL6u03G device编号:0200581314000002 name: 三层东南角电梯厅]
map[flag:983xwgL6u03t device编号:0200581314000010 name:三层西南角电梯厅]
map[flag:j4GgL8Z1Jk3k device编号:0100581314000032 name:二层西走廊南]
map[flag:j4GgL8Z1Jd3s device编号:0100581314000040 name:二层南走廊东]
]
*/
}