Browse Source

增加go模块

master
lusiyi 2 years ago
parent
commit
c191c48b3e
3 changed files with 79 additions and 0 deletions
  1. +48
    -0
      src/main/go-mod/excel.go
  2. +24
    -0
      src/main/go-mod/func.go
  3. +7
    -0
      src/main/go-mod/struct.go

+ 48
- 0
src/main/go-mod/excel.go View File

@@ -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
}

+ 24
- 0
src/main/go-mod/func.go View File

@@ -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
}

+ 7
- 0
src/main/go-mod/struct.go View File

@@ -0,0 +1,7 @@
package test
type Books struct {
title string
author string
subject string
book_id int
}

Loading…
Cancel
Save