Go (golang) library for reading and writing XLSX files.

XLSX

https://img.shields.io/travis/tealeg/xlsx/master.svg?style=flat-square https://codecov.io/gh/tealeg/xlsx/branch/master/graph/badge.svg https://godoc.org/github.com/tealeg/xlsx?status.svg https://img.shields.io/badge/license-bsd-orange.svg

Introduction

xlsx is a library to simplify reading and writing the XML format used by recent version of Microsoft Excel in Go programs.

Tutorial

If you’d like an introduction to this project try the new tutorial.

Different versions of this project

Prior to v1.0.0

You don’t want these versions ;-)

It’s hard to remember exactly, but work on this library started within a month of the first public announcement of Go, now more than a decade ago. It was essentially a quick hack to get data out of XLSX files at my workplace. Nobody but me relied on it, so it was fine to use this brand new language for this task. Somewhat later I decided to share the code, and I know it was well established as an open-source project by the time I left that job in late 2011.

Although I did do some “release” tags, versioning in Go in the early days relied on tagging your code with the name of the Go release (i.e. go1.2) and then `go get` would fetch that tag, if it existed, and if not, it’d grab the master branch.

Version 1.x.x

Version 1.0.0 was tagged in 2017 to support vendoring tools.

As of October 8th, 2019, I’ve branched off v1.x.x maintenance work from master. The master branch now tracks v2.x.x.

If you have existing code, can live with the issues in the 1.x.x codebase, and don’t want to update your code to use a later version, then you can stick to these releases. I mostly won’t be touching this code, but if something really important comes up, let me know.

Version 2.x.x

Version 2.0.0 introduced breaking changes in the API.

The scope of these changes included the way `Col` elements and `DataValidation` works, as these aspects have been built around incorrect models of the underlying XLSX format.

See the https://github.com/tealeg/xlsx/milestone/5 for details.

Version 2.0.1 was tagged purely because 2.0.0 wasn’t handled correctly with regards to how go modules work. It isn’t possible to use 2.0.0 from a Go Modules based project.

Version 3.x.x

Version 3.0.0 introduces some more breaking changes in the API. All methods that can return an `xlsx.File` struct now accept zero, one or many `xlsx.FileOption` functions as their final arguments. These can be used to modify the behaviour of the resultant struct - in particular they replace the `…WithRowLimit` variants of those methods with the result of calling `xlsx.RowLimit` and they add the ability to define a custom backing store for the spreadsheet data to be held in whilst processing.

StreamFileBuilder has been dropped from this version of the library as it has become difficult to maintain.

Full API docs

The full API docs can be viewed using go’s built in documentation tool, or online at godoc.org.

Contributing

We’re extremely happy to review pull requests. Please be patient, maintaining XLSX doesn’t pay anyone’s salary (to my knowledge).

If you’d like to propose a change please ensure the following:

Eat a peach - Geoff

