[Open Source] A basic library written in Go for manipulating Office Excel documents, simple and easy to use

Open SourceProcess Management
2026-06-28 03:47 4 次浏览 • 0 次下载 0KB
[Open Source] A basic library written in Go for manipulating Office Excel documents, simple and easy to use

1. Project Overview

Excelize is a foundational library written in Go for manipulating Office Excel documents, based on the ECMA-376 and ISO/IEC 29500 international standards. It can be used to read and write spreadsheet documents created by Microsoft Excel™ 2007 and later versions. It supports multiple document formats, including XLAM / XLSM / XLSX / XLTM / XLTX, and offers high compatibility with documents containing complex components such as styles, images, pivot tables, and slicers. It also provides streaming read and write APIs for processing workbooks containing large amounts of data. It can be applied to various systems, including reporting platforms, cloud computing, and edge computing. This library requires Go version 1.16 or later.

2. Open Source License

Released under the BSD-3-Clause open-source license

3. Screenshots







4. Feature Overview

Project Mission

Excelize aims to create and maintain a Go implementation of an Excel document API for processing spreadsheet documents that comply with the Office Open XML (OOXML) standard. With Excelize, you can use Go to read and write MS Excel files.

Why Use Excelize

In some situations, we need to manipulate Excel documents programmatically. For example: opening and reading existing Excel documents, creating new Excel documents, generating new Excel documents based on existing documents (templates), inserting elements such as images, charts, and tables into Excel documents, and sometimes implementing these operations across platforms. Excelize makes it easy to meet all these requirements.

5. Tech Stack

Quick start

Installation


go get github.com/xuri/excelize
  • If you use Go Modules to manage packages, run the following command to install the latest version.

go get github.com/xuri/excelize/v2

Creating an Excel Document

Here is a simple example of creating an Excel document:


package main

import (
    "fmt"

    "github.com/xuri/excelize/v2"
)

func main() {
    f := excelize.NewFile()
    defer func() {
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    // Create a worksheet
    index, err := f.NewSheet("Sheet2")
    if err != nil {
        fmt.Println(err)
        return
    }
    // Set cell values
    f.SetCellValue("Sheet2", "A2", "Hello world.")
    f.SetCellValue("Sheet1", "B2", 100)
    // Set the default worksheet of the workbook
    f.SetActiveSheet(index)
    // Save the file to the specified path
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}
Read an Excel Document
Here is an example of reading an Excel document:
package main

import (
    "fmt"

    "github.com/xuri/excelize/v2"
)

func main() {
    f, err := excelize.OpenFile("Book1.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer func() {
        // Close the workbook
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    // Get the value of a specified cell in the worksheet
    cell, err := f.GetCellValue("Sheet1", "B2")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(cell)
    // Get all cells in Sheet1
    rows, err := f.GetRows("Sheet1")
    if err != nil {
        fmt.Println(err)
        return
    }
    for _, row := range rows {
        for _, colCell := range row {
            fmt.Print(colCell, "\t")
        }
        fmt.Println()
    }
}

Create Charts in Excel Documents

Creating charts with Excelize is very simple and requires only a few lines of code. You can build charts using existing data in a worksheet, or add data to a worksheet and then create charts.



package main

import (
    "fmt"

    "github.com/xuri/excelize/v2"
)

func main() {
    f := excelize.NewFile()
    defer func() {
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    for idx, row := range [][]interface{}{
        {nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3},
        {"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
    } {
        cell, err := excelize.CoordinatesToCellName(1, idx+1)
        if err != nil {
            fmt.Println(err)
            return
        }
        f.SetSheetRow("Sheet1", cell, &row)
    }
    if err := f.AddChart("Sheet1", "E1", &excelize.Chart{
        Type: excelize.Col3DClustered,
        Series: []excelize.ChartSeries{
            {
                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: excelize.ChartTitle{
            Name: "Fruit 3D Clustered Column Chart",
        },
    }); err != nil {
        fmt.Println(err)
        return
    }
    // Save the file to the specified path
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

Insert Images into Excel Documents


package main

import (
    "fmt"
    _ "image/gif"
    _ "image/jpeg"
    _ "image/png"

    "github.com/xuri/excelize/v2"
)

func main() {
    f, err := excelize.OpenFile("Book1.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer func() {
        // Close the workbook
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    // Insert an image
    if err := f.AddPicture("Sheet1", "A2", "image.png", nil); err != nil {
        fmt.Println(err)
    }
    // Insert an image into the worksheet and set its scaling ratio
    if err := f.AddPicture("Sheet1", "D2", "image.jpg",
        &excelize.GraphicOptions{ScaleX: 0.5, ScaleY: 0.5}); err != nil {
        fmt.Println(err)
    }
    // Insert an image into the worksheet and set its print properties
    enable, disable := true, false
    if err := f.AddPicture("Sheet1", "H2", "image.gif",
        &excelize.GraphicOptions{
            PrintObject:     &enable,
            LockAspectRatio: false,
            OffsetX:         15,
            OffsetY:         10,
            Locked:          &disable,
        }); err != nil {
        fmt.Println(err)
    }
    // Save the workbook
    if err = f.Save(); err != nil {
        fmt.Println(err)
    }
}

#1
资源直链https://github.com/qax-os/excelize/archive/refs/heads/master.zip
去下载
星河码客

发现和分享优质开源项目,传承互联网精神,为开发者服务。

关于本站

联系我们

微信/广告合作:ITcloub (备注星河码客)

免责声明:本站资源版权归原作者所有,仅供学习与交流。如有侵权,请通过「关于我们 - 侵权投诉」联系删除。

Powered by Nuxt · Deployed on Cloudflare