xlsxreader

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2020 License: MIT Imports: 10 Imported by: 0

README

xlsxreader logo

xlsxreader: A Go package for reading data from an xlsx file

Overview GoDoc CircleCI Go Report Card

A low-memory high performance library for reading data from an xlsx file.

Suitable for reading .xlsx data and designed to aid with the bulk uploading of data where the key requirement is to parse and read raw data.

The reader will read data out row by row (1->n) and has no concept of headers or data types (this is to be managed by the consumer).

The reader is currently not concerned with handling some of the more advanced cell data that can be stored in a xlsx file.

Further reading on how this came to be is available on our blog

Install

go get github.com/thedatashed/xlsxreader

Example Usage

Reading from the file system:

package main

import (
  "github.com/thedatashed/xlsxreader"
)

func main() {
    // Create an instance of the reader by opening a target file
    xl, _ := xlsxreader.OpenFile("./test.xlsx")

    // Ensure the file reader is closed once utilised
    defer xl.Close()

    // Iterate on the rows of data
    for row := range xl.ReadRows(e.Sheets[0]){
    ...
    }
}

Reading from an already in-memory source

package main

import (
  "io/ioutil"
  "github.com/thedatashed/xlsxreader"
)

func main() {

    // Preprocessing of file data
    file, _ := os.Open("./test/test-small.xlsx")
    defer file.Close()
    bytes, _ := ioutil.ReadAll(file)

    // Create an instance of the reader by providing a data stream
    xl, _ := xlsxreader.NewReader(bytes)

    // Iterate on the rows of data
    for row := range xl.ReadRows(e.Sheets[0]){
    ...
    }
}

Key Concepts

Files

The reader operates on a single file and will read data from the specified file using the OpenFile function.

Data

The Reader can also be instantiated with a byte array by using the NewReader function.

Sheets

An xlsx workbook can contain many worksheets, when reading data, the target sheet name should be passed. To process multiple sheets, either iterate on the array of sheet names identified by the reader or make multiple calls to the ReadRows function with the desired sheet names.

Rows

A sheet contains n rows of data, the reader returns an iterator that can be accessed to cycle through each row of data in a worksheet. Each row holds an index and contains n cells that contain column data.

Cells

A cell represents a row/column value and contains a string representation of that data. Currently numeric data is parsed as found, with dates parsed to ISO 8601 / RFC3339 format.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToSlice

func ToSlice(filenname string) (output [][][]string, err error)

Types

type Cell

type Cell struct {
	Column string // E.G   A, B, C
	Row    int
	Value  string
}

Cell represents the data in a single cell as a consumable format.

type Row

type Row struct {
	Error error
	Index int
	Cells []Cell
}

Row represents a row of data read from an Xlsx file, in a consumable format

type XlsxFile

type XlsxFile struct {
	Sheets []string
	// contains filtered or unexported fields
}

XlsxFile defines a populated XLSX file struct.

func NewReader

func NewReader(xlsxBytes []byte) (*XlsxFile, error)

NewReader takes bytes of Xlsx file and returns a populated XlsxFile struct for it. If the file cannot be found, or key parts of the files contents are missing, an error is returned.

func (*XlsxFile) ReadRows

func (x *XlsxFile) ReadRows(sheet string) chan Row

ReadRows provides an interface allowing rows from a specific worksheet to be streamed from an xlsx file. In order to provide a simplistic interface, this method returns a channel that can be range-d over.

This method has one notable drawback however - the entire file must be consumed before the channel will be closed. Reading only some of the values will leave an orphaned goroutine and channel behind.

Notes: Xlsx sheets may omit cells which are empty, meaning a row may not have continuous cell references. This function makes not attempt to fill/pad the missing cells.

type XlsxFileCloser

type XlsxFileCloser struct {
	XlsxFile
	// contains filtered or unexported fields
}

XlsxFileCloser wraps XlsxFile to be able to close an open file

func OpenFile

func OpenFile(filename string) (*XlsxFileCloser, error)

OpenFile takes the name of an XLSX file and returns a populated XlsxFile struct for it. If the file cannot be found, or key parts of the files contents are missing, an error is returned. Note that the file must be Close()-d when you are finished with it.

func (*XlsxFileCloser) Close

func (xl *XlsxFileCloser) Close() error

Close closes the XlsxFile, rendering it unusable for I/O.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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