dongqiang1226 2019-04-03 08:44
浏览 125

从xls文件中读取值,但数值无法正确读取

Numeric value from xls file are not reading properly but the string values are fine

file, _ := xls.Open("test.xls", "utf-8")

    sheet := file.GetSheet(0)
    for r := 0; r <= (int(sheet.MaxRow)); r++ {
      row := sheet.Row(r)
      log.Println("column with numeric value: ", row.Col(0))
      log.Println("column with string value: ", row.Col(1))
    }

test.xls:

123 | test

456 | testing

output:

column with numeric value: @

column with string value: test

column with numeric value: @

column with string value: testing

How can I get numeric value correctly?

  • 写回答

1条回答 默认 最新

  • douquan9826 2019-04-03 12:08
    关注

    On my Ubuntu 18.04,I can open file and print content of second column

    package main
    
    import (
        "fmt"
        "github.com/extrame/xls"
        "log"
    )
    
    
    func main() {
    if xlFile, err := xls.Open("test.xls", "utf-8"); err == nil {
        for i := 0; i < xlFile.NumSheets(); i++ {
            sheet := xlFile.GetSheet(i)
            fmt.Println(sheet.Name)
            for r := 0; r <= (int(sheet.MaxRow)); r++ {
                    row := sheet.Row(r)
                    log.Println("column ", row.Col(1))
            }
        }
    }
    

    }

    Pay special attention on Col(1) indexing.

    Output

    Sheet1
    2019/04/03 14:28:29 column  test
    2019/04/03 14:28:29 column  testi
    2019/04/03 14:28:29 column  testing
    

    But for numerical column I got this

    Sheet1
    2019/04/03 14:27:46 column  General
    2019/04/03 14:27:46 column  General
    2019/04/03 14:27:46 column  General
    

    I saved the same file as xlsx. This works with tealeg/xlsx package

    import (
        "fmt"
        "github.com/tealeg/xlsx"
    )
    
    func main() {
        excelFileName := "test.xlsx"
        xlFile, err := xlsx.OpenFile(excelFileName)
        if err != nil {
            err = fmt.Errorf("can not open file!!!", err)
        return
        }
        for _, sheet := range xlFile.Sheets {
            for _, row := range sheet.Rows {
                for _, cell := range row.Cells {
                    text := cell.String()
                    fmt.Println(text)
                }
            }
        }
    }
    

    Output

    123
    test
    788
    456
    testi
    999
    789
    testing
    100
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP