golang解析xlsx文件

首先获取第三方库

go get github.com/tealeg/xlsx

参考代码示例
作用为获取第一列除表头外的所有信息,存入数组

//xlsx文件解析
func ExcelParse(fileName string) []string {
    filePath := "upload/" + fileName
    xlFile, err := xlsx.OpenFile(filePath)
    checkErr(err)//自己定义的函数
    //获取行数
    length := len(xlFile.Sheets[0].Rows)
    //开辟除表头外的行数的数组内存
    resourceArr := make([]string, length-1)
    //遍历sheet
    for _, sheet := range xlFile.Sheets {
        //遍历每一行
        for rowIndex, row := range sheet.Rows {
            //跳过第一行表头信息
            if rowIndex == 0 {
                // for _, cell := range row.Cells {
                // text := cell.String()
                // fmt.Printf("%s\n", text)
                // }
                continue
            }
            //遍历每一个单元
            for cellIndex, cell := range row.Cells {
                text := cell.String()
                if text != "" {
                    //如果是每一行的第一个单元格
                    if cellIndex == 0 {
                        resourceArr[rowIndex-1] = text
                    }
                }
            }
        }
    }
    return resourceArr
}
全部评论

相关推荐

头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务