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
Golang bindings for libxlsxwriter for writing XLSX files
Golang bindings for libxlsxwriter for writing XLSX files

goxlsxwriter provides Go bindings for the libxlsxwriter C library. Install goxlsxwriter requires the libxslxwriter library to be installe

Nov 18, 2022
Golang bindings for libxlsxwriter for writing XLSX files
Golang bindings for libxlsxwriter for writing XLSX files

goxlsxwriter goxlsxwriter provides Go bindings for the libxlsxwriter C library. Install goxlsxwriter requires the libxslxwriter library to be installe

May 30, 2021
Fast and reliable way to work with Microsoft Excel™ [xlsx] files in Golang

Xlsx2Go package main import ( "github.com/plandem/xlsx" "github.com/plandem/xlsx/format/conditional" "github.com/plandem/xlsx/format/conditional/r

Dec 17, 2022
Pure go library for creating and processing Office Word (.docx), Excel (.xlsx) and Powerpoint (.pptx) documents
Pure go library for creating and processing Office Word (.docx), Excel (.xlsx) and Powerpoint (.pptx) documents

unioffice is a library for creation of Office Open XML documents (.docx, .xlsx and .pptx). Its goal is to be the most compatible and highest performan

Jan 4, 2023
go-eexcel implements encoding and decoding of XLSX like encoding/json

go-eexcel go-eexcel implements encoding and decoding of XLSX like encoding/json Usage func ExampleMarshal() { type st struct { Name string `eexce

Dec 9, 2021
A Go native tabular data extraction package. Currently supports .xls, .xlsx, .csv, .tsv formats.

grate A Go native tabular data extraction package. Currently supports .xls, .xlsx, .csv, .tsv formats. Why? Grate focuses on speed and stability first

Dec 26, 2022
Cheap/fast/simple XLSX file writer for textual data

xlsxwriter Cheap/fast/simple XLSX file writer for textual data -- no fancy formatting or graphs go get github.com/mzimmerman/xlsxwriter data := [][]s

Feb 8, 2022
A simple excel engine without ui to parse .csv files.

A simple excel engine without ui to parse .csv files.

Nov 4, 2021
Fastq demultiplexer for single cell data from MGI sequencer (10x converted library).

fastq_demultiplexer Converts fastq single cell data from MGI (10x converted library) to Illumina compatible format. Installation go install github.com

Nov 24, 2021
A simple and light excel file reader to read a standard excel as a table faster | 一个轻量级的Excel数据读取库,用一种更`关系数据库`的方式解析Excel。

Intro | 简介 Expect to create a reader library to read relate-db-like excel easily. Just like read a config. This library can read all xlsx file correct

Dec 19, 2022
Go (golang) library for reading and writing XLSX files.

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

Jan 5, 2023
Golang library for reading and writing Microsoft Excel™ (XLSX) files.
Golang library for reading and writing Microsoft Excel™ (XLSX) files.

Excelize Introduction Excelize is a library written in pure Go providing a set of functions that allow you to write to and read from XLSX / XLSM / XLT

Jan 5, 2023
Go (golang) library for reading and writing XLSX files.

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

Dec 28, 2022
Go (golang) library for reading and writing XLSX files.

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

Jan 5, 2023
Golang library for reading and writing Microsoft Excel™ (XLSX) files.
Golang library for reading and writing Microsoft Excel™ (XLSX) files.

Excelize Introduction Excelize is a library written in pure Go providing a set of functions that allow you to write to and read from XLSX / XLSM / XLT

Dec 31, 2022
Golang bindings for libxlsxwriter for writing XLSX files
Golang bindings for libxlsxwriter for writing XLSX files

goxlsxwriter provides Go bindings for the libxlsxwriter C library. Install goxlsxwriter requires the libxslxwriter library to be installe

Nov 18, 2022
Golang bindings for libxlsxwriter for writing XLSX files
Golang bindings for libxlsxwriter for writing XLSX files

goxlsxwriter goxlsxwriter provides Go bindings for the libxlsxwriter C library. Install goxlsxwriter requires the libxslxwriter library to be installe

May 30, 2021
General purpose library for reading, writing and working with OpenStreetMap data

osm This package is a general purpose library for reading, writing and working with OpenStreetMap data in Go (golang). It has the ability to read OSM

Dec 30, 2022
General purpose library for reading, writing and working with OpenStreetMap data

osm This package is a general purpose library for reading, writing and working with OpenStreetMap data in Go (golang). It has the ability to read OSM

Dec 30, 2022
Package implement reading and writing popular playlist formats: PLS, ASX, M3U, XSPF and others.

go-playlist ⚠️ WARNING The API is not stable yet and can change. Package playlist implement reading and writing popular playlist formats: PLS, ASX, M3

Oct 14, 2021