xlsx

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2020 License: MIT Imports: 11 Imported by: 0

README

xlsx

golang mapping between xlsx and struct instances.

本库提供高层golang的struct切片和excel文件的映射,避免直接处理底层sheet/row/cells等细节。

本库底层使用unioffice,其提供了比360EntSecGroup-Skylar/excelize更加友好的API。

还有另外一个比较活跃的底层实现tealeg/xlsx尚未认证。

Usage documentation

Directly write excel file
package main

import "github.com/bingoohuang/xlsx"

type memberStat struct {
	xlsx.T `sheet:"会员"` // 可选,如果不声明,会默认选择第一个sheet页进行读写

	Total     int `title:"会员总数"`
	New       int `title:"其中:新增"`
	Effective int `title:"其中:有效"`
}

func main() {
	x, _ := xlsx.New()
    defer x.Close()

    x.Write([]memberStat{
    		{Total: 100, New: 50, Effective: 50},
    		{Total: 200, New: 60, Effective: 140},
    	})
    x.SaveToFile("testdata/test1.xlsx")
}

you will get the result excel file in testdata/demo1.xlsx as the following:

image

Write excel with template file
x, _ := xlsx.New(xlsx.WithTemplate("testdata/template.xlsx"))
defer x.Close()

x.Write([]memberStat{
        {Total: 100, New: 50, Effective: 50},
        {Total: 200, New: 60, Effective: 140},
    })
x.SaveToFile("testdata/test1.xlsx")

you will get the result excel file in testdata/demo2.xlsx as the following:

image

Read excel with titled row
var memberStats []memberStat

x, _ := xlsx.New(xlsx.WithInputFile("testdata/test1.xlsx"))
defer x.Close()

if err := x.Read(&memberStats); err != nil {
    panic(err)
}

assert.Equal(t, []memberStat{
    {Total: 100, New: 50, Effective: 50},
    {Total: 200, New: 60, Effective: 140},
}, memberStats)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertNumberToFloat64

func ConvertNumberToFloat64(v interface{}) (float64, bool)

ConvertNumberToFloat64 converts a number value to float64. If the value is not a number, it returns 0, false.

func ParseJavaTimeFormat

func ParseJavaTimeFormat(layout string) string

ParseJavaTimeFormat converts the time format in java to golang.

Types

type Option

type Option struct {
	TemplateFile string
	InputFile    string
	// contains filtered or unexported fields
}

Option defines the option for the xlsx processing.

type OptionFn

type OptionFn func(*Option)

OptionFn defines the func to change the option.

func WithInputFile

func WithInputFile(f string) OptionFn

WithInputFile defines the input excel file for reading.

func WithTemplate

func WithTemplate(f string) OptionFn

WithTemplate defines the template excel file for writing template.

func WithUpload added in v1.1.0

func WithUpload(r *http.Request, filenameKey string) OptionFn

WithUpload defines the input excel file for reading.

type T

type T interface {
	// contains filtered or unexported methods
}

T just for tag for convenience to declare some tags for the whole structure.

type Xlsx

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

Xlsx is the structure for xlsx processing.

func New

func New(optionFns ...OptionFn) (xlsx *Xlsx, err error)

New creates a new instance of Xlsx.

func (*Xlsx) Close

func (x *Xlsx) Close() error

Close does some cleanup like remove temporary files.

func (*Xlsx) Download added in v1.1.0

func (x *Xlsx) Download(w http.ResponseWriter, filename string) error

Download downloads the excels file in the http response.

func (*Xlsx) Read

func (x *Xlsx) Read(slicePtr interface{}) error

func (*Xlsx) Save

func (x *Xlsx) Save(w io.Writer) error

Save writes the workbook out to a writer in the zipped xlsx format.

func (*Xlsx) SaveToFile

func (x *Xlsx) SaveToFile(file string) error

SaveToFile writes the workbook out to a file.

func (*Xlsx) Write

func (x *Xlsx) Write(beans interface{})

Write Writes beans to the underlying xlsx.

Jump to

Keyboard shortcuts

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