excel

package module
v0.0.0-...-3be3a2f Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 15 Imported by: 0

README

Excel

中文 | English

Excel is a Go library for reading and writing Excel files with ease. It provides a simple, struct-based API to map spreadsheet data directly to Go objects and vice versa. Built on top of the high-performance excelize library.

Go Reference Go Report Card

Features

  • Read Excel sheets into slices of structs
  • Write slices of structs to Excel files
  • Support for multiple sheets in a single file
  • Customizable column mapping via struct tags (xlsx:"column_name")
  • Handle headers not in the first row with offset
  • Support for custom types by implementing MarshalXLSX / UnmarshalXLSX
  • Stream export for large datasets (faster, string‑only values)
  • Works with files or io.Reader / io.Writer

Installation

go get github.com/cuishu/excel

Quick Start

Define a struct with xlsx tags that match the column headers in your Excel file:

type Human struct {
    ID   int    `xlsx:"id"`
    Name string `xlsx:"name"`
}

Assume an Excel file a.xlsx with a sheet named Sheet1:

id name
1 Smith
Reading a Sheet
var humans []Human

// From file
err := excel.NewSheetFromFile("a.xlsx", "Sheet1").Scan(&humans)
if err != nil {
    // handle error
}

// From io.Reader
reader := ... // e.g., http.Request body or os.File
err = excel.NewSheetFromReader(reader, "Sheet1").Scan(&humans)

for _, h := range humans {
    fmt.Println(h.Name)
}
Writing a Sheet
humans := []Human{
    {ID: 1, Name: "Smith"},
    {ID: 2, Name: "Jack"},
    {ID: 3, Name: "James"},
}

buff, err := excel.NewSheet("Sheet1").Export(&humans)
if err != nil {
    // handle error
}
err = ioutil.WriteFile("a.xlsx", buff.Bytes(), 0644)

Working with Multiple Sheets

When your Excel file contains several sheets, use a container struct that holds slices for each sheet. The sheet name is specified in the xlsx tag.

type Human struct {
    ID   int    `xlsx:"id"`
    Name string `xlsx:"name"`
}

type Animal struct {
    Species string `xlsx:"species"`
    Legs    int    `xlsx:"legs"`
}

type Data struct {
    Humans  []Human  `xlsx:"humans"`
    Animals []Animal `xlsx:"animals"`
}
Reading Multiple Sheets
var data Data

// From file
err := excel.NewExcel(Filename: "b.xlsx").Scan(&data)

// From io.Reader
err = excel.NewExcelFromReader(reader).Scan(&data)
Writing Multiple Sheets
data := Data{
    Humans:  []Human{{ID: 1, Name: "Smith"}},
    Animals: []Animal{{Species: "Cat", Legs: 4}},
}

buff, err := excel.Excel{}.Export(&data)
if err != nil {
    // handle error
}
ioutil.WriteFile("b.xlsx", buff.Bytes(), 0644)

Advanced Usage

Header Offset

If the column headers are not on the first row, use Offset(n) to skip rows:

var items []MyStruct
err := excel.NewSheetFromFile("data.xlsx", "Sheet1").
    Offset(2). // skip first two rows
    Scan(&items)
Custom Type Marshaling

Implement MarshalXLSX and UnmarshalXLSX to control how your custom types are converted to/from Excel cell values.

type Sex int

const (
    Male Sex = iota + 1
    Female
)

func (s Sex) MarshalXLSX() ([]byte, error) {
    switch s {
    case Male:
        return []byte("Male"), nil
    case Female:
        return []byte("Female"), nil
    default:
        return nil, errors.New("invalid sex")
    }
}

func (s *Sex) UnmarshalXLSX(data []byte) error {
    switch string(data) {
    case "Male":
        *s = Male
    case "Female":
        *s = Female
    default:
        return errors.New("invalid sex")
    }
    return nil
}

Now you can use Sex directly in your struct:

type Person struct {
    Name string `xlsx:"name"`
    Sex  Sex    `xlsx:"sex"`
}
Stream Export for Large Data

StreamExport writes rows one by one, reducing memory usage. It is faster but only accepts values that can be directly converted to strings (no custom marshaling).

bigData := make([]Human, 1000000) // large slice

buff, err := excel.NewSheet("Sheet1").StreamExport(&bigData)
if err != nil {
    // handle error
}
ioutil.WriteFile("large.xlsx", buff.Bytes(), 0644)

Supported Data Types

The following Go types are supported out of the box:

  • Signed integers: int, int8, int16, int32, int64
  • Unsigned integers: uint, uint8, uint16, uint32, uint64
  • Floating point: float32, float64
  • string
  • bool
  • time.Time (converted to Excel date/time format)

