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

xurime's avatar
xurime 已提交
3 4 5 6
<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>
7
    <a href="https://pkg.go.dev/github.com/360EntSecGroup-Skylar/excelize/v2?tab=doc"><img src="https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white" alt="go.dev"></a>
xurime's avatar
xurime 已提交
8
    <a href="https://opensource.org/licenses/BSD-3-Clause"><img src="https://img.shields.io/badge/license-bsd-orange.svg" alt="Licenses"></a>
9
    <a href="https://www.paypal.com/paypalme/xuri"><img src="https://img.shields.io/badge/Donate-PayPal-green.svg" alt="Donate"></a>
xurime's avatar
xurime 已提交
10
</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

16
Excelize is a library written in pure Go providing a set of functions that allow you to write to and read from XLSX / XLSM / XLTM files. Supports reading and writing spreadsheet documents generated by Microsoft Excel&trade; 2007 and later. Supports complex components by high compatibility, and provided streaming API for generating or reading data from a worksheet with huge amounts of data. This library needs Go version 1.10 or later. The full API docs can be seen using go's built-in documentation tool, or online at [go.dev](https://pkg.go.dev/github.com/360EntSecGroup-Skylar/excelize/v2?tab=doc) 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

xurime's avatar
xurime 已提交
22
```bash
M
Matthew McFarling 已提交
23
go get github.com/360EntSecGroup-Skylar/excelize
xurime's avatar
xurime 已提交
24 25
```

26 27 28 29 30 31
- If your package management with [Go Modules](https://blog.golang.org/using-go-modules), please install with following command.

```bash
go get github.com/360EntSecGroup-Skylar/excelize/v2
```

32
### Create spreadsheet
xurime's avatar
xurime 已提交
33

34
Here is a minimal example usage that will create spreadsheet file.
xurime's avatar
xurime 已提交
35

A
Aldi Priya Perdana 已提交
36
```go
xurime's avatar
xurime 已提交
37 38
package main

xurime's avatar
xurime 已提交
39 40 41
import (
    "fmt"

42
    "github.com/360EntSecGroup-Skylar/excelize/v2"
xurime's avatar
xurime 已提交
43
)
xurime's avatar
xurime 已提交
44 45

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

61
### Reading spreadsheet
xurime's avatar
xurime 已提交
62

63
The following constitutes the bare to read a spreadsheet document.
xurime's avatar
xurime 已提交
64

A
Aldi Priya Perdana 已提交
65
```go
xurime's avatar
xurime 已提交
66 67
package main

xurime's avatar
xurime 已提交
68 69 70
import (
    "fmt"

71
    "github.com/360EntSecGroup-Skylar/excelize/v2"
xurime's avatar
xurime 已提交
72
)
xurime's avatar
xurime 已提交
73 74

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

98
### Add chart to spreadsheet file
99

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

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

xurime's avatar
xurime 已提交
104
```go
105 106
package main

xurime's avatar
xurime 已提交
107 108 109
import (
    "fmt"

110
    "github.com/360EntSecGroup-Skylar/excelize/v2"
xurime's avatar
xurime 已提交
111
)
112 113

func main() {
114 115 116 117
    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}
xurime's avatar
xurime 已提交
118
    f := excelize.NewFile()
xurime's avatar
xurime 已提交
119
    for k, v := range categories {
xurime's avatar
xurime 已提交
120
        f.SetCellValue("Sheet1", k, v)
xurime's avatar
xurime 已提交
121 122
    }
    for k, v := range values {
xurime's avatar
xurime 已提交
123 124
        f.SetCellValue("Sheet1", k, v)
    }
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    if err := f.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"
        }
    }`); err != nil {
xurime's avatar
xurime 已提交
148
        fmt.Println(err)
xurime's avatar
xurime 已提交
149
        return
xurime's avatar
xurime 已提交
150
    }
151
    // Save spreadsheet by the given path.
xurime's avatar
xurime 已提交
152
    if err := f.SaveAs("Book1.xlsx"); err != nil {
xurime's avatar
xurime 已提交
153
        fmt.Println(err)
xurime's avatar
xurime 已提交
154
    }
155 156 157
}
```

158
### Add picture to spreadsheet file
159 160 161 162 163

```go
package main

import (
xurime's avatar
xurime 已提交
164
    "fmt"
165 166 167
    _ "image/gif"
    _ "image/jpeg"
    _ "image/png"
168

169
    "github.com/360EntSecGroup-Skylar/excelize/v2"
170 171 172
)

func main() {
xurime's avatar
xurime 已提交
173
    f, err := excelize.OpenFile("Book1.xlsx")
174
    if err != nil {
xurime's avatar
xurime 已提交
175
        fmt.Println(err)
xurime's avatar
xurime 已提交
176
        return
177
    }
178
    // Insert a picture.
xurime's avatar
xurime 已提交
179
    if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil {
xurime's avatar
xurime 已提交
180
        fmt.Println(err)
181
    }
182
    // Insert a picture to worksheet with scaling.
183 184
    if err := f.AddPicture("Sheet1", "D2", "image.jpg",
        `{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {
xurime's avatar
xurime 已提交
185
        fmt.Println(err)
186
    }
187
    // Insert a picture offset in the cell with printing support.
188 189 190 191 192 193 194
    if err := f.AddPicture("Sheet1", "H2", "image.gif", `{
        "x_offset": 15,
        "y_offset": 10,
        "print_obj": true,
        "lock_aspect_ratio": false,
        "locked": false
    }`); err != nil {
xurime's avatar
xurime 已提交
195
        fmt.Println(err)
196
    }
197
    // Save the spreadsheet with the origin path.
xurime's avatar
xurime 已提交
198
    if err = f.Save(); err != nil {
xurime's avatar
xurime 已提交
199
        fmt.Println(err)
200
    }
201 202 203
}
```

xurime's avatar
xurime 已提交
204
## Contributing
xurime's avatar
xurime 已提交
205

206
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 已提交
207

xurime's avatar
xurime 已提交
208
## Licenses
xurime's avatar
xurime 已提交
209

xurime's avatar
xurime 已提交
210 211
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).

212 213 214
The Excel logo is a trademark of [Microsoft Corporation](https://aka.ms/trademarks-usage). This artwork is an adaptation.

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/).