Documentation
¶
Index ¶
- func AxisToCellRow(axis string) (cell string, row int)
- func ExcelSheet1ToList(excel *excelize.File, listPtr interface{}) (err error)
- func ExcelSheet1ToListFromPath(path string, listPtr interface{}) (err error)
- func GetCellCode(columnNumber int) string
- func ListToExcelSheet1(list interface{}) (*excelize.File, error)
- func ListToExcelSheet1Base(list interface{}, rowStyle func(row int) (style string), ...) (*excelize.File, error)
- func ListToExcelSheet1BaseToBytes(list interface{}, rowStyle func(row int) (style string), ...) ([]byte, error)
- func ListToExcelSheet1ToBytes(list interface{}) ([]byte, error)
- func NewExcelSheet1ToList(excel *excelize.File, model interface{}) (e *excelToList, err error)
- func NewExcelSheet1ToListFromPath(path string, model interface{}) (e *excelToList, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AxisToCellRow ¶
AxisToCellRow Axis转换成列号和行号
func ExcelSheet1ToListFromPath ¶
ExcelSheet1ToListFromPath path路径excel文件数据转换成 listPtr 结构数据 切片元素export数据必须与
excel中表头和结构一致
Example ¶
package main
import (
"fmt"
"github.com/freefishgo/goexcel"
)
func main() {
type s1 struct {
Name string `json:"name" export:"一级姓名|姓名2,3"`
Age int32 `json:"age" export:"年龄2,6"`
Time string `json:"time" export:"时间2,9"`
}
type s struct {
Name string `json:"name" export:"一级姓名|姓名1,2" headStyle:"{\"fill\":{\"type\":\"pattern\",\"color\":[\"#E0EBF5\"],\"pattern\":1}}"`
Age int32 `json:"age" export:"年龄1,5" cellStyle:"{\"fill\":{\"type\":\"gradient\",\"color\":[\"#FFFFFF\",\"#E0EBF5\"],\"shading\":1}}"`
Time string `json:"time" export:"时间1,8"`
List []s1
}
type sut struct {
A int32 `json:"a"`
B string `json:"b"`
}
type p struct {
Name string `json:"name" export:"一级姓名|二级姓名|姓名,1"`
Age int32 `json:"age" export:"年龄,4"`
Time string `json:"time" export:"时间,7" headStyle:"{\"font\":{\"bold\":true,\"italic\":true,\"family\":\"Berlin Sans FB Demi\",\"size\":36,\"color\":\"#777777\"}}"`
List []s
Export *sut `json:"export" export:"结构体,10"`
}
var list []*p
goexcel.ExcelSheet1ToListFromPath("20210814182854.xlsx", &list)
fmt.Println(len(list))
for _, d := range list {
fmt.Println(fmt.Sprintf("%+v", d))
}
}
Output:
func ListToExcelSheet1 ¶
ListToExcelSheet1 通过list数据来生成数据格式 通过结构体 tag export:"一级姓名|姓名1,2"
生成表头和数据格式 通过,来分割表头名和列位置 表头名通过 |来判断层级
数据级转换成 excelize.File
Example ¶
package main
import (
"fmt"
"github.com/freefishgo/goexcel"
"time"
)
func main() {
type s1 struct {
Name string `json:"name" export:"一级姓名|姓名2,3"`
Age int32 `json:"age" export:"年龄2,6"`
Time string `json:"time" export:"时间2,9"`
}
type s struct {
Name string `json:"name" export:"一级姓名|姓名1,2" headStyle:"{\"fill\":{\"type\":\"pattern\",\"color\":[\"#E0EBF5\"],\"pattern\":1}}"`
Age int32 `json:"age" export:"年龄1,5" cellStyle:"{\"fill\":{\"type\":\"gradient\",\"color\":[\"#FFFFFF\",\"#E0EBF5\"],\"shading\":1}}"`
Time string `json:"time" export:"时间1,8"`
List []s1
}
type p struct {
Name string `json:"name" export:"一级姓名|二级姓名|姓名,1"`
Age int32 `json:"age" export:"年龄,4"`
Time string `json:"time" export:"时间,7" headStyle:"{\"font\":{\"bold\":true,\"italic\":true,\"family\":\"Berlin Sans FB Demi\",\"size\":36,\"color\":\"#777777\"}}"`
List []s
}
v := &p{
Name: "天外飞仙",
Age: 18,
Time: "我是时间",
List: []s{
{
Name: "大名",
Age: 19,
Time: "我是大名时间",
},
{
Name: "小名",
Age: 20,
},
},
}
list := append([]*p(nil), &p{
Name: "天外飞仙",
Age: 16,
Time: "我是开始时间",
List: []s{
{
Name: "小名",
Age: 20,
Time: "我是小名时间",
},
{
Name: "小名",
Age: 21,
Time: "我是小名名时间2",
},
{
Name: "小名",
Age: 21,
Time: "我是小名名时间2",
},
},
}, v, v, v, v, v, v, v, v, v, v)
xlsx, err := goexcel.ListToExcelSheet1(list)
if err != nil {
fmt.Println(err.Error())
return
}
err = xlsx.SaveAs(time.Now().Format("20060102150405") + ".xlsx")
}
Output:
func ListToExcelSheet1Base ¶
func ListToExcelSheet1Base(list interface{}, rowStyle func(row int) (style string), cellDo func(cell int, value interface{}) (style string, newValue interface{})) (*excelize.File, error)
ListToExcelSheet1Base 通过list数据来生成数据格式 通过结构体 tag export:"一级姓名|姓名1,2"
生成表头和数据格式 通过,来分割表头名和列位置 表头名通过 |来判断层级
rowStyle 对list行处理 cellDo 单元格处理
Example ¶
package main
import (
"fmt"
"github.com/freefishgo/goexcel"
"time"
)
func main() {
type s1 struct {
Name string `json:"name" export:"一级姓名|姓名2,3"`
Age int32 `json:"age" export:"年龄2,6"`
Time string `json:"time" export:"时间2,9"`
}
type s struct {
Name string `json:"name" export:"一级姓名|姓名1,2" headStyle:"{\"fill\":{\"type\":\"pattern\",\"color\":[\"#E0EBF5\"],\"pattern\":1}}"`
Age int32 `json:"age" export:"年龄1,5" cellStyle:"{\"fill\":{\"type\":\"gradient\",\"color\":[\"#FFFFFF\",\"#E0EBF5\"],\"shading\":1}}"`
Time string `json:"time" export:"时间1,8"`
List []s1
}
type p struct {
Name string `json:"name" export:"一级姓名|二级姓名|姓名,1"`
Age int32 `json:"age" export:"年龄,4"`
Time string `json:"time" export:"时间,7" headStyle:"{\"font\":{\"bold\":true,\"italic\":true,\"family\":\"Berlin Sans FB Demi\",\"size\":36,\"color\":\"#777777\"}}"`
List []s
}
v := &p{
Name: "天外飞仙",
Age: 18,
Time: "我是时间",
List: []s{
{
Name: "大名",
Age: 19,
Time: "我是大名时间",
List: []s1{
{
Name: "大名",
Age: 19,
Time: "我是大名时间",
},
},
},
{
Name: "小名",
Age: 20,
},
},
}
list := append([]*p(nil), &p{
Name: "天外飞仙",
Age: 16,
Time: "我是开始时间",
List: []s{
{
Name: "小名",
Age: 20,
Time: "我是小名时间",
List: []s1{
{
Name: "大名",
Age: 19,
Time: "我是大名时间",
},
{
Name: "大名",
Age: 19,
Time: "我是大名时间",
},
},
},
{
Name: "小名",
Age: 21,
Time: "我是小名名时间2",
},
{
Name: "小名",
Age: 21,
Time: "我是小名名时间2",
},
},
}, v, v, v, v, v, v, v, v, v, v)
rows := func(row int) (style string) {
if row%2 == 0 {
return `{"fill":{"type":"pattern","color":["RED"],"pattern":1}}`
}
return ""
}
cell := func(cell int, value interface{}) (style string, newValue interface{}) {
if cell == 9 {
return `{"fill":{"type":"pattern","color":["RED"],"pattern":1}}`, value
}
return style, value
}
xlsx, err := goexcel.ListToExcelSheet1Base(list, rows, cell)
if err != nil {
fmt.Println(err.Error())
return
}
err = xlsx.SaveAs(time.Now().Format("20060102150405") + ".xlsx")
}
Output:
func ListToExcelSheet1BaseToBytes ¶
func ListToExcelSheet1BaseToBytes(list interface{}, rowStyle func(row int) (style string), cellDo func(cell int, value interface{}) (style string, newValue interface{})) ([]byte, error)
ListToExcelSheet1BaseToBytes 通过list数据来生成数据格式 通过结构体 tag export:"一级姓名|姓名1,2"
生成表头和数据格式 通过,来分割表头名和列位置 表头名通过 |来判断层级
rowStyle 对list行处理 cellDo 单元格处理
Example ¶
package main
import (
"github.com/freefishgo/goexcel"
)
func main() {
type s1 struct {
Name string `json:"name" export:"一级姓名|姓名2,3"`
Age int32 `json:"age" export:"年龄2,6"`
Time string `json:"time" export:"时间2,9"`
}
type s struct {
Name string `json:"name" export:"一级姓名|姓名1,2" headStyle:"{\"fill\":{\"type\":\"pattern\",\"color\":[\"#E0EBF5\"],\"pattern\":1}}"`
Age int32 `json:"age" export:"年龄1,5" cellStyle:"{\"fill\":{\"type\":\"gradient\",\"color\":[\"#FFFFFF\",\"#E0EBF5\"],\"shading\":1}}"`
Time string `json:"time" export:"时间1,8"`
List []s1
}
type p struct {
Name string `json:"name" export:"一级姓名|二级姓名|姓名,1"`
Age int32 `json:"age" export:"年龄,4"`
Time string `json:"time" export:"时间,7" headStyle:"{\"font\":{\"bold\":true,\"italic\":true,\"family\":\"Berlin Sans FB Demi\",\"size\":36,\"color\":\"#777777\"}}"`
List []s
}
v := &p{
Name: "天外飞仙",
Age: 18,
Time: "我是时间",
List: []s{
{
Name: "大名",
Age: 19,
Time: "我是大名时间",
List: []s1{
{
Name: "大名",
Age: 19,
Time: "我是大名时间",
},
},
},
{
Name: "小名",
Age: 20,
},
},
}
list := append([]*p(nil), &p{
Name: "天外飞仙",
Age: 16,
Time: "我是开始时间",
List: []s{
{
Name: "小名",
Age: 20,
Time: "我是小名时间",
List: []s1{
{
Name: "大名",
Age: 19,
Time: "我是大名时间",
},
{
Name: "大名",
Age: 19,
Time: "我是大名时间",
},
},
},
{
Name: "小名",
Age: 21,
Time: "我是小名名时间2",
},
{
Name: "小名",
Age: 21,
Time: "我是小名名时间2",
},
},
}, v, v, v, v, v, v, v, v, v, v)
rows := func(row int) (style string) {
if row%2 == 0 {
return `{"fill":{"type":"pattern","color":["RED"],"pattern":1}}`
}
return ""
}
cell := func(cell int, value interface{}) (style string, newValue interface{}) {
if cell == 9 {
return `{"fill":{"type":"pattern","color":["RED"],"pattern":1}}`, value
}
return style, value
}
goexcel.ListToExcelSheet1BaseToBytes(list, rows, cell)
}
Output:
func ListToExcelSheet1ToBytes ¶
ListToExcelSheet1ToBytes 通过list数据来生成数据格式 通过结构体 tag export:"一级姓名|姓名1,2"
生成表头和数据格式 通过,来分割表头名和列位置 表头名通过 |来判断层级
数据级转换成excel[]Byte数组
Example ¶
package main
import (
"github.com/freefishgo/goexcel"
)
func main() {
type s1 struct {
Name string `json:"name" export:"一级姓名|姓名2,3"`
Age int32 `json:"age" export:"年龄2,6"`
Time string `json:"time" export:"时间2,9"`
}
type s struct {
Name string `json:"name" export:"一级姓名|姓名1,2" headStyle:"{\"fill\":{\"type\":\"pattern\",\"color\":[\"#E0EBF5\"],\"pattern\":1}}"`
Age int32 `json:"age" export:"年龄1,5" cellStyle:"{\"fill\":{\"type\":\"gradient\",\"color\":[\"#FFFFFF\",\"#E0EBF5\"],\"shading\":1}}"`
Time string `json:"time" export:"时间1,8"`
List []s1
}
type p struct {
Name string `json:"name" export:"一级姓名|二级姓名|姓名,1"`
Age int32 `json:"age" export:"年龄,4"`
Time string `json:"time" export:"时间,7" headStyle:"{\"font\":{\"bold\":true,\"italic\":true,\"family\":\"Berlin Sans FB Demi\",\"size\":36,\"color\":\"#777777\"}}"`
List []s
}
v := &p{
Name: "天外飞仙",
Age: 18,
Time: "我是时间",
List: []s{
{
Name: "大名",
Age: 19,
Time: "我是大名时间",
},
{
Name: "小名",
Age: 20,
},
},
}
list := append([]*p(nil), &p{
Name: "天外飞仙",
Age: 16,
Time: "我是开始时间",
List: []s{
{
Name: "小名",
Age: 20,
Time: "我是小名时间",
},
{
Name: "小名",
Age: 21,
Time: "我是小名名时间2",
},
{
Name: "小名",
Age: 21,
Time: "我是小名名时间2",
},
},
}, v, v, v, v, v, v, v, v, v, v)
goexcel.ListToExcelSheet1ToBytes(list)
}
Output:
func NewExcelSheet1ToList ¶
func NewExcelSheet1ToListFromPath ¶
NewExcelSheet1ToListFromPath path路径excel文件数据转换成 e 结构数据 切片元素export数据必须与
excel中表头和结构一致
Example ¶
package main
import (
"fmt"
"github.com/freefishgo/goexcel"
)
func main() {
type s1 struct {
Name string `json:"name" export:"一级姓名|姓名2,3"`
Age int32 `json:"age" export:"年龄2,6"`
Time string `json:"time" export:"时间2,9"`
}
type s struct {
Name string `json:"name" export:"一级姓名|姓名1,2" headStyle:"{\"fill\":{\"type\":\"pattern\",\"color\":[\"#E0EBF5\"],\"pattern\":1}}"`
Age int32 `json:"age" export:"年龄1,5" cellStyle:"{\"fill\":{\"type\":\"gradient\",\"color\":[\"#FFFFFF\",\"#E0EBF5\"],\"shading\":1}}"`
Time string `json:"time" export:"时间1,8"`
List []s1
}
type sut struct {
A int32 `json:"a"`
B string `json:"b"`
}
type p struct {
Name string `json:"name" export:"一级姓名|二级姓名|姓名,1"`
Age int32 `json:"age" export:"年龄,4"`
Time string `json:"time" export:"时间,7" headStyle:"{\"font\":{\"bold\":true,\"italic\":true,\"family\":\"Berlin Sans FB Demi\",\"size\":36,\"color\":\"#777777\"}}"`
List []s
Export *sut `json:"export" export:"结构体,10"`
}
e, err := goexcel.NewExcelSheet1ToListFromPath("20210814182854.xlsx", &p{})
if err != nil {
fmt.Println(err.Error())
}
for e.Next() {
data := e.GetRow()
d := data.(*p)
fmt.Println(fmt.Sprintf("%+v", d))
}
}
Output:
Types ¶
This section is empty.