If you code in Go, you will necessarily need this lib one day. Her name is Excelize and it allows you to manipulate Excel files directly in your programs written in Go.

It thus makes it possible to read and write Excel files of different formats (XLAM, XLSM, XLSX and XLTX) and also supports older versions 2007 and later.

Excelize thus makes it possible to process large files, therefore large volumes of files, which is super practical for your job grinders or to create various and varied processing processes. And yes, the Excel format is crap, but it’s widely used so you have to deal with it, especially in the professional world.

To install it, all you have to do is open a terminal and type the following command:

go install github.com/xuri/[email protected]

Once installed, you can then use Excelize to create new Excel files or to read existing files. Here is a sample code to demonstrate creating a new Excel file and adding a worksheet and a value to that sheet.

package main

import (
    "fmt"

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

func main() {
    f := excelize.NewFile()
    // Créer une nouvelle feuille.
    index := f.NewSheet("Sheet2")
    // Définir la valeur d'une cellule.
    f.SetCellValue("Sheet2", "A2", "Hello world.")
    f.SetCellValue("Sheet1", "B2", 100)
    // Définir la feuille active du classeur.
    f.SetActiveSheet(index)
    // Enregistrer le fichier xlsx par le chemin donné.
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

Easyyyy!

grid 0 2 1

Then, to read an Excel file, you can use the “ReadFile” method like this:

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() {
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    // Obtenir la valeur de la cellule par nom de feuille de calcul donné et axe.
    cell, err := f.GetCellValue("Sheet1", "B2")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(cell)
    // Obtenez toutes les lignes de la feuille 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()
    }
}

Practical, no?

Rate this post
Web

About the Author

SAKHRI Mohamed

Founder & Editor

Passionate about the web, new technologies and IT, I share on easy-tutorials.com tutorials, tips, advice, online tools and software for Windows, Mac and Linux. I'm the founder of this blog and I'm very interested in anything to do with technology, but I also love playing games. I was born in Constantine, but now I live in Algiers/Algeria

View All Articles