xlsread

package
v0.0.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

excel 快速读取工具

支持从excel文档到 struct 或者 []struct

使用示例


Documentation

Index

Constants

View Source
const TAG_NAME = "xlsr"

标签名称

Variables

View Source
var (
	ErrNotPtr     = errors.New("dst needs to be a pointer")
	ErrInvlidKind = errors.New("invalid kind of dst")
)
View Source
var (
	DefaultDateStyleStr = "yyyy-mm-dd"
	DefaultDateStyle    = CellStyle{
		Style: &excelize.Style{CustomNumFmt: &DefaultDateStyleStr},
	}
)
View Source
var (
	ErrUnknownType = errors.New("unknown type field")
)

Functions

func DefaultDateParser

func DefaultDateParser(src string) any

默认日期格式处理

func DefaultStrDataParser

func DefaultStrDataParser(src string) any

Types

type CellStyle

type CellStyle struct {
	Style *excelize.Style
	// contains filtered or unexported fields
}

格式转换器

type Cursor

type Cursor interface {
	Next() (hasNext bool)
	All(dst any) error
	Parse(dst any) error
}

type Document

type Document struct {
	// contains filtered or unexported fields
}

func New

func New(path string) (d *Document, e error)

新建一个excel文件 path: 存储路径 @return d: 操作句柄

func OpenFile

func OpenFile(path string) (d *Document, e error)

打开一个excel文件 path: 存储路径 @return d: 操作句柄

func OpenReader

func OpenReader(reader io.Reader) (d *Document, e error)

func (*Document) Close

func (d *Document) Close() (err error)

Close implements io.Closer

func (*Document) GetSheetData

func (d *Document) GetSheetData(opt *Option) ([][]string, error)

func (*Document) ReadSheetByRow

func (d *Document) ReadSheetByRow(opt ...RowReadOption) (Cursor, error)

func (*Document) ReadSheetByTable

func (d *Document) ReadSheetByTable(dst any, opt ...*Option) error

将工作表中的数据根据struct中的tag插入结构体中 (目标为单个结构体)

func (*Document) Save

func (d *Document) Save() (err error)

func (*Document) Write

func (d *Document) Write(w io.Writer) (size int64, err error)

func (*Document) WriteBuffer

func (d *Document) WriteBuffer() (*bytes.Buffer, error)

func (*Document) WriteSheetByRow

func (d *Document) WriteSheetByRow(opt ...RowWriteOption) WriteCursor

func (*Document) WriteSheetData

func (d *Document) WriteSheetData(opt *Option) (res [][]string, err error)

type IParser

type IParser func(string) any

type Option

type Option struct {
	SheetName string
}

type RowReadCursor

type RowReadCursor struct {
	// contains filtered or unexported fields
}

读取光标

func (*RowReadCursor) All

func (c *RowReadCursor) All(dst any) error

将工作表中的数据根据struct中的tag插入结构体中 (目标为结构体切片)

func (*RowReadCursor) Next

func (c *RowReadCursor) Next() (hasNext bool)

funcNext implements Cursor

func (*RowReadCursor) Parse

func (c *RowReadCursor) Parse(dst any) error

处理本行 dst: 目标结构体

type RowReadOption

type RowReadOption struct {
	Parsers map[string]IParser
	Option
}

type RowWriteCursor

type RowWriteCursor struct {
	// contains filtered or unexported fields
}

func (*RowWriteCursor) All

func (c *RowWriteCursor) All(dst any) error

All implements WriteCursor

func (*RowWriteCursor) Format

func (c *RowWriteCursor) Format(dst any) error

Format implements WriteCursor

func (*RowWriteCursor) Next

func (c *RowWriteCursor) Next()

type RowWriteOption

type RowWriteOption struct {
	Option
	Col    int // 最大列数(过多会导致内存浪费,过少会导致数据被截断)
	Row    int // 开始光标所在行数
	Styles map[string]CellStyle
}

type TagInfo

type TagInfo struct {
	// contains filtered or unexported fields
}

type WriteCursor

type WriteCursor interface {
	Next()
	All(src any) error
	Format(src any) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL