Home > Backend Development > Golang > golang implements excel reading and writing

golang implements excel reading and writing

王林
Release: 2023-05-13 10:04:37
Original
3512 people have browsed it

As the demand for data processing becomes more and more important, data in Excel tables has gradually become an indispensable part of daily work and life. In the Golang programming language, there are also excellent libraries that can easily read and write Excel files. This article will take you step by step to read and write the Golang version of Excel files.

  1. Install the Excel processing library

To implement the read and write operations of Excel files, we need to use the third-party Golang library, go-excelize. Run the following command to install the library:

go get github.com/360EntSecGroup-Skylar/excelize
Copy after login
  1. Read Excel File

First, we will open an Excel file named "test.xlsx" and put Sheet1 is used as the table that needs to be read.

f, err := excelize.OpenFile("./test.xlsx")
if err != nil {
    fmt.Println(err)
    return
}
// 取得 Sheet1 表格中所有的行
rows, err := f.GetRows("Sheet1")
if err != nil {
    fmt.Println(err)
    return
}
Copy after login

Through the GetRows method, we can easily get all the rows in the Sheet1 table. Next, we can loop through each row of data and print out the value of each cell.

for i, row := range rows {
    for j, colCell := range row {
        fmt.Printf("(%d,%d) %s
", i+1, j+1, colCell)
    }
}
Copy after login
  1. Writing an Excel file

Similar to reading an Excel file, we first open an Excel file named "write.xlsx" and select the file to be written The entered form is Sheet1.

f := excelize.NewFile()
// 创建一个名为 Sheet1 的表格
sheetIndex := f.NewSheet("Sheet1")
// 设置 Sheet1 表格为当前操作表格
f.SetActiveSheet(sheetIndex)
Copy after login

Now, we have created a new Excel file and added a table named Sheet1. Next, we can write the required data into the table.

// 向 A1 单元格写入内容
f.SetCellValue("Sheet1", "A1", "姓名")
f.SetCellValue("Sheet1", "B1", "年龄")
f.SetCellValue("Sheet1", "C1", "性别")

// 写入数据行
f.SetCellValue("Sheet1", "A2", "张三")
f.SetCellValue("Sheet1", "B2", 26)
f.SetCellValue("Sheet1", "C2", "男")

f.SetCellValue("Sheet1", "A3", "李四")
f.SetCellValue("Sheet1", "B3", 28)
f.SetCellValue("Sheet1", "C3", "男")

f.SetCellValue("Sheet1", "A4", "小花")
f.SetCellValue("Sheet1", "B4", 24)
f.SetCellValue("Sheet1", "C4", "女")
Copy after login

Through the above code, we have successfully written data into the Excel table. Finally, we need to save the file to ensure that the data can be persisted.

err := f.SaveAs("./write.xlsx")
if err != nil {
    fmt.Println(err)
}
Copy after login
  1. Complete code

Note: This sample code is for demonstration purposes only. The table format used for reading and writing files needs to be adjusted according to the actual situation.

The complete code is as follows:

package main

import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize"
)

func main() {
    // 读取 Excel 文件
    f, err := excelize.OpenFile("./test.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    // 取得 Sheet1 表格中所有的行
    rows, err := f.GetRows("Sheet1")
    if err != nil {
        fmt.Println(err)
        return
    }
    // 遍历所有单元格的数据
    for i, row := range rows {
        for j, colCell := range row {
            fmt.Printf("(%d,%d) %s
", i+1, j+1, colCell)
        }
    }

    // 创建 Excel 文件
    f := excelize.NewFile()
    // 创建一个名为 Sheet1 的表格
    sheetIndex := f.NewSheet("Sheet1")
    // 设置 Sheet1 表格为当前操作表格
    f.SetActiveSheet(sheetIndex)

    // 向 A1 单元格写入内容
    f.SetCellValue("Sheet1", "A1", "姓名")
    f.SetCellValue("Sheet1", "B1", "年龄")
    f.SetCellValue("Sheet1", "C1", "性别")

    // 写入数据行
    f.SetCellValue("Sheet1", "A2", "张三")
    f.SetCellValue("Sheet1", "B2", 26)
    f.SetCellValue("Sheet1", "C2", "男")

    f.SetCellValue("Sheet1", "A3", "李四")
    f.SetCellValue("Sheet1", "B3", 28)
    f.SetCellValue("Sheet1", "C3", "男")

    f.SetCellValue("Sheet1", "A4", "小花")
    f.SetCellValue("Sheet1", "B4", 24)
    f.SetCellValue("Sheet1", "C4", "女")

    // 保存 Excel 文件
    err := f.SaveAs("./write.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}
Copy after login
  1. Summary

Through the above code demonstration, we can see that using Golang to implement reading and writing operations of Excel files Very simple and efficient. go-excelize is a powerful library in the Golang community. It provides many functions to process Excel files and can be integrated with other libraries. Therefore, Golang has become one of the very good choices for processing Excel files. I hope it can help everyone.

The above is the detailed content of golang implements excel reading and writing. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template