/excelsior

An excelize wrapper to separate the presentation and business logic

Primary LanguageGoMIT LicenseMIT

github pkg.go.dev

Dall-E generated exelsior image

Excelsior

An excelize wrapper to separate the presentation and business logic.

go get -u -v github.com/kakilangit/excelsior

Example:

package main

import (
	"log"
	"os"

	"github.com/kakilangit/excelsior"
	"github.com/xuri/excelize/v2"
)

// notFound implements excelsior.SheetData.
type notFound []string

// Total return total data.
func (f notFound) Total() int {
	return len(f)
}

// Row represents excel row.
func (f notFound) Row(i int) excelsior.Row {
	return []any{f[i]}
}

func main() {
	data, err := excelsior.Serialize(func(file *excelize.File, style excelsior.Style) ([]byte, error) {
		const sheetName = "not found alphabet"

		excelsior.SetDefaultSheetName(file, sheetName)

		headers := []any{"alphabet"}
		rows := []string{"a", "b", "c"}

		sheet := excelsior.NewSheet(headers, excelsior.DefaultGetStyleFn, style.Header(), notFound(rows))
		if err := sheet.Generate(file, sheetName); err != nil {
			return nil, err
		}

		return excelsior.Byte(file)
	})
	if err != nil {
		log.Fatal(err)
	}

	f, err := os.Create("alphabet.xlsx")
	if err != nil {
		log.Fatal(err)
	}
	defer func() { _ = f.Close() }()

	if _, err := f.Write(data); err != nil {
		log.Fatal(err)
	}

	if err := f.Sync(); err != nil {
		log.Fatal(err)
	}
}

License

MIT Copyright (c) 2022 kakilangit