初始
This commit is contained in:
49
api/v1/common/tool/excel/excel.go
Normal file
49
api/v1/common/tool/excel/excel.go
Normal file
@ -0,0 +1,49 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/tool"
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
type Sheet struct {
|
||||
Name string `json:"name"`
|
||||
Rows [][]string `json:"rows"`
|
||||
}
|
||||
|
||||
func ReadXlsx(xlsx string) (err error, sheet []Sheet) {
|
||||
if !tool.PathExists(xlsx) {
|
||||
return errors.New("文件不存在:" + xlsx), sheet
|
||||
}
|
||||
f, err := excelize.OpenFile(xlsx)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err, sheet
|
||||
}
|
||||
defer func() {
|
||||
// 关闭工作簿
|
||||
if err := f.Close(); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}()
|
||||
list := f.GetSheetList()
|
||||
// 获取 Sheet1 上所有单元格
|
||||
for _, sheetName := range list {
|
||||
result, err := f.GetRows(sheetName)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
sheet = append(sheet, Sheet{sheetName, result})
|
||||
//rows = append(rows, result...)
|
||||
}
|
||||
|
||||
//for _, row := range rows {
|
||||
// for _, colCell := range row {
|
||||
// fmt.Print(colCell, "\t")
|
||||
// }
|
||||
// fmt.Println()
|
||||
//}
|
||||
return nil, sheet
|
||||
}
|
Reference in New Issue
Block a user