excelize

package module
v0.0.0-...-6adcb9d Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2016 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Excelize

Excelize

Build Status Code Coverage Go Report Card GoDoc Licenses

Introduction

Excelize is a library written in pure Golang and providing a set of functions that allow you to write to and read from XLSX files. Support reads and writes XLSX file generated by Office Excel 2007 and later. Support save file without losing original charts of XLSX. The full API docs can be seen using go's built-in documentation tool, or online at godoc.org.

Basic Usage

Installation
go get github.com/Luxurioust/excelize
Create XLSX files

Here is a minimal example usage that will create XLSX file.

package main

import (
    "fmt"
    "os"

    "github.com/Luxurioust/excelize"
)

func main() {
    xlsx := excelize.CreateFile()
    xlsx.NewSheet(2, "Sheet2")
    xlsx.NewSheet(3, "Sheet3")
    xlsx.SetCellInt("Sheet2", "A23", 10)
    xlsx.SetCellStr("Sheet3", "B20", "Hello")
    err := xlsx.WriteTo("/tmp/Workbook.xlsx")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}
Writing XLSX files

The following constitutes the bare minimum required to write an XLSX document.

package main

import (
    "fmt"
    "os"

    "github.com/Luxurioust/excelize"
)

func main() {
    xlsx, err := excelize.OpenFile("/tmp/Workbook.xlsx")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    xlsx.SetCellValue("Sheet2", "B2", 100)
    xlsx.SetCellValue("Sheet2", "C7", "Hello")
    xlsx.NewSheet(4, "TestSheet")
    xlsx.SetCellInt("Sheet4", "A3", 10)
    xlsx.SetCellStr("Sheet4", "b6", "World")
    xlsx.SetActiveSheet(2)
    err = xlsx.Save()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}
Reading XLSX files
package main

import (
    "fmt"
    "os"

    "github.com/Luxurioust/excelize"
)

func main() {
    xlsx, err := excelize.OpenFile("/tmp/Workbook.xlsx")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    cell := xlsx.GetCellValue("Sheet2", "C7")
    fmt.Println(cell)
}

Contributing

Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change.

Credits

Some struct of XML originally by tealeg/xlsx.

Licenses

This program is under the terms of the BSD 3-Clause License. See https://opensource.org/licenses/BSD-3-Clause.

Documentation

Index

Constants

View Source
const XMLHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"

XMLHeader define an XML declaration can also contain a standalone declaration.

Variables

This section is empty.

Functions

func ReadZip

func ReadZip(f *zip.ReadCloser) (map[string]string, int, error)

ReadZip takes a pointer to a zip.ReadCloser and returns a xlsx.File struct populated with its contents. In most cases ReadZip is not used directly, but is called internally by OpenFile.

func ReadZipReader

func ReadZipReader(r *zip.Reader) (map[string]string, int, error)

ReadZipReader can be used to read an XLSX in memory without touching the filesystem.

Types

type File

type File struct {
	XLSX       map[string]string
	Path       string
	SheetCount int
}

File define a populated xlsx.File struct.

func CreateFile

func CreateFile() *File

CreateFile provide function to create new file by default template. For example: xlsx := CreateFile()

func OpenFile

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

OpenFile take the name of an XLSX file and returns a populated xlsx.File struct for it.

func (*File) GetCellValue

func (f *File) GetCellValue(sheet string, axis string) string

GetCellValue provide function get value from cell by given sheet index and axis in XLSX file.

func (*File) GetRows

func (f *File) GetRows(sheet string) [][]string

GetRows return all the rows in a sheet, for example:

rows := xlsx.GetRows("Sheet2")
for _, row := range rows {
    for _, colCell := range row {
        fmt.Print(colCell, "\t")
    }
    fmt.Println()
}

func (*File) NewSheet

func (f *File) NewSheet(index int, name string)

NewSheet provice function to greate a new sheet by given index, when creating a new XLSX file, the default sheet will be create, when you create a new file, you need to ensure that the index is continuous.

func (*File) Save

func (f *File) Save() error

Save provide function override the xlsx file with origin path.

func (*File) SetActiveSheet

func (f *File) SetActiveSheet(index int)

SetActiveSheet provide function to set default active sheet of XLSX by given index.

func (*File) SetCellInt

func (f *File) SetCellInt(sheet string, axis string, value int)

SetCellInt provide function to set int type value of a cell.

func (*File) SetCellStr

func (f *File) SetCellStr(sheet string, axis string, value string)

SetCellStr provide function to set string type value of a cell.

func (*File) SetCellValue

func (f *File) SetCellValue(sheet string, axis string, value interface{})

SetCellValue provide function to set int or string type value of a cell.

func (*File) UpdateLinkedValue

func (f *File) UpdateLinkedValue()

UpdateLinkedValue fix linked values within a spreadsheet are not updating in Office Excel 2007 and 2010. This function will be remove value tag when met a cell have a linked value. Reference https://social.technet.microsoft.com/Forums/office/en-US/e16bae1f-6a2c-4325-8013-e989a3479066/excel-2010-linked-cells-not-updating?forum=excel

Notice: after open XLSX file Excel will be update linked value and generate new value and will prompt save file or not.

For example:

<row r="19" spans="2:2">
    <c r="B19">
        <f>SUM(Sheet2!D2,Sheet2!D11)</f>
        <v>100</v>
     </c>
</row>

to

<row r="19" spans="2:2">
    <c r="B19">
        <f>SUM(Sheet2!D2,Sheet2!D11)</f>
    </c>
</row>

func (*File) WriteTo

func (f *File) WriteTo(name string) error

WriteTo provide function create or update to an xlsx file at the provided path.

Jump to

Keyboard shortcuts

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