解決excel 資料問題
github地址: github.com/qax-os/excelize/tree/ma...
例項
安裝
go get github.com/xuri/excelize/v2
簡單用法 寫入資料到excel,支援多行 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)
}
}
- 讀取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()
}
}
批量的資料如何 批量呢?
給你們一個思路,嘿嘿嘿
轉換excel 資料函式
func (this *ProjectPlanController) ExportToXLSXMedicineData(ctx *gin.Context,projectId int) ([][]string, error) {
// 查詢出 當前專案下 所有的藥物
MedicineModel := NewMedicineModel(util.GetDbObject(ctx))
MedicineModel.ProjectId = projectId
MedicineList,count := MedicineModel.GetList(1,1000)
var datas [][]string
if count > 0 {
datas = append(datas, []string{"藥物名稱","藥物型別", "給藥方式", "藥物簡介"})
for _, m := range MedicineList {
datas = append(datas, []string{m.MedicineName, m.MedicineType, m.TakeMethod, m.Desc})
}
}
return datas, nil
}
再使用函式來進行寫入檔案
此處,保密,你自己玩哈哈哈哈
本作品採用《CC 協議》,轉載必須註明作者和本文連結