excel

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

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 ReadExcel struct {
	ID   int    `excel:"ID"`
	Name string `excel:"Name"`
}

func (*ReadExcel) ReadConfigure(rc *exl.ReadConfig) {}

func main() {
	if models, err := exl.ReadFile[*ReadExcel]("/to/path.xlsx"); 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 WriteExcel struct {
	ID   int    `excel:"ID"`
	Name string `excel:"Name"`
}

func (m *WriteExcel) WriteConfigure(wc *exl.WriteConfig) {}

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

Writer

package main

import (
	"fmt"

	"github.com/go-the-way/exl"
)

func main() {
	w := exl.NewWriter()
	if err := w.Write("int", []int{1, 2}); err != nil {
		fmt.Println(err)
		return
	}
	if err := w.Write("float", []float64{1.1, 2.2}); err != nil {
		fmt.Println(err)
		return
	}
	if err := w.Write("string", []string{"hello", "world"}); err != nil {
		fmt.Println(err)
		return
	}
	if err := w.Write("map", []map[string]string{{"id":"1000","name":"hello"},{"id":"2000","name":"world"}}); err != nil {
		fmt.Println(err)
		return
	}
	if err := w.Write("structWithField", []struct{ID int}{{1000},{2000}}); err != nil {
		fmt.Println(err)
		return
	}
	if err := w.Write("structWithTag", []struct{ID int `excel:"编号"`}{{1000},{2000}}); err != nil {
		fmt.Println(err)
		return
	}
	if err := w.Write("structWithTagAndIgnore", []struct{
		ID int `excel:"编号"`
		Extra int `excel:"-"`
		Name string `excel:"名称"`
	}{{1000,0,"Coco"},{2000,0,"Apple"}}); err != nil {
		fmt.Println(err)
		return
	} 
	if err := w.SaveTo("dist.xlsx"); err != nil {
		fmt.Println(err)
		return
	}
}

Documentation

Overview

Package exl

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

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSheetIndexOutOfRange        = errors.New("exl: sheet index out of range")
	ErrHeaderRowIndexOutOfRange    = errors.New("exl: header row index out of range")
	ErrDataStartRowIndexOutOfRange = errors.New("exl: data start row index out of range")
)

Functions

func ChanWriteTo

func ChanWriteTo[T WriteConfigurator](w io.Writer, c <-chan T) error

func Read

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

Read io.Reader each row bind to `T`

func ReadBinary

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

ReadBinary each row bind to `T`

func ReadExcel

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

ReadExcel walk func from excel

func ReadFile

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

ReadFile each row bind to `T`

func Write

func Write[T WriteConfigurator](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

func WriteExcelTo

func WriteExcelTo(w io.Writer, data [][]string) error

WriteExcelTo defines write [][]string to excel

params: w, the dist writer

params: data, write data to excel

func WriteTo

func WriteTo[T WriteConfigurator](w io.Writer, ts []T) error

WriteTo defines write to []T to excel file

params: w, the dist writer

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

Types

type ReadConfig

type ReadConfig struct {
	TagName           string
	SheetIndex        int
	HeaderRowIndex    int
	DataStartRowIndex int
	TrimSpace         bool
}

type ReadConfigurator

type ReadConfigurator interface{ ReadConfigure(rc *ReadConfig) }

type WriteConfig

type WriteConfig struct{ SheetName, TagName string }

type WriteConfigurator

type WriteConfigurator interface{ WriteConfigure(wc *WriteConfig) }

type Writer

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

Writer define a writer for exl

func NewWriter

func NewWriter(options ...xlsx.FileOption) *Writer

NewWriter returns new exl writer

func (*Writer) SaveTo

func (w *Writer) SaveTo(path string) (err error)

SaveTo the buffered binary into dist file

func (*Writer) Write

func (w *Writer) Write(sheet string, data any) error

Write or append the param data into sheet

func (*Writer) WriteTo

func (w *Writer) WriteTo(dw io.Writer) (n int, err error)

WriteTo the buffered binary into new writer

Jump to

Keyboard shortcuts

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