excel

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

go-excel

  • depend on excelize
  • read and write excel with struct

Functions

  • read from filepath GetRowsFromFile
  • read from io.Read GetRowsFromRead
  • read from upload multipart file GetRowsFromMultipart

Usage

  1. import lib

    go get -u github.com/kangarooxin/go-excel
    
  2. mark struct column with tag xlsx, if not config tag, use field name default.

    type User struct {
    	Id       int       `xlsx:"账号ID"`
    	Name     string    `xlsx:"账号名"`
    	Birthday time.Time `xlsx:"生日"`
        Interest []string  `xlsx:"兴趣"`
        Numbers  []int     `xlsx:"数字"`
    }
    
  3. create excel with struct slice

    func TestCreate(t *testing.T) {
    	users := &[]User{
    		{
    			Id:       1,
    			Name:     "Test1",
    			Birthday: time.Now(),
                Interest: []string{"篮球", "户外"},
    		    Numbers:  []int{1, 2},
    		},
    		{
    			Id:       2,
    			Name:     "Test2",
    			Birthday: time.Now(),
                Interest: []string{"篮球", "户外"},
    		    Numbers:  []int{1, 2},
    		},
    	}
    	f, err := excel.NewFile(users)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	if err := f.SaveAs("Test1.xlsx"); err != nil {
    		fmt.Println(err)
    	}
    }
    
  4. read excel to struct slice

    func TestRead(t *testing.T) {
    	users := &[]User{}
    	err = excel.GetRowsFromFile("Test1.xlsx", users)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	fmt.Println(users)
    }
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCellName

func GetCellName(col, row int) string

func GetRows

func GetRows[T any](file *excelize.File, records *[]T) error

func GetRowsBySheetIndex

func GetRowsBySheetIndex[T any](file *excelize.File, index int, records *[]T) error

func GetRowsBySheetName

func GetRowsBySheetName[T any](file *excelize.File, sheet string, records *[]T) error

func GetRowsFromFile

func GetRowsFromFile[T any](filepath string, records *[]T) error

func GetRowsFromFileBySheetIndex

func GetRowsFromFileBySheetIndex[T any](filepath string, index int, records *[]T) error

func GetRowsFromFileBySheetName

func GetRowsFromFileBySheetName[T any](filepath string, sheet string, records *[]T) error

func GetRowsFromMultipart

func GetRowsFromMultipart[T any](file *multipart.FileHeader, records *[]T) error

func GetRowsFromMultipartBySheetIndex

func GetRowsFromMultipartBySheetIndex[T any](file *multipart.FileHeader, index int, records *[]T) error

func GetRowsFromMultipartBySheetName

func GetRowsFromMultipartBySheetName[T any](file *multipart.FileHeader, sheet string, records *[]T) error

func GetRowsFromReader

func GetRowsFromReader[T any](filepath string, records *[]T) error

func GetRowsFromReaderBySheetIndex

func GetRowsFromReaderBySheetIndex[T any](r io.Reader, index int, records *[]T) error

func GetRowsFromReaderBySheetName

func GetRowsFromReaderBySheetName[T any](r io.Reader, sheet string, records *[]T) error

func NewFile

func NewFile[T any](records *[]T) (*excelize.File, error)

func NewFileWithSheetName

func NewFileWithSheetName[T any](sheetName string, records *[]T) (*excelize.File, error)

func WriteCellValue

func WriteCellValue(f *excelize.File, sheet string, col, row int, value interface{}) error

Types

type TimeConvert

type TimeConvert struct{}

func (*TimeConvert) Convert

func (d *TimeConvert) Convert(v string) (any, error)

func (*TimeConvert) SupportType

func (d *TimeConvert) SupportType() reflect.Type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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