From c191c48b3ee96a80513a54219bf1deb58e1d1cff Mon Sep 17 00:00:00 2001 From: lusiyi Date: Thu, 10 Nov 2022 16:07:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0go=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/go-mod/excel.go | 48 +++++++++++++++++++++++++++++++++++++++ src/main/go-mod/func.go | 24 ++++++++++++++++++++ src/main/go-mod/struct.go | 7 ++++++ 3 files changed, 79 insertions(+) create mode 100644 src/main/go-mod/excel.go create mode 100644 src/main/go-mod/func.go create mode 100644 src/main/go-mod/struct.go diff --git a/src/main/go-mod/excel.go b/src/main/go-mod/excel.go new file mode 100644 index 0000000..1bfb4a0 --- /dev/null +++ b/src/main/go-mod/excel.go @@ -0,0 +1,48 @@ +package excel + +import ( + "fmt" + "reflect" + + "github.com/360EntSecGroup-Skylar/excelize" +) + +// WriteXlsx 生成表格到本地服务器 +func WriteXlsx(sheet string, records interface{}) *excelize.File { + f := excelize.NewFile() // new file + index := f.NewSheet(sheet) // new sheet + f.SetActiveSheet(index) // set active (default) sheet + firstCharacter := 65 // start from 'A' line + t := reflect.TypeOf(records) + + if t.Kind() != reflect.Slice { + return f + } + + s := reflect.ValueOf(records) + for i := 0; i < s.Len(); i++ { + elem := s.Index(i).Interface() + elemType := reflect.TypeOf(elem) + elemValue := reflect.ValueOf(elem) + for j := 0; j < elemType.NumField(); j++ { + field := elemType.Field(j) + // 结构体中xlsx 类似json + tag := field.Tag.Get("xlsx") + name := tag + column := string(firstCharacter + j) + if tag == "" { + continue + } + // 设置表头 + if i == 0 { + f.SetCellValue(sheet, fmt.Sprintf("%s%d", column, i+1), name) + } + // 设置列宽 + f.SetColWidth(sheet, "A", fmt.Sprintf("%s", column), 20) + + // 设置内容 + f.SetCellValue(sheet, fmt.Sprintf("%s%d", column, i+2), elemValue.Field(j).Interface()) + } + } + return f +} diff --git a/src/main/go-mod/func.go b/src/main/go-mod/func.go new file mode 100644 index 0000000..d88120a --- /dev/null +++ b/src/main/go-mod/func.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +type Publisher struct { + name string + address string +} +type Book struct { + title, name string + publisher Publisher +} + +func main() { + var book = Book{"title","name",Publisher{"pub","beijing"}} + /* 这是我的第一个简单的程序 */ + fmt.Println("Hello, World!") + fmt.Println(book) + +} + +func test (book Book) (b1 Book, b2 Book){ + return book,book +} \ No newline at end of file diff --git a/src/main/go-mod/struct.go b/src/main/go-mod/struct.go new file mode 100644 index 0000000..1de4533 --- /dev/null +++ b/src/main/go-mod/struct.go @@ -0,0 +1,7 @@ +package test +type Books struct { + title string + author string + subject string + book_id int +}