123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package sxlsx
- import (
- . "createindex/base"
- "log"
- "github.com/xuri/excelize/v2"
- )
- //源端读取对象,excel
- type (
- SXlsx struct {
- File string
- Sheet string
- //Fields []string //同步字段,这空默认全部
- Mfields map[string]string //字段映射
- Min int //最小行
- Max int //最大行
- }
- )
- func GetXlsx(file, sheet string, mfields map[string]string, min, max int) *SXlsx {
- return &SXlsx{
- File: file,
- Sheet: sheet,
- //Fields: fields,
- Mfields: mfields,
- Min: min,
- Max: max,
- }
- }
- func (m *SXlsx) Find(d Direct) {
- f, err := excelize.OpenFile(m.File)
- if err != nil {
- log.Println(err)
- return
- }
- defer Catch()
- rows, err := f.GetRows(m.Sheet)
- if err != nil {
- log.Println(err)
- return
- }
- //第一行表头,获取同步字段
- syncFileds := map[int]string{}
- lenRow := len(rows) - 2
- if lenRow > 2 {
- //if len(m.Fields) == 0 {
- //表头第二行
- for index, cell := range rows[1] {
- if len(m.Mfields) == 0 {
- syncFileds[index] = cell
- } else {
- fs := m.Mfields[cell]
- if fs != "" {
- syncFileds[index] = fs
- }
- }
- }
- //}
- } else {
- log.Println(m.File, "无数据")
- }
- //获取数据范围
- // max := m.Max
- // if m.Min > 0 && m.Max > m.Min {
- // if m.Min > lenRow {
- // log.Println(m.File, "数据索引超出范围", m.Min, m.Max, lenRow)
- // } else {
- // if m.Max > lenRow {
- // max = lenRow
- // }
- // rows = rows[m.Min:max]
- // }
- // }
- // 读取数据
- for _, row := range rows[2:] {
- data := map[string]any{}
- for index, colCell := range row {
- fs := syncFileds[index]
- if fs != "" && colCell != "" {
- f, d := ParseData(fs, colCell)
- data[f] = d
- }
- }
- //发送数据
- d.Send(data)
- }
- //数据遍历完成
- d.End()
- }
|