Custom types can be supported by implementing the marshaling interfaces as shown above.

License

MIT License. See LICENSE for details.


Powered by excelize

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell

type Cell struct {
	Value     string
	HyperLink HyperLink
	Style     *excelize.Style
}

type Error

type Error struct {
	Row Row
	// contains filtered or unexported fields
}

func (Error) Error

func (e Error) Error() string

type Excel

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

func NewExcel

func NewExcel(filename string) *Excel

Create a new Excel instance with filename.

func NewExcelFromReader

func NewExcelFromReader(r io.Reader) *Excel

Create a new Excel instance with io.Reader.

func (Excel) Export

func (e Excel) Export(v any) (*bytes.Buffer, error)

Export exports the struct pointed to by Slice to an byte buffer.

func (Excel) ExportTo

func (e Excel) ExportTo(w io.Writer, v any) error

Export exports the struct pointed to by Slice to an io.Writer.

func (*Excel) Offset

func (e *Excel) Offset(n int) *Excel

Offset sets the offset of the first row to read.

func (Excel) Scan

func (e Excel) Scan(v any) error

Scan reads the data from the Excel file and stores it in the struct pointed to by Slice.

func (Excel) StreamExport

func (e Excel) StreamExport(v any) (*bytes.Buffer, error)

StreamExport exports the struct pointed to by Slice to an byte buffer.

func (Excel) StreamExportTo

func (e Excel) StreamExportTo(w io.Writer, v any) error

StreamExport exports the struct pointed to by Slice to an io.Writer.

func (*Excel) UseTextStyle

func (e *Excel) UseTextStyle() *Excel

UseTextStyle sets the style of the cell to text.

type HyperLink struct {
	Link string
	Type LinkType
}

type LinkType

type LinkType string
const (
	External LinkType = "External"
	Location LinkType = "Location"
)

type PicFormat

type PicFormat excelize.GraphicOptions

type Picture

type Picture struct {
	Name   string
	File   []byte
	Format *PicFormat
	// contains filtered or unexported fields
}

func NewPicture

func NewPicture(path string, format *PicFormat) Picture

func NewPictureFromBytes

func NewPictureFromBytes(file []byte, format *PicFormat) (Picture, error)

func (Picture) Buffer

func (pic Picture) Buffer() *bytes.Buffer

type Row

type Row struct {
	ID   int
	Data map[string]string
}

func (Row) Get

func (row Row) Get(key string) string

type Schema

type Schema map[string]bool

type Sheet

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

func NewSheet

func NewSheet(sheet string) *Sheet

NewSheet creates a new Sheet.

func NewSheetFromFile

func NewSheetFromFile(filename, sheet string) *Sheet

NewSheetFromFile creates a new Sheet from a file.

func NewSheetFromReader

func NewSheetFromReader(r io.Reader, sheet string) *Sheet

NewSheetFromReader creates a new Sheet from a io.Reader.

func (*Sheet) CollectErrors

func (s *Sheet) CollectErrors() *Sheet

CollectErrors collects errors while scanning the sheet.

func (*Sheet) Errors

func (s *Sheet) Errors() []Error

Errors returns the errors collected while scanning the sheet.

func (*Sheet) Export

func (s *Sheet) Export(v any) (*bytes.Buffer, error)

Export exports the sheet to a bytes.Buffer.

func (*Sheet) ExportTo

func (s *Sheet) ExportTo(w io.Writer, v any) error

ExportTo exports the sheet to a io.Writer.

func (*Sheet) Filter

func (s *Sheet) Filter(schema Schema) *Sheet

Filter sets the filter of the sheet.

func (*Sheet) Offset

func (s *Sheet) Offset(n int) *Sheet

Offset sets the offset of the sheet.

func (*Sheet) Scan

func (s *Sheet) Scan(v any) error

func (*Sheet) StreamExport

func (s *Sheet) StreamExport(v any) (*bytes.Buffer, error)

func (*Sheet) StreamExportTo

func (s *Sheet) StreamExportTo(writer io.Writer, v any) error

func (*Sheet) UseTextStyle

func (s *Sheet) UseTextStyle() *Sheet

UseTextStyle sets the style of the cell to text.

type XLSXMarshaler

type XLSXMarshaler interface {
	MarshalXLSX() ([]byte, error)
}

type XLSXUnmarshaler

type XLSXUnmarshaler interface {
	UnmarshalXLSX(data []byte) error
}

Jump to

Keyboard shortcuts

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