An open API service indexing awesome lists of open source software.

https://github.com/miaomiao3/xlsxutil

golang reader and writer for csv/xlsx
https://github.com/miaomiao3/xlsxutil

csv excel go golang reflect

Last synced: 5 months ago
JSON representation

golang reader and writer for csv/xlsx

Awesome Lists containing this project

README

          

![CI status](https://github.com/miaomiao3/xlsxutil/actions/workflows/main.yml/badge.svg)
[![stability-unstable](https://img.shields.io/badge/stability-unstable-yellow.svg)](https://github.com/emersion/stability-badges#stable)

## xlsxutil
Super-fast to read and write csv or xlsx files.

# Feature
* Simple api. Only `*Load` and `*Dump` function.
* Struct inline support
* `precision` tag to set precision for xlsx
* Support separator when dump csv

# Limit
* Only support string and numeric data type

***
## Usage
`xls` tag is used in this repo.
Pay attention to keys:
* `inline` for extract struct
* `precision` for `floats` data type precision control.

Refer to example for more cases.

# Quickstart

Get codes
` $ go get github.com/miaomiao3/xlsxutil`

Import
`import ( "github.com/miaomiao3/xlsxutil" )`

***
Supposed a csv document content:

```$xslt
name,money,age,school,address
n-0,1.2345678900,20,school-0,hali-0
n-1,1.2345678900,21,school-1,hali-1
n-2,1.2345678900,22,school-2,hali-2
n-3,1.2345678900,23,school-3,hali-3
n-4,1.2345678900,24,school-4,hali-4
```
To bind this csv to out data, and dump data to csv string, just like

```go
package main

import (
"fmt"
"github.com/miaomiao3/xlsxutil"
)

const (
csvFilePath = "people.csv"
)

type Person struct {
Name string `xls:"name"`
Money float64 `xls:"money,precision:2"`
Age int `xls:"age"`
Edu `xls:",inline"`
}

type Edu struct {
School string `xls:"school"`
Address string `xls:"address"`
}

func main() {
persons := make([]*Person, 0)
err := xlsxutil.CsvLoad(csvFilePath, ",", &persons)
if err != nil {
panic(err)
}
fmt.Println("persons:", persons)

buf, err := xlsxutil.CsvDump(",", persons)
if err != nil {
panic(err)
}
fmt.Println("buf")
fmt.Println(buf)
}

```
terminal output:
```$xslt
$ go run main.go
persons: [0xc000078140 0xc000078180 0xc0000781c0 0xc000078200 0xc000078240]
buf
name,money,age,school,address
n-0,1.23,20,school-0,hali-0
n-1,1.23,21,school-1,hali-1
n-2,1.23,22,school-2,hali-2
n-3,1.23,23,school-3,hali-3
n-4,1.23,24,school-4,hali-4

```

# Examples
For other examples, Please refer to [examples](https://github.com/miaomiao3/xlsxutil/tree/master/example) dir.