exl

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: Apache-2.0 Imports: 9 Imported by: 2

README

exl

Excel binding to struct written in Go.(Only supports Go1.18+)

CircleCI GitHub go.mod Go version codecov Go Report Card GoDoc Mentioned in Awesome Go

usage

Read Excel
package main

import (
	"fmt"
	"github.com/go-the-way/exl"
)

type ReadExcelModel struct {
	ID   int    `excel:"ID"`
	Name string `excel:"Name"`
}

func (*ReadExcelModel) Read(*exl.ReadMetadata) {}

func main() {
	if models, err := exl.ReadFile("/to/path.xlsx", new(ReadExcelModel)); err != nil {
		fmt.Println("read excel err:" + err.Error())
	} else {
		fmt.Printf("read excel num: %d\n", len(models))
	}
}
Write Excel
package main

import (
	"fmt"
	"github.com/go-the-way/exl"
)

type WriteExcelModel struct {
	ID   int    `excel:"ID"`
	Name string `excel:"Name"`
}

func (m *WriteExcelModel) Write(*exl.WriteMetadata) {}

func main() {
	if err := exl.Write("/to/path.xlsx", []*WriteExcelModel{{100, "apple"}, {200, "pear"}}); err != nil {
		fmt.Println("write excel err:" + err.Error())
	} else {
		fmt.Println("write excel done")
	}
}

Methods

  • exl.Read(reader io.Reader, bind T, filterFunc ...func(t T) (add bool)) error
  • exl.ReadFile(file string, bind T, filterFunc ...func(t T) (add bool)) error
  • exl.ReadBinary(bytes []byte, bind T, filterFunc ...func(t T) (add bool)) error
  • exl.Write(file string, ts []T) error
  • exl.ReadExcel(file string, sheetIndex int, walk func(index int, rows *xlsx.Row)) error
  • exl.WriteExcel(file string, data [][]string) error

Documentation

Overview

Package exl

Excel binding to struct written in Go.(Only supports Go1.18+)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Read

func Read[T ReadBind](reader io.Reader, bind T, filterFunc ...func(t T) (add bool)) ([]T, error)

Read defines read io.Reader each row bind to `T`

params: file,excel file full path

params: typed parameter T, must be implements exl.Bind

params: filterFunc, filter callback func

func ReadBinary

func ReadBinary[T ReadBind](bytes []byte, bind T, filterFunc ...func(t T) (add bool)) ([]T, error)

ReadBinary defines read binary each row bind to `T`

params: file,excel file full path

params: typed parameter T, must be implements exl.Bind

params: filterFunc, filter callback func

func ReadExcel

func ReadExcel(file string, sheetIndex int, walk func(index int, rows *xlsx.Row)) error

ReadExcel defines read walk func from excel

params: file, excel file pull path

params: sheetIndex, current sheet index

params: walk, walk func

func ReadFile

func ReadFile[T ReadBind](file string, bind T, filterFunc ...func(t T) (add bool)) ([]T, error)

ReadFile defines read excel each row bind to `T`

params: file,excel file full path

params: typed parameter T, must be implements exl.Bind

params: filterFunc, filter callback func

func Write

func Write[T WriteBind](file string, ts []T) error

Write defines write []T to excel file

params: file,excel file full path

params: typed parameter T, must be implements exl.Bind

func WriteExcel

func WriteExcel(file string, data [][]string) error

WriteExcel defines write [][]string to excel

params: file, excel file pull path

params: data, write data to excel

Types

type ReadBind

type ReadBind interface{ Read(rm *ReadMetadata) }

ReadBind defines read bind metadata

type ReadMetadata

type ReadMetadata struct {
	TagName           string // TagName: tag name
	SheetIndex        int    // SheetIndex: read sheet index
	HeaderRowIndex    int    // HeaderRowIndex: sheet header row index
	DataStartRowIndex int    // DataStartRowIndex: sheet data start row index
	TrimSpace         bool   // TrimSpace: trim space left and right only on `string` type
}

ReadMetadata defines read metadata

type WriteBind

type WriteBind interface{ Write(wm *WriteMetadata) }

WriteBind defines write bind metadata

type WriteMetadata

type WriteMetadata struct {
	SheetName string // SheetName: default sheet name
	TagName   string // TagName: tag name
}

WriteMetadata defines write metadata

Jump to

Keyboard shortcuts

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