Owner
Geoffrey J. Teale
Developer Experience engineer at HDI Systems Cloud Competence Centre.
Geoffrey J. Teale
Comments
  • Writer gives broken Excel File

    Writer gives broken Excel File

    Using the following file with version d6607c512cf8c1083a9b5ec1efd7e1ccfb1832c8 I get a broken Excel file. How can I write multiple rows?

    package main
    
    import (
        "fmt"
        "github.com/tealeg/xlsx"
    )
    
    func main() {
        var file *xlsx.File
        var sheet *xlsx.Sheet
        var row *xlsx.Row
        var cell *xlsx.Cell
        var err error
    
        file = xlsx.NewFile()
        sheet = file.AddSheet("Sheet1")
        row = sheet.AddRow()
        cell = row.AddCell()
        cell.Value = "I am a cell!"
    
        row = sheet.AddRow()
        cell = row.AddCell()
        cell.Value = "I am a cell!"
    
        err = file.Save("MyXLSXFile.xlsx")
    
        if err != nil {
            fmt.Printf(err.Error())
        }
    }
    
  • XLSX files produced by this library need repair by Excel.

    XLSX files produced by this library need repair by Excel.

    Excel 2013 is prone to give this warning: "We found a problem with some content in [generated file name.xlsx. Do you want us to try to recover as much as we can ?" But nothing appears to be broken - the generated data had made its way into the file.

    I suspect this issue is due to the incomplete style information exported currently.

  • Added merge cell borders

    Added merge cell borders

    We originally had both custom column width and merge cell borders in this patch, but coincidentally someone else did a custom column width patch, so we had to deal with that conflict.

  • Proposal: add method for reading structs from rows

    Proposal: add method for reading structs from rows

    Problem

    Reading structs from a row which has a well-known structure is a bit repetitive, because you will have to iter over the cells, check for the desired type to be obtained, checking for errors and filling the struct.

    Current approach

    If you want to read a whole file (or a single sheet) and convert it to a slice of structs, you have to take any of these two paths:

    • Use FileToSlice iter over the slices and handle the type-casting for yourself
    • Iter over the Sheets of the file, then over the rows and filling the structs by yourself

    Proposal

    Currently we have a method in *Row to Write a struct , so maybe we can define a symmetric method like (*Row).ReadStruct(interface{}) error that receives a struct pointer to be filled.

    To know the relation column <-> field we can use struct tags that defines the index of the cell to be dumped in the field, like so:

    type structTest struct {
    	IntVal     int     `xlsx:"0"`
    	StringVal  string  `xlsx:"1"`
    	FloatVal   float64 `xlsx:"2"`
    	IgnoredVal int     `xlsx:"-"`
    	BoolVal    bool    `xlsx:"4"`
    }
    

    I'm wondering if would be better to use a name for the column, but I sincerely don't know how this would work.

    I have a very simple implementation of the proposal using the tags in a forked repo:

    Implementation commit

    Comments are welcome!

  • Interrupting a program without saving a file corrupts it / no close function

    Interrupting a program without saving a file corrupts it / no close function

    This is not the issue by itself, the issue is that I HAVE to call Save() to properly close a file while I could be willing to discard it upon program interruption, for example with defer file.Close(). We need a proper function on the File struct to close it without saving.

  • Border style not allowed to be empty

    Border style not allowed to be empty

    https://github.com/tealeg/xlsx/commit/27f0f707d42799573616e9166c4be707dd4d2621 introduced a fix for merged cells AFAIK but introduced a regression which created borders styles with attribute empty string, which is not definition compliant. Value must be one of {'double', 'mediumDashDot', 'mediumDashDotDot', 'hair', 'dashDot', 'slantDashDot', 'medium', 'thick', 'mediumDashed', 'dashDotDot', 'thin', 'dotted', 'dashed'}

  • Opening an xlsx file and saving it as another xlsx file creates an invalid file

    Opening an xlsx file and saving it as another xlsx file creates an invalid file

    package main
    
    import (
        "fmt"
        "github.com/tealeg/xlsx"
    )
    
    func main() {
        var file *xlsx.File
        var err error
    
        file, err = xlsx.OpenFile("/path/to/file.xlsx")
        err = file.Save("/path/to/anotherFile.xlsx")
        if err != nil {
            fmt.Printf(err.Error())
        }
    }
    

    The above code compiles and runs fine. It creates anotherFile.xlsx but when opening this file Excel application crashes.

  • XLSX Style doesn't work

    XLSX Style doesn't work

    Hi, I'm using the XLSX style, e.g Fill style in a Cell, I set cell.SetStyle(style) where style is fill := NewFill("solid", "#12CDD3", "#FFFFFF"); style.Fill = *fill, but it does not show any fill color in the generated XLSX file, Am I using the style the right way, what I was missing here? I set ApplyFill to true by the way. ;-) Thanks

  • Need another release~

    Need another release~

    The last release 1.0.3 was released in 2017, is there any plan to release a new version, like 1.0.4 or 1.1.0? There is a lot of commits merged to master since 2017.

  • Fails to open with Google Docs

    Fails to open with Google Docs

    my sheet is simple and based on the Write example. I only use those 4 or 5 methods. Absolutely nothing fancy. The Sheets can be opened with Excel, however, not consistently with Google Docs. Sorry Google does not provide an explanation.

  • Open and Save xlsx Error

    Open and Save xlsx Error

    I use Mac Office 2013 to generator a xlsx,and Use following code to edit xlsx file and open test2.xlsx, it tell me this file need repair.

    
    func main() {
        file, err := xlsx.OpenFile("test.xlsx")
        if err != nil {
            log.Fatal(err)
        }
    
        sheet := file.Sheets[0]
        row := sheet.AddRow()
        row.AddCell().SetValue("12313131")
        err = file.Save("test2.xlsx")
        if err != nil {
            log.Fatal(err)
        }
    }
    
    
  • Upgrade CodeSee workflow to version 2

    Upgrade CodeSee workflow to version 2

    CodeSee is a code visibility platform.

    This change updates the CodeSee workflow file to the latest version for security, maintenance, and support improvements (see changelog below).

    That workflow file:

    • runs CodeSee's code analysis on every PR push and merge
    • uploads that analysis to CodeSee.
    • It does not transmit your code.

    The code analysis is used to generate maps and insights about this codebase.

    CodeSee workflow changelog:

    • Improved security: Updates permission to be read-only.
    • Improved future maintenance: Replaces the body of the workflow with a single github action: codesee-action. This makes it significantly easier for CodeSee to introduce future improvements and fixes without requiring another PR like this.
    • Improved Python support: The action now properly supports Python 3.11, and will continue to support new Python versions as they are released.
  • Resource leakage in the lib.go file

    Resource leakage in the lib.go file

    1、rc, err := worksheetRelsFile.Open() in the makeHyperlinkTable function does not close the RC handle. As a result, resource leakage occurs. 2、In the readSheetsFromZipFile function, rc, err = f.Open() does not close the rc handle, causing resource leakage. 3、In the readSharedStringsFromZipFile function, rc, err = f.Open() does not close the rc handle, causing resource leakage. 4、In the readStylesFromZipFile function, rc, err = f.Open() does not close the rc handle, causing resource leakage. 5、In the readThemeFromZipFile function, rc, err = f.Open() does not close the rc handle, causing resource leakage. 6、In the readWorkbookRelationsFromZipFile function, rc, err = workbookRels.Open() does not close the rc handle, causing resource leakage.

  • The func (f *File) Save(path string) (err error) resource in the file.go file is leaked.

    The func (f *File) Save(path string) (err error) resource in the file.go file is leaked.

    In func (f *File) Save(path string) (err error), target.Close() must be after os.Create and before f.Write. Otherwise, resource leakage may occur if f.Write(target) fails to be closed.

  • Security protection is recommended for functions OpenReaderAt and OpenFile.

    Security protection is recommended for functions OpenReaderAt and OpenFile.

    The functions OpenReaderAt and OpenFile are added to prevent bomb attack (limiting the total number of files). The restrictions on the number of sheets, size of a single file, and size of the total file are added to prevent security attacks caused by opening files with high compression ratios.

  • v3: got empty string in some cells with content

    v3: got empty string in some cells with content

    I got a strange xlsx file from one of my clients. Content of some cells in this file cannot be accessed correctly. Instead, empty strings were returned.

    However, v1 works corretly on this file. You can remove comments to see the difference.

    Here's my testing code.

    package main
    
    import (
    	"fmt"
    	"github.com/tealeg/xlsx"
    	xlsx_v3 "github.com/tealeg/xlsx/v3"
    )
    
    func GetRowCell(m map[string]int, row *xlsx.Row, index string) string {
    	val, ok := m[index]
    	if !ok {
    		return ""
    	}
    	return row.Cells[val].Value
    }
    
    func GetRowCellV3(m map[string]int, row *xlsx_v3.Row, index string) string {
    	val, ok := m[index]
    	if !ok {
    		return ""
    	}
    	return row.GetCell(val).Value
    }
    
    func main() {
    	//file, err := xlsx.OpenFile("/path/to/issue.xlsx")
    	file, err := xlsx_v3.OpenFile("/path/to/issue.xlsx")
    
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    
    	sheet := file.Sheets[0]
    	maxRow := sheet.MaxRow
    	nameCol := map[string]int{"品牌名称": 8, "备案品名": 14}
    
    	for i := 0; i < maxRow; i++ {
    		//row := sheet.Row(i)
    		//brandName := GetRowCell(nameCol, row, "品牌名称")
    		//recordName := GetRowCell(nameCol, row, "备案品名")
    
    		row, _ := sheet.Row(i)
    		brandName := GetRowCellV3(nameCol, row, "品牌名称")
    		recordName := GetRowCellV3(nameCol, row, "备案品名")
    
    		fmt.Printf("BrandName:%v RecordName:%v\n", brandName, recordName)
    	}
    }
    

    module version

    • V1: v1.0.5
    • V3: v3.2.3

    issue file issue.xlsx

