golang GUI图形界面框架fyne例子 excel数据汇总小工具
// Copyright 2016 - 2021 The fyne-excelize Authors @https://gitee.com/y2h. All rights reserved. Use of
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package main
import (
"path/filepath"
"strconv"
"strings"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
"github.com/xuri/excelize/v2"
)
func main() {
ap := app.NewWithID("y2h.excel.transport")
ap.Settings().SetTheme(&myTheme{})
w := ap.NewWindow("Golang GUI 框架fyne 学习: excel data transport")
w.Resize(fyne.NewSize(600, 600))
dataList := binding.NewStringList()
lst := BtnLblList(dataList)
dataList2 := binding.NewStringList()
lst2 := BtnLblList(dataList2)
lbl1 := widget.NewLabel("lbl1")
btn1 := widget.NewButton("do", nil)
f1 := binding.NewString()
f1.AddListener(binding.NewDataListener(func() {
s, e := f1.Get()
if e != nil {
btn1.Disable()
return
}
if s == "" {
btn1.Disable()
return
}
btn1.Enable()
}))
wf1 := NewOpenFile(f1, w)
f2 := binding.NewString()
wf2 := NewOpenFile(f2, w)
var topIn *fyne.Container
var isFolder bool
chk1 := widget.NewCheck("IsFolder", func(b bool) {
isFolder = b
if b {
topIn.Objects[0] = NewOpenFolder(f1, w)
} else {
topIn.Objects[0] = wf1
}
})
chk1.SetChecked(false)
topIn = container.NewBorder(nil, nil, widget.NewLabel(" In"), nil, wf1)
topOut := container.NewBorder(nil, nil, widget.NewLabel("Out"), nil, wf2)
col := widget.NewSelectEntry(Alphabics())
row := widget.NewSelectEntry(Rows(30))
leftBtnAdd := widget.NewButtonWithIcon("In", theme.ContentAddIcon(), func() {
Add2List(dataList, col.Text, row.Text)
})
rightBtnAdd := widget.NewButtonWithIcon("Out", theme.ContentAddIcon(), func() {
Add2List(dataList2, col.Text, row.Text)
})
btn1.OnTapped = func() {
inS, _ := dataList.Get()
outS, _ := dataList2.Get()
f1path, _ := f1.Get()
f2path, _ := f2.Get()
if len(inS) < 1 || len(outS)<1{
lbl1.SetText("nothing to do")
return
}
fb, _ := excelize.OpenFile(f2path)
sheet1 := "Sheet1"
rows, _ := fb.GetRows(sheet1)
maxRow := len(rows) + 1
if isFolder {
for _, file := range GetFileList(f1path) {
if filepath.Ext(file) != ".xlsx" {
lbl1.SetText("something wrong with the filepath")
return
}
f, _ := excelize.OpenFile(file)
for i, v := range inS {
cellIn := strings.Join(strings.Split(v, "-"), "")
b3, _ := f.GetCellValue(sheet1, cellIn)
colOut := strings.Split(outS[i], "-")[0]
fb.SetCellValue(sheet1, colOut+strconv.Itoa(maxRow), b3)
}
maxRow++
}
} else {
if filepath.Ext(f1path) != ".xlsx" {
lbl1.SetText("something wrong with the filepath")
return
}
f, err := excelize.OpenFile(f1path)
if err != nil {
lbl1.SetText("file open error")
return
}
sheetList := f.GetSheetList()
for _, sheet := range sheetList {
for i, v := range inS {
cellIn := strings.Join(strings.Split(v, "-"), "")
b3, _ := f.GetCellValue(sheet, cellIn)
colOut := strings.Split(outS[i], "-")[0]
fb.SetCellValue(sheet1, colOut+strconv.Itoa(maxRow), b3)
}
maxRow++
}
}
fb.Save()
lbl1.SetText("done")
}
btnRefresh:=widget.NewButtonWithIcon("",theme.ViewRefreshIcon(), func(){
dataList.Set([]string{})
dataList2.Set([]string{})
})
top := container.NewVBox(topIn, topOut, container.NewHBox(
chk1, leftBtnAdd, col, row, rightBtnAdd,btnRefresh,
), btn1)
c := container.NewBorder(top, lbl1, nil, nil, container.NewHBox(lst, lst2))
// w.SetContent(c)
tabs := container.NewAppTabs(
container.NewTabItem("Main", c),
container.NewTabItem("Support", support()),
)
tabs.SetTabLocation(container.TabLocationBottom)
w.SetContent(tabs)
w.CenterOnScreen()
w.SetMaster()
w.ShowAndRun()
}