package
0.0.0-20240826130954-40fd50bc6bd8
Repository: https://github.com/zhao520a1a/go-utils.git
Documentation: pkg.go.dev
# README
简介
引言
对于很多非开发人员来说, 写个excel要比写json或者yaml什么简单得多。这种场景下,读取特定格式(符合关系数据库特点的表格)的数据会比各种花式写入Excel的功能更为重要,于是写了这个简化库。
目标
期望创建一个阅读器库以轻松阅读类似excel,就像读取 DB 中数据一样。Cancel changes
示例
所有的示例可以移步查看 decode_test.go.
假设你有一个如下的 excel 文件(第一行是表头,其他行是内容):
ID | NameOf | AgeOf | Slice | UnmarshalString |
---|---|---|---|---|
1 | Andy | 1 | 1|2 | {"Foo":"Andy"} |
2 | Leo | 2 | 2|3|4 | {"Foo":"Leo"} |
3 | Ben | 3 | 3|4|5|6 | {"Foo":"Ben"} |
4 | Ming | 4 | 1 | {"Foo":"Ming"} |
// defined a struct
type Standard struct {
// 使用字段名称作为默认列名称
ID int
// column表示映射列名
Name string `xlsx:"column(NameOf)"`
// 可以将一列映射到多个字段中
NamePtr *string `xlsx:"column(NameOf)"`
// 如果只想映射到列名,则忽略“ column”,等价于“ column(AgeOf)”
Age int `xlsx:"AgeOf"`
// split表示通过`|`将字符串分割成切片
Slice []int `xlsx:"split(|)"`
// *Temp 实现了 `encoding.BinaryUnmarshaler`
Temp *Temp `xlsx:"column(UnmarshalString)"`
// 使用“-”去忽略映射信息
Ignored string `xlsx:"-"`
}
type Temp struct {
Foo string
}
func (this *Temp) UnmarshalBinary(d []byte) error {
return json.Unmarshal(d, this)
}
func simpleUsage() {
var stdList []Standard
err := excel.UnmarshalXLSX("./testdata/simple.xlsx", &stdList)
if err != nil {
panic(err)
}
}
Tips:
- 空行将被跳过。
- 大于len(TitleRow)的列将被跳过。
- 只有空单元格可以填充默认值,如果一个单元格不能解析成一个字段,将返回一个错误。
- 默认值也可以通过
encoding.BinaryUnmarshaler
来解读。 - 如果没有标题行私有化,默认的列名如
'A', 'B', 'C', 'D' ......, 'XFC', 'XFD'
可以作为26数字系统的列名使用。 - 当标题行有重复的标题,将返回错误`ErrDuplicatedTitles'。
- 针对excel版本,支持.xlsx 不支持.xls。
进阶用法
自定义配置
Using a config as "excel.Config":
type Config struct {
//如果sheet是字符串,将使用sheet作为工作表名称。
// 如果sheet是int,将使用工作簿中的第i个sheet,注意隐藏的sheet会被计算在内。
// 如果工作表是一个实现`GetXLSXSheetName()string'的对象,将使用其返回值。
// 否则,将使用sheet作为结构并反映它的名称。
// 如果sheet是一个切片,将像以前一样使用元素的类型来推断。
Sheet interface{}
// 指定作为标题的索引行,标题行之前的每一行都将被忽略,默认为0。
TitleRowIndex int
// 跳过标题后的n行,默认为0(不跳过),空行不计算在内。
Skip int
// 自动为sheet添加前缀。
Prefix string
// 自动为sheet添加后缀。
Suffix string
}
XLSX 标签使用
column
映射到标题行的字段名,默认将使用字段名。
default
当excel单元格中没有填入数值时,设置默认值,默认为0或""。
split
分割一个字符串,并将其转换为一个切片。
nil
当数据等于'nil(xxx)'中xxx时,将跳过扫描单元格中的值,不会赋值到struct字段。
req
如果excel中不存在clomun标题,将返回错误。
XLSX Field Config | 字段的解析配置
有时处理转义字符有点麻烦,所以实现GetXLSXFieldConfigs() map[string]FieldConfig
的接口将比tag
更优先提供字段配置,更多信息请看测试文件field_config_test.go。
参考资料
这个库主要参考了szyhf/go-excel 的部分实现和读取逻辑。
# Functions
GetString 转换一个对象为string.
Hack: 实现上有偏差 参考:https://blog.csdn.net/qq_15043089/article/details/118612717#circle=on.
No description provided by the author
NewConnector make a new connecter to connect to a exist xlsx file.
ToBool 字符串中将"1", "t", "T", "true", "TRUE", "True"转为true 将"0", "f", "F", "false", "FALSE", "False", ""转为false,空字符串也转为false 数字中仅支持将0转为false,1转为true。.
ToDecimalism convert string to int.
ToFloat32 尽最大努力将一个值转为float32类型的数据 string会按顺序尝试将数据解析为float64\uint64\bool,然后再转换为float64 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
ToFloat64 尽最大努力将一个值转为float64类型的数据 string会按顺序尝试将数据解析为float64\uint64\bool,然后再转换为float64 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
ToInt 尽最大努力将一个值转为int类型的数据 string会按顺序尝试将数据解析为int64\uint64\float64\bool,然后再转换为int float会抹去小数 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
ToInt16 尽最大努力将一个值转为int16类型的数据 string会按顺序尝试将数据解析为int64\uint64\float64\bool,然后再转换为int16 float会抹去小数 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
ToInt32 尽最大努力将一个值转为int32类型的数据 string会按顺序尝试将数据解析为int64\uint64\float64\bool,然后再转换为int32 float会抹去小数 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
ToInt64 尽最大努力将一个值转为int64类型的数据 string会按顺序尝试将数据解析为int64\uint64\float64\bool,然后再转换为int64 float会抹去小数 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
ToInt8 string会按顺序尝试将数据解析为int64\uint64\float64\bool,然后再转换为int8 float会抹去小数 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
No description provided by the author
ToUint 尽最大努力将一个值转为uint类型的数据 string会按顺序尝试将数据解析为int64\uint64\float64\bool,然后再转换为uint float会抹去小数 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
ToUint16 尽最大努力将一个值转为uint16类型的数据 string会按顺序尝试将数据解析为int64\uint64\float64\bool,然后再转换为uint16 float会抹去小数 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
ToUint32 尽最大努力将一个值转为uint32类型的数据 string会按顺序尝试将数据解析为int64\uint64\float64\bool,然后再转换为uint32 float会抹去小数 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
ToUint64 尽最大努力将一个值转为uint64类型的数据 string会按顺序尝试将数据解析为int64\uint64\float64\bool,然后再转换为uint64 float会抹去小数 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
ToUint8 尽最大努力将一个值转为uint8类型的数据 string会按顺序尝试将数据解析为int64\uint64\float64\bool,然后再转换为uint8 float会抹去小数 uint8~64、int8~64都会做默认的转换 bool类型的数据,true-1;false-0.
UnmarshalXLSX unmarshal a sheet of XLSX file into a slice container.
# Variables
ErrConnectNotOpened means can not open connect to excel.
ErrDuplicatedTitles means the row of title has duplicated value and can not read into a map or struct since it need unique keys.
ErrEmptyRow means the row is empty.
ErrInvalidConatiner means can not using the container.
ErrNoRow means there is no row.
ErrScanNil means scan nil.
ErrSharedStringsNotExist means can not found the shared of excel.
ErrWorkbookNotExist means can not found the workbook of excel.
ErrWorkbookRelsNotExist means can not found the workbook rels of excel.
# Interfaces
An Connector of excel file.
No description provided by the author
Reader to read excel.