go excel文件处理
go excel操作
package util import ( "github.com/extrame/xls" "github.com/tealeg/xlsx" "reflect" "strings" ) // ReadXlsx xlsx解析 func ReadXlsx(sheet *xlsx.Sheet) (res [][]string) { temp := make([][]string, len(sheet.Rows)) for k, row := range sheet.Rows { var data []string for _, cell := range row.Cells { if strings.Index(cell.NumFmt, "yy") > -1 { rv := reflect.ValueOf(*cell) date1904 := rv.FieldByName("date1904").Bool() dateTime, timeErr := cell.GetTime(date1904) if timeErr != nil { data = append(data, cell.Value) } else { data = append(data, dateTime.Format("2006-01-02 15:04:05")) } } else { data = append(data, cell.Value) } } temp[k] = data } res = append(res, temp...) return res } // ReadXls xls解析 func ReadXls(sheet *xls.WorkSheet) (res [][]string) { if sheet.MaxRow == 0 { return res } temp := make([][]string, sheet.MaxRow+1) for i := 0; i <= int(sheet.MaxRow); i++ { row := sheet.Row(i) data := make([]string, 0) if row.LastCol() > 0 { for j := 0; j < row.LastCol(); j++ { col := row.Col(j) data = append(data, col) } temp[i] = data } } res = append(res, temp...) return res } // ExecList2map list转map func ExecList2map(res [][]string) (result []map[string]string) { if len(res) < 2 { return result } //第一行为标题 title := res[0] titleMap := make(map[int]string) for index, val := range title { if val != "" { titleMap[index] = val } } for i := 1; i < len(res); i++ { row := res[i] rowMap := make(map[string]string) for index, val := range row { if titleMap[index] != "" { sVal := strings.ToLower(val) // 处理Excel网址超链接 if strings.Index(sVal, "(http") > -1 { sCol := strings.Split(sVal, "(http") val = sCol[0] } // 处理Excel邮箱超链接 if strings.Index(sVal, "(mailto") > -1 { sCol := strings.Split(sVal, "(mailto") val = sCol[0] } rowMap[titleMap[index]] = val } } result = append(result, rowMap) } return result }
分类:
go/golang
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2015-08-17 oracle自动执行一个sql文件的脚本