README.md 6.5 KB
Newer Older
xurime's avatar
xurime 已提交
1
<p align="center"><img width="650" src="./excelize.png" alt="Excelize logo"></p>
xurime's avatar
xurime 已提交
2

xurime's avatar
xurime 已提交
3 4 5 6 7 8 9 10
<p align="center">
    <a href="https://travis-ci.org/360EntSecGroup-Skylar/excelize"><img src="https://travis-ci.org/360EntSecGroup-Skylar/excelize.svg?branch=master" alt="Build Status"></a>
    <a href="https://codecov.io/gh/360EntSecGroup-Skylar/excelize"><img src="https://codecov.io/gh/360EntSecGroup-Skylar/excelize/branch/master/graph/badge.svg" alt="Code Coverage"></a>
    <a href="https://goreportcard.com/report/github.com/360EntSecGroup-Skylar/excelize"><img src="https://goreportcard.com/badge/github.com/360EntSecGroup-Skylar/excelize" alt="Go Report Card"></a>
    <a href="https://godoc.org/github.com/360EntSecGroup-Skylar/excelize"><img src="https://godoc.org/github.com/360EntSecGroup-Skylar/excelize?status.svg" alt="GoDoc"></a>
    <a href="https://opensource.org/licenses/BSD-3-Clause"><img src="https://img.shields.io/badge/license-bsd-orange.svg" alt="Licenses"></a>
    <a href="https://www.paypal.me/xuri"><img src="https://img.shields.io/badge/Donate-PayPal-green.svg" alt="Donate"></a>
</p>
xurime's avatar
xurime 已提交
11

xurime's avatar
xurime 已提交
12
# Excelize
xurime's avatar
xurime 已提交
13

xurime's avatar
xurime 已提交
14
## Introduction
xurime's avatar
xurime 已提交
15

xurime's avatar
xurime 已提交
16
Excelize is a library written in pure Go 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 Microsoft Excel&trade; 2007 and later. Support save file without losing original charts of XLSX. This library needs Go version 1.8 or later. The full API docs can be seen using go's built-in documentation tool, or online at [godoc.org](https://godoc.org/github.com/360EntSecGroup-Skylar/excelize) and [docs reference](https://xuri.me/excelize/).
xurime's avatar
xurime 已提交
17

xurime's avatar
xurime 已提交
18
## Basic Usage
xurime's avatar
xurime 已提交
19

xurime's avatar
xurime 已提交
20
### Installation
xurime's avatar
xurime 已提交
21

A
Aldi Priya Perdana 已提交
22
```go
23
go get github.com/360EntSecGroup-Skylar/excelize
xurime's avatar
xurime 已提交
24 25
```

xurime's avatar
xurime 已提交
26
### Create XLSX file
xurime's avatar
xurime 已提交
27 28 29

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

A
Aldi Priya Perdana 已提交
30
```go
xurime's avatar
xurime 已提交
31 32 33 34
package main

import (
    "fmt"
xurime's avatar
xurime 已提交
35

36
    "github.com/360EntSecGroup-Skylar/excelize"
xurime's avatar
xurime 已提交
37 38 39
)

func main() {
40
    xlsx := excelize.NewFile()
xurime's avatar
xurime 已提交
41
    // Create a new sheet.
42
    index := xlsx.NewSheet("Sheet2")
43
    // Set value of a cell.
xurime's avatar
xurime 已提交
44 45
    xlsx.SetCellValue("Sheet2", "A2", "Hello world.")
    xlsx.SetCellValue("Sheet1", "B2", 100)
46
    // Set active sheet of the workbook.
47
    xlsx.SetActiveSheet(index)
xurime's avatar
xurime 已提交
48
    // Save xlsx file by the given path.
xurime's avatar
xurime 已提交
49
    err := xlsx.SaveAs("./Book1.xlsx")
xurime's avatar
xurime 已提交
50 51 52 53 54 55
    if err != nil {
        fmt.Println(err)
    }
}
```

xurime's avatar
xurime 已提交
56
### Reading XLSX file
xurime's avatar
xurime 已提交
57

xurime's avatar
xurime 已提交
58
The following constitutes the bare to read a XLSX document.
xurime's avatar
xurime 已提交
59

