接上期:
https://www.toutiao.com/i6932329449341862412/?group_id=6932329449341862412
上期只完成了一个walk窗体,这次再增加excel表格读取。
第1步:引用go get github.com/360EntSecGroup-Skylar/excelize/v2
excelize是一个强大的读写excel的第三方库,下面将使用它来读写excel
package main
import (
"fmt"
"strconv"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
func main() {
var TextEdit1 *walk.TextEdit //声明2个标准win控件
var PushButton1 *walk.PushButton
MainWindow{
Title: "探索excel(GoLang版)",
Size: Size{500, 500},
Icon: "sun.ico", //添加图标到窗体标题栏
Layout: VBox{}, //纵向布局,包含PushButton1和TextEdit1
Children: []Widget{
PushButton{
Text: "一键执行",
AssignTo: &PushButton1,
OnClicked: func() {
TextEdit_click(TextEdit1)
},
},
TextEdit{AssignTo: &TextEdit1},
},
}.Run()
}
func TextEdit_click(a *walk.TextEdit) {
f, err := excelize.OpenFile("测试工作簿1.xlsx")
if err != nil {
fmt.Println(err)
return
}
a.SetTextColor(walk.RGB(byte(255), byte(48), byte(48)))
var sheetlist = f.GetSheetList()
for i := 0; i < len(sheetlist); i {
a.AppendText(strconv.Itoa(i) " " sheetlist[i] "\n")
}
a.AppendText("欢迎关注“探索3000”")
}
效果图:
主框架代码和上期完全相同,
f, err := excelize.OpenFile("测试工作簿1.xlsx")
打开xlsx文件,f.GetSheetList()获取sheet列表名,这是个字符串数组,不过它的序号从0开始,有VBA或者VBNET基础的同学千万要注意这个差别。
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved