Recommend an excel wheel – minimalist usage

Time:2022-5-14

Solve the problem of Excel data

GitHub address:github.com/qax-os/excelize/tree/ma…

example

  1. install

    go get github.com/xuri/excelize/v2
  2. Simple usage: write data to excel and support multi line sheet


package main

import (
    "fmt"

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

func main() {
    f := excelize.NewFile()
    // Create a new sheet.
    index := f.NewSheet("Sheet2")
    // Set value of a cell.
    f.SetCellValue("Sheet2", "A2", "Hello world.")
    f.SetCellValue("Sheet1", "B2", 100)
    // Set active sheet of the workbook.
    f.SetActiveSheet(index)
    // Save spreadsheet by the given path.
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}
  1. Read Excel
package main

import (
    "fmt"

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

func main() {
    f, err := excelize.OpenFile("Book1.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    // Get value from cell by given worksheet name and axis.
    cell, err := f.GetCellValue("Sheet1", "B2")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(cell)
    // Get all the rows in the 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()
    }
}

How to batch data?

Give you a thought, hey hey

Converting Excel data functions

func (this *ProjectPlanController) ExportToXLSXMedicineData(ctx *gin.Context,projectId int) ([][]string, error) {
    //Query all drugs under the current project
    MedicineModel := NewMedicineModel(util.GetDbObject(ctx))
    MedicineModel.ProjectId = projectId
    MedicineList,count := MedicineModel.GetList(1,1000)

    var datas [][]string

    if count > 0 {
        Data = append (data, [] string {"drug name", "drug type", "administration method", "drug profile"})
        for _, m := range MedicineList  {
            datas = append(datas, []string{m.MedicineName, m.MedicineType, m.TakeMethod, m.Desc})
        }
    }

    return datas, nil
}

Then use the function to write to the file

Here, it’s confidential. You can play by yourself. Ha ha ha

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Thank you for your attention

Recommended Today

Simulation of M/M/1 random service queuing system in R language

Original link:http://tecdat.cn/?p=8199 Original source:Tuoduan Data Tribe Public Account In this article I construct a _discrete event_ simulation of a simple M/M/1 queue in R.  Simulation variables As usual, we start with the simulation and the variables needed for its detection. t.end <- 10^5 # 模拟的持续时间 t.clock <- 0 # 模拟时间 By &lt;- 1.3333 # arrival interval Ts <- 1.0000 # 服务期 t1 &lt;- 0 # 下一个时间的时间 t2 […]