excel

package
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: MIT Imports: 12 Imported by: 0

README

go-excel

简介

go-excel 是基于 excelize 的简单封装,支持通过结构体便捷地读写 Excel 文件。

无论是读取 Excel 还是写入 Excel,都需要定义一个结构体,结构体的字段通过 tag(即 ex)来指定 Excel 的相关信息。

特性

  • 通过结构体标签定义Excel列映射关系
  • 支持读取和写入Excel文件
  • 支持基于validator的数据验证

安装

go get github.com/morehao/go-excel

使用

读取Excel

读取Excel的简单示例:

package main

import (
	"fmt"
	"github.com/morehao/go-excel"
	"github.com/xuri/excelize/v2"
)

type DataItem struct {
	SerialNumber int64  `ex:"head:序号" validate:"min=10,max=100"`
	UserName     string `ex:"head:姓名"`
	Age          int64  `ex:"head:年龄"`
}

func main() {
	f, openErr := excelize.OpenFile("test.xlsx")
	if openErr != nil {
		fmt.Println("open file error: ", openErr)
		return
	}
	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	reader := excel.NewReader(f, &excel.ReaderOption{
		SheetNumber:  0,
		HeadRow:      0,
		DataStartRow: 1,
	})
	var dataList []DataItem
	validateErrMap, readerErr := reader.Read(&dataList)
	if readerErr != nil {
		fmt.Println("read error: ", readerErr)
		return
	}
	if len(validateErrMap) > 0 {
		fmt.Println("validate error: ", validateErrMap)
		return
	}
	for _, item := range dataList {
		fmt.Println(item)
	}
}
写入Excel

生成Excel的简单示例:

package main

import (
	"fmt"
	"github.com/morehao/go-excel"
)

type DataItem struct {
	SerialNumber int64  `ex:"head:序号" validate:"min=10,max=100"`
	UserName     string `ex:"head:姓名"`
	Age          int64  `ex:"head:年龄"`
}

func main() {
	var dataList []DataItem
	dataList = append(dataList, DataItem{
		SerialNumber: 1,
		UserName:     "张三",
		Age:          18,
	})
	excelWriter := excel.NewWrite(&excel.WriteOption{
		SheetName: "Sheet1",
		HeadRow:   0,
	})
	if err := excelWriter.SaveAs(dataList, "write.xlsx"); err != nil {
		fmt.Println("write error: ", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

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

func NewReader

func NewReader(file *excelize.File, option *ReaderOption) *Reader

func (*Reader) Read

func (r *Reader) Read(dest interface{}) (map[int][]ValidationError, error)

type ReaderOption

type ReaderOption struct {
	SheetNumber  int // 0开始
	HeadRow      int // 0开始
	DataStartRow int // 0开始
}

type ValidationError

type ValidationError struct {
	DataRowNumber int    // Excel 表中的行号,从0开始
	Head          string // Excel 表中的列名(即表头名)
	CellValue     string // Excel 表中的单元格值
	ExpectType    string // 期望的单元格类型
	ErrorMessage  string // 错误信息
}

type Write

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

func NewWrite

func NewWrite(option *WriteOption) *Write

func (*Write) GenerateFileStream

func (w *Write) GenerateFileStream(data interface{}) (*bytes.Buffer, error)

func (*Write) SaveAs

func (w *Write) SaveAs(data interface{}, filePath string) error

type WriteOption

type WriteOption struct {
	SheetName string // 表名
	HeadRow   int    // 0开始
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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