SoFunction
Updated on 2025-03-04

Detailed explanation of the excel class library of Go language operation Excel tools

Preface

Some requirements in development need to be operated through programsexcelDocuments, such as exportexcel, importexcel,TowardsexcelInsert information such as pictures, tables, and charts into the document, and useExcelizeThis can easily meet the above needs. This article mainly summarizes the use of Excelize, hoping it will be helpful to everyone. If there are any mistakes or no complete considerations, I would like to give you advice.

Introduction to Excelize

Excelize is a library written in Go for operating Office Excel document. It can be used to read and write Excel files. It also supports inserting pictures, icons, tool functions, etc. into Excel. It has relatively complete functions and is sufficient for basic needs. Without saying much nonsense, just do it.

Install

go get /xuri/excelize
# If you are using a Go Module managed package, perform the following installationgo get /xuri/excelize/v2

Export Excel Documents

package main

import (
    "fmt"

    "/xuri/excelize/v2"
)

func main() {
    f := ()
    // Create a worksheet    index := ("Sheet2")
    // Set the value of the cell    ("Sheet2", "A2", "Hello world.")
    ("Sheet1", "B2", 100)
    // Set the default worksheet for the workbook    (index)
    //Save the file according to the specified path    if err := (""); err != nil {
        (err)
    }
}

Read Excel Documents

package main

import (
    "fmt"

    "/xuri/excelize/v2"
)

func main() {
    f, err := ("")
    if err != nil {
        (err)
        return
    }
    defer func() {
        if err := (); err != nil {
            (err)
        }
    }()
    // Get the value of the specified cell in the worksheet    cell, err := ("Sheet1", "B2")
    if err != nil {
        (err)
        return
    }
    (cell)
    // Get all cells on Sheet1    rows, err := ("Sheet1")
    if err != nil {
        (err)
        return
    }
    for _, row := range rows {
        for _, colCell := range row {
            (colCell, "\t")
        }
        ()
    }
}

summary

In this article, we briefly introduce the use of Go to operate Excel through Excel. Excelize is a basic library written in Go for operating Office Excel documents. It can be used to read and write Excel documents. It also supports inserting pictures, icons, and tool functions into Excel. It has relatively complete functions and is fully sufficient for basic needs.

There is a Chinese document on the Internet that introduces the use of excelize. The summary is quite comprehensive. Address:/excelize/zh…

This is the article about the excelize library, a tool for Go operation Excel. For more information about Go operation Excel, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!