A
Aldi Priya Perdana 已提交
60
```go
xurime's avatar
xurime 已提交
61 62 63 64
package main

import (
    "fmt"
xurime's avatar
xurime 已提交
65

66
    "github.com/360EntSecGroup-Skylar/excelize"
xurime's avatar
xurime 已提交
67 68 69
)

func main() {
xurime's avatar
xurime 已提交
70
    xlsx, err := excelize.OpenFile("./Book1.xlsx")
71 72
    if err != nil {
        fmt.Println(err)
xurime's avatar
xurime 已提交
73
        return
74
    }
75
    // Get value from cell by given worksheet name and axis.
xurime's avatar
xurime 已提交
76 77
    cell := xlsx.GetCellValue("Sheet1", "B2")
    fmt.Println(cell)
78 79
    // Get all the rows in the Sheet1.
    rows := xlsx.GetRows("Sheet1")
xurime's avatar
xurime 已提交
80 81 82 83
    for _, row := range rows {
        for _, colCell := range row {
            fmt.Print(colCell, "\t")
        }
84
        fmt.Println()
85
    }
xurime's avatar
xurime 已提交
86 87 88
}
```

89 90
### Add chart to XLSX file

91
With Excelize chart generation and management is as easy as a few lines of code. You can build charts based off data in your worksheet or generate charts without any data in your worksheet at all.
92

xurime's avatar
xurime 已提交
93
<p align="center"><img width="650" src="./test/images/chart.png" alt="Excelize"></p>
94

xurime's avatar
xurime 已提交
95
```go
96 97 98
package main

import (
xurime's avatar
xurime 已提交
99
    "fmt"
100

xurime's avatar
xurime 已提交
101
    "github.com/360EntSecGroup-Skylar/excelize"
102 103 104
)

func main() {
xurime's avatar
xurime 已提交
105 106 107 108 109 110 111 112 113
    categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
    values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
    xlsx := excelize.NewFile()
    for k, v := range categories {
        xlsx.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        xlsx.SetCellValue("Sheet1", k, v)
    }
114
    xlsx.AddChart("Sheet1", "E1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`)
xurime's avatar
xurime 已提交
115 116 117 118 119
    // Save xlsx file by the given path.
    err := xlsx.SaveAs("./Book1.xlsx")
    if err != nil {
        fmt.Println(err)
    }
120
}
xurime's avatar
xurime 已提交
121

122 123
```

xurime's avatar
xurime 已提交
124
### Add picture to XLSX file
125 126 127 128 129 130

```go
package main

import (
    "fmt"
131 132 133
    _ "image/gif"
    _ "image/jpeg"
    _ "image/png"
134

135
    "github.com/360EntSecGroup-Skylar/excelize"
136 137 138
)

func main() {
xurime's avatar
xurime 已提交
139
    xlsx, err := excelize.OpenFile("./Book1.xlsx")
140 141
    if err != nil {
        fmt.Println(err)
xurime's avatar
xurime 已提交
142
        return
143
    }
144
    // Insert a picture.
145
    err = xlsx.AddPicture("Sheet1", "A2", "./image1.png", "")
146 147 148
    if err != nil {
        fmt.Println(err)
    }
149
    // Insert a picture to worksheet with scaling.
150
    err = xlsx.AddPicture("Sheet1", "D2", "./image2.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`)
151 152 153
    if err != nil {
        fmt.Println(err)
    }
154
    // Insert a picture offset in the cell with printing support.
155
    err = xlsx.AddPicture("Sheet1", "H2", "./image3.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`)
156 157 158
    if err != nil {
        fmt.Println(err)
    }
159 160
    // Save the xlsx file with the origin path.
    err = xlsx.Save()
161 162 163
    if err != nil {
        fmt.Println(err)
    }
164 165 166
}
```

xurime's avatar
xurime 已提交
167
## Contributing
xurime's avatar
xurime 已提交
168

169
Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change. XML is compliant with [part 1 of the 5th edition of the ECMA-376 Standard for Office Open XML](http://www.ecma-international.org/publications/standards/Ecma-376.htm).
xurime's avatar
xurime 已提交
170

xurime's avatar
xurime 已提交
171
## Licenses
xurime's avatar
xurime 已提交
172

xurime's avatar
xurime 已提交
173 174
This program is under the terms of the BSD 3-Clause License. See [https://opensource.org/licenses/BSD-3-Clause](https://opensource.org/licenses/BSD-3-Clause).

175 176 177 178 179 180
The Excel logo is a trademark of [Microsoft Corporation](https://aka.ms/trademarks-usage). This artwork is an adaptation.

Some struct of XML originally by [tealeg/xlsx](https://github.com/tealeg/xlsx). Licensed under the [BSD 3-Clause License](https://github.com/tealeg/xlsx/blob/master/LICENSE).

gopher.{ai,svg,png} was created by [Takuya Ueda](https://twitter.com/tenntenn). Licensed under the [Creative Commons 3.0 Attributions license](http://creativecommons.org/licenses/by/3.0/).

xurime's avatar
xurime 已提交
181
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2F360EntSecGroup-Skylar%2Fexcelize.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2F360EntSecGroup-Skylar%2Fexcelize?ref=badge_large)