Related tags
Dasel - Select, put and delete data from JSON, TOML, YAML, XML and CSV files with a single tool.
Dasel - Select, put and delete data from JSON, TOML, YAML, XML and CSV files with a single tool.

Select, put and delete data from JSON, TOML, YAML, XML and CSV files with a single tool. Supports conversion between formats and can be used as a Go package.

Jan 1, 2023
Package for indexing zip files and storing a compressed index

zipindex zipindex provides a size optimized representation of a zip file to allow decompressing the file without reading the zip file index. It will o

Nov 30, 2022
Golang string comparison and edit distance algorithms library, featuring : Levenshtein, LCS, Hamming, Damerau levenshtein (OSA and Adjacent transpositions algorithms), Jaro-Winkler, Cosine, etc...

Go-edlib : Edit distance and string comparison library Golang string comparison and edit distance algorithms library featuring : Levenshtein, LCS, Ham

Dec 20, 2022
Generates data structure definitions from JSON files for any kind of programming language

Overview Archivist generates data structure definitions from JSON files for any kind of programming language. It also provides a library for golang to

Jun 28, 2022
grep utility that searches through zip,jar,ear,tgz,bz2 in any form of nesting; it can also decompile class files

rzgrep - grep for stuff in archives that are embedded within archives This is a small utility, it greps through the contents of an archive file, it al

May 10, 2022
Golang library for querying and parsing OFX

OFXGo OFXGo is a library for querying OFX servers and/or parsing the responses. It also provides an example command-line client to demonstrate the use

Nov 25, 2022
Library for hashing any Golang interface

recursive-deep-hash Library for hashing any Golang interface Making huge struct comparison fast & easy How to use package main import ( "fmt" "git

Mar 3, 2022
A radix sorting library for Go (golang)

zermelo A radix sorting library for Go. Trade memory for speed! import "github.com/shawnsmithdev/zermelo" func foo(large []uint64) zermelo.Sort(l

Jul 30, 2022
Go native library for fast point tracking and K-Nearest queries

Geo Index Geo Index library Overview Splits the earth surface in a grid. At each cell we can store data, such as list of points, count of points, etc.

Dec 3, 2022
Data structure and algorithm library for go, designed to provide functions similar to C++ STL

GoSTL English | 简体中文 Introduction GoSTL is a data structure and algorithm library for go, designed to provide functions similar to C++ STL, but more p

Dec 26, 2022
Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers

nan - No Allocations Nevermore Package nan - Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmar

Dec 20, 2022
A feature complete and high performance multi-group Raft library in Go.
A feature complete and high performance multi-group Raft library in Go.

Dragonboat - A Multi-Group Raft library in Go / 中文版 News 2021-01-20 Dragonboat v3.3 has been released, please check CHANGELOG for all changes. 2020-03

Jan 5, 2023
Document Indexing and Searching Library in Go

Fehrist Fehrist is a pure Go library for indexing different types of documents. Currently it supports only CSV and JSON but flexible architecture give

May 22, 2022
A generic Go library for implementations of tries (radix trees), state commitments and proofs of inclusion

trie.go Go library for implementations of tries (radix trees), state commitments and proof of inclusion for large data sets. It implements a generic 2

Aug 3, 2022
Juniper is an extension to the Go standard library using generics, including containers, iterators, and streams.

Juniper Juniper is a library of extensions to the Go standard library using generics, including containers, iterators, and streams. container/tree con

Dec 25, 2022
A small flexible merge library in go
A small flexible merge library in go

conjungo A merge utility designed for flexibility and customizability. The library has a single simple point of entry that works out of the box for mo

Dec 27, 2022
A Go library for an efficient implementation of a skip list: https://godoc.org/github.com/MauriceGit/skiplist
A Go library for an efficient implementation of a skip list: https://godoc.org/github.com/MauriceGit/skiplist

Fast Skiplist Implementation This Go-library implements a very fast and efficient Skiplist that can be used as direct substitute for a balanced tree o

Dec 30, 2022
Go Library [DEPRECATED]

Tideland Go Library Description The Tideland Go Library contains a larger set of useful Google Go packages for different purposes. ATTENTION: The cell

Nov 15, 2022
an R-Tree library for Go

rtreego A library for efficiently storing and querying spatial data in the Go programming language. About The R-tree is a popular data structure for e

Jan 3, 2023