需求分析
需求:如何自动识别excel中的时间类型数据并转化成对应的 "y-m-d h:i:s"类型数据。
分析:excelize在读取excel时,getrows() 返回的都是字符串类型,并且有些时间类型的数据会进行转换,如果全部转化成 float64 格式,然后转换成对应的字符串,并且excelize提供函数
func exceldatetotime(exceldate float64, use1904format bool) (time.time, error) { ... }
可以将float64 转换成time.time 类型,time.time 很容易转化成对应"y-m-d h:i:s"格式字符串类型数据。所以我们的难点就在于如何自动识别excel中时日期时间类型数据
excel 单元格格式
以下3月1日数据写入到excel中,excel都会识别成2024/3/1,但是对应单元格格式不同,转化为常规类型的话大部分都相同
- 2024年3月1日------------- yyyy"年"m"月"d"日"-------------453352
- 2024/3/1------------- yyyy/m/d-------------453352
- mar-24------------- mmm-yy-------------453352
- 2024年3月------------- yyyy"年"m"月"-------------453352
- 2024/3/1 0:00-------------yyyy/m/d h:mm-------------453352
- '2024-03-01 00:00:00-------------通用-------------2024-03-01 00:00:00
excelize 读取
func parsefileurl(filepath string) ([]map[string]string, error) { f, err := excelize.openfile(filepath) if err != nil { return nil, err } sheetname := f.getsheetname(0) rows, err := f.getrows(sheetname) if err != nil { return nil, err } if len(rows) > 0 { for rowkey, cols := range rows { if len(cols) > 0 { for colkey, value := range cols { fmt.println(rowkey, "-", colkey, ":", value) } } } } return nil, err }
结果打印
0 - 0 : 45352 1 - 0 : 03-01-24 2 - 0 : mar-24 3 - 0 : 45352 4 - 0 : 3/1/24 00:00 5 - 0 : 2024-03-01 00:00:00
由此我们可以看出时间类型打印出来的内容不尽相同,这里我们可以想办法把他们全部转化成45352
这里我们就把他们转化成常规类型,常规类型的数据大部分都为45352
func parsefileurl(filepath string) ([]map[string]string, error) { f, err := excelize.openfile(filepath) if err != nil { return nil, err } sheetname := f.getsheetname(0) rows, err := f.getrows(sheetname) if err != nil { return nil, err } //转化为常规类型,对应style numfmt 0 styleid, _ := f.newstyle(&excelize.style{numfmt: 0}) //示例例中都放入a列,所以将a列数据全部转化成对应的常规类型 _ = f.setcolstyle(sheetname, "a", styleid) rows, err = f.getrows(sheetname) if len(rows) > 0 { for rowkey, cols := range rows { if len(cols) > 0 { for colkey, value := range cols { fmt.println(rowkey, "-", colkey, ":", value) } } } } return nil, err }
再次进行打印
0 - 0 : 45352 1 - 0 : 45352 2 - 0 : 45352 3 - 0 : 45352 4 - 0 : 45352 5 - 0 : 2024-03-01 00:00:00
这时我们就可以看到大部分数据都已经转化成了45352,所以接下来很简单将数据转化成float64类型,再转化成time.time类型,最后转化成我们想要的数据类型
func parsefileurl(filepath string) ([]map[string]string, error) { f, err := excelize.openfile(filepath) if err != nil { return nil, err } sheetname := f.getsheetname(0) rows, err := f.getrows(sheetname) if err != nil { return nil, err } styleid, _ := f.newstyle(&excelize.style{numfmt: 0}) _ = f.setcolstyle(sheetname, "a", styleid) rows, err = f.getrows(sheetname) if len(rows) > 0 { for rowkey, cols := range rows { if len(cols) > 0 { for colkey, value := range cols { timefloat, err := strconv.parsefloat(value, 64) if err != nil { //err 说明无法转化成float64 那么有可能本身是字符串时间进行返回 timetime, err := time.parse("2006-01-02 15:04:05", value) if err != nil { fmt.println(rowkey, "-", colkey, ":", value) } else { value = timetime.format("2006-01-02 15:04:05") fmt.println(rowkey, "-", colkey, ":", value) } break } timetime, _ := excelize.exceldatetotime(timefloat, false) value = timetime.format("2006-01-02 15:04:05") fmt.println(rowkey, "-", colkey, ":", value) } } } } return nil, err }
打印结果
0 - 0 : 2024-03-01 00:00:00 1 - 0 : 2024-03-01 00:00:00 2 - 0 : 2024-03-01 00:00:00 3 - 0 : 2024-03-01 00:00:00 4 - 0 : 2024-03-01 00:00:00 5 - 0 : 2024-03-01 00:00:00
此时可以解决了我们的问题,指定对应的列,转化为常规类型,然后再转化为float64(针对无法转化的数据返回),再转化为time.time类型,最后转化成我们需要的类型
进阶
那么如何自动进行转化?
其实我们可以根据单元格自定义类型来进行转化,正如上面的例子,如当单元格自定义类型为:
- yyyy"年"m"月"d"日"
- yyyy/m/d
- mmm-yy
- yyyy"年"m"月"
- yyyy/m/d h:mm
- ...
- 等类型的时候我们需要将他们转化成常规类型,然后根据后续操作转化成我们想要的类型。
如何自定类型判断呢?
其实根据上述转化成常规类型的操作我们就可以知道是哪个字段来进行确定单元格格式类型
styleid, _ := f.newstyle(&excelize.style{numfmt: 0})
style 中的 numfmt 来进行决定
我们可以看下excelize 中 针对 numfmt 的注释(在newstyle方法上面有详细注释),这里我只粘贴部分注释代码
// index | format string // -------+---------------------------------------------------- // 0 | general // 1 | 0 // 2 | 0.00 // 3 | #,##0 // 4 | #,##0.00 // 5 | ($#,##0_);($#,##0) // 6 | ($#,##0_);[red]($#,##0) // 7 | ($#,##0.00_);($#,##0.00) // 8 | ($#,##0.00_);[red]($#,##0.00) // 9 | 0% // 10 | 0.00% // 11 | 0.00e+00 // 12 | # ?/? // 13 | # ??/?? // 14 | m/d/yy // 15 | d-mmm-yy // 16 | d-mmm // 17 | mmm-yy // 18 | h:mm am/pm // 19 | h:mm:ss am/pm // 20 | h:mm // 21 | h:mm:ss // 22 | m/d/yy h:mm // ... | ... // 37 | (#,##0_);(#,##0) // 38 | (#,##0_);[red](#,##0) // 39 | (#,##0.00_);(#,##0.00) // 40 | (#,##0.00_);[red](#,##0.00) // 41 | _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_) // 42 | _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_) // 43 | _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_) // 44 | _($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_) // 45 | mm:ss // 46 | [h]:mm:ss // 47 | mm:ss.0 // 48 | ##0.0e+0 // 49 | @ // number format code in zh-cn language: // // index | symbol // -------+------------------------------------------- // 27 | yyyy"年"m"月" // 28 | m"月"d"日" // 29 | m"月"d"日" // 30 | m-d-yy // 31 | yyyy"年"m"月"d"日" // 32 | h"时"mm"分" // 33 | h"时"mm"分"ss"秒" // 34 | 上午/下午 h"时"mm"分" // 35 | 上午/下午 h"时"mm"分"ss"秒 // 36 | yyyy"年"m"月 // 50 | yyyy"年"m"月 // 51 | m"月"d"日 // 52 | yyyy"年"m"月 // 53 | m"月"d"日 // 54 | m"月"d"日 // 55 | 上午/下午 h"时"mm"分 // 56 | 上午/下午 h"时"mm"分"ss"秒 // 57 | yyyy"年"m"月 // 58 | m"月"d"日"
我们可以知道numfmt 对应的值代表着各种单元格格式,此时我们可以整理一下我们需要将其转化成常规类型的数据(只做参考,并没有全部实验,自己可以把常用的实验一下)
var conversiontimenumfmt = []int{ 14, //m/d/yy 15, //d-mmm-yy 17, //mmm-yy 22, //m/d/yy h:mm 27, // yyyy"年"m"月" 30, //m-d-yy 31, //yyyy"年"m"月"d"日" 36, //yyyy"年"m"月 50, //yyyy"年"m"月 52, //yyyy"年"m"月 57, //yyyy"年"m"月 }
好了,现在我们要转换的单元格格式了,那么接下来我们就可以遍历一下excel的全部单元格格式类型,看一下哪些字段需要进行转化了,不过接下来问题又来了,如何知道excel里面对应的单元格格式呢
如何知道excel中单元格格式
excelize 中提供方法 getcellstyle() 可以获取该单元格的所有样式对应的styleid
// getcellstyle provides a function to get cell style index by given worksheet // name and cell reference. this function is concurrency safe. func (f *file) getcellstyle(sheet, cell string) (int, error) { ... }
根据styleid 我们可以找到对应的所有样式配置 getstyle()
// getstyle provides a function to get style definition by given style index. func (f *file) getstyle(idx int) (*style, error) { ... }
单元格格式 就对应的是 style.numfmt
这时我们只需要知道每一个单元格的styleid 对应的 style.numfmt 就可以将数据进行转化(这里做了三个循环,第一遍是获取了对应styleid, 第二遍是更改表样式,将指定类型转化为常规类型,第三遍就是获取对应的数据)
// parsefileurl 解析文件流excel func parsefileurl(filepath string) ([]map[string]string, error) { f, err := excelize.openfile(filepath) if err != nil { return nil, err } sheetname := f.getsheetname(0) rows, err := f.getrows(sheetname) if err != nil { return nil, err } //读取excel 所有styleid 数组 styles := make([]int, 0) //所有需要更改单元格格式的 styleid 数组 needchangestyleids := make([]int, 0) //所更改的cells needchangecells := make([]string, 0) if len(rows) > 0 { //需要转化成的 style 对应style id styleidzero, _ := f.newstyle(&excelize.style{numfmt: 0}) for rowkey, cols := range rows { if len(cols) > 0 { for colkey, _ := range cols { columnnumber, _ := excelize.coordinatestocellname(colkey+1, rowkey+1) styleid, _ := f.getcellstyle(sheetname, columnnumber) if !arrayhelper.inarray(styles, styleid) { styles = append(styles, styleid) } } } } fmt.println(styles) if len(styles) > 0 { for _, styleid := range styles { style, _ := f.getstyle(styleid) if arrayhelper.inarray(conversiontimenumfmt, style.numfmt) { needchangestyleids = append(needchangestyleids, styleid) } } } for rowkey, cols := range rows { if len(cols) > 0 { for colkey, _ := range cols { columnnumber, _ := excelize.coordinatestocellname(colkey+1, rowkey+1) styleid, _ := f.getcellstyle(sheetname, columnnumber) if arrayhelper.inarray(needchangestyleids, styleid) { _ = f.setcellstyle(sheetname, columnnumber, columnnumber, styleidzero) needchangecells = append(needchangecells, columnnumber) } } } } rows, err = f.getrows(sheetname) if err != nil { return nil, err } if len(rows) > 0 { for rowkey, cols := range rows { if len(cols) > 0 { for colkey, value := range cols { columnnumber, _ := excelize.coordinatestocellname(colkey+1, rowkey+1) if arrayhelper.inarray(needchangecells, columnnumber) { timefloat, err := strconv.parsefloat(value, 64) if err != nil { //err 说明无法转化成float64 那么有可能本身是字符串时间进行返回 timetime, err := time.parse("2006-01-02 15:04:05", value) if err != nil { fmt.println(rowkey, "-", colkey, ":", value) } else { value = timetime.format("2006-01-02 15:04:05") fmt.println(rowkey, "-", colkey, ":", value) } break } timetime, _ := excelize.exceldatetotime(timefloat, false) value = timetime.format("2006-01-02 15:04:05") fmt.println(rowkey, "-", colkey, ":", value) } } } } } } return nil, err } var conversiontimenumfmt = []int{ 14, //m/d/yy 15, //d-mmm-yy 17, //mmm-yy 22, //m/d/yy h:mm 27, // yyyy"年"m"月" 30, //m-d-yy 31, //yyyy"年"m"月"d"日" 36, //yyyy"年"m"月 50, //yyyy"年"m"月 52, //yyyy"年"m"月 57, //yyyy"年"m"月 }
其中inarray方法为
// inarray 判断元素是否再数组内 func inarray[t int | float64 | string](array []t, value t) bool { for _, v := range array { if v == value { return true } } return false }
通过三次遍历操作,自动转化了时间类型
以上就是go excelize读取excel进行时间类型转换的示例代码(自动转换)的详细内容,更多关于go excelize excel时间类型转换的资料请关注代码网其它相关文章!
发表评论