excel

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package excel 提供了一个excel读写的封装 读取时需要注意,循环Exce.Fetch()直到返回nil就没有数据了,但是如果表格中有合并等情况,返回的row中并不能反应出来,需要自己check row中的数据

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Excel

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

Excel 操作Excel的结构

func NewReader

func NewReader(file string, header []*HeaderColumn, headerlineNum int) (*Excel, error)

NewReader 创建一个Excel读操作实例 接受的 header 为表头和字段映射关系,如:{Field: "title", Title: "标题", Requre: true} headerlineNum 表头行号是第几行,默认是第1行

func NewWriter

func NewWriter() (*Excel, error)

NewWriter 创建一个Excel写操作实例

func (*Excel) Fetch

func (t *Excel) Fetch() (MapValue, error)

Fetch 逐行读取内容,请循环调用该值,直到返回 nil 该方法会直接跳过空行

func (*Excel) Reset

func (t *Excel) Reset()

Reset 重置游标

func (*Excel) Save

func (t *Excel) Save(w io.Writer) error

Save 输出到io.Writer

func (*Excel) SaveToFile

func (t *Excel) SaveToFile(path string) error

SaveToFile 直接输出到文件

func (*Excel) SetHeader

func (t *Excel) SetHeader(header []*HeaderColumn, width map[string]float64) error

SetHeader 写模式下,设置字段表头和字段顺序 参数 header 为表头和字段映射关系,如:HeaderColumn{Field:"title", Title: "标题", Requre: true} 参数 width 为表头每列的宽度,单位 CM:map[string]float64{"title": 0.8}

func (*Excel) Write

func (t *Excel) Write(data MapValue) error

Write 写入行

type HeaderColumn

type HeaderColumn struct {
	Field   string // 字段,数据映射到的数据字段名
	Title   string // 标题,表格中的列名称
	Require bool   // 是否必选
}

HeaderColumn 表头字段定义

type MapValue

type MapValue map[string]interface{}

MapValue Excel读取出来的每一行的值

func (MapValue) Get

func (t MapValue) Get(key string) interface{}

Get 获取一个interface{}的值

func (MapValue) GetInt

func (t MapValue) GetInt(key string) int

GetInt 获取一个int类型的值

func (MapValue) GetString

func (t MapValue) GetString(key string) string

GetString 获取一个string类型的值

Jump to

Keyboard shortcuts

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