drby30217
2016-10-22 00:09
浏览 392
已采纳

Golang Excel文件读取

I'm using tealeg xlsx library to read an excel file https://github.com/tealeg/xlsx . They have documentation here https://godoc.org/github.com/tealeg/ . It works perfectly fine if I call the OpenFile() by local directory, but I wanted to use an http.Request.FormFile() return object which is of type multipart.Form. How do I use this file to be read by the tealeg package?

Tealeg's OpenReaderAt() looks like something I should use, but the multipart. Form object returned from http.Request.FormFile() returns a file interface but I'm not sure how to access the readerAt object? https://golang.org/pkg/mime/multipart/#File

图片转代码服务由CSDN问答提供 功能建议

我正在使用tealeg xlsx库读取Excel文件 https://github.com/tealeg/xlsx 。 他们在 https://godoc.org/github.com/tealeg/ 中提供了文档。 如果我通过本地目录调用 OpenFile(),它工作得很好,但是我想使用 http.Request.FormFile()返回对象,其类型为 multipart.Form 。 我如何使用tealeg包读取此文件?

Tealeg的 OpenReaderAt()看起来应该使用,但是要分多个部分。 从 http.Request.FormFile()返回的Form对象返回文件接口,但是我不确定如何访问readerAt对象? https://golang.org/pkg/mime/multipart/#File

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douchunxian9740 2016-10-22 03:22
    已采纳

    func OpenReaderAt(r io.ReaderAt, size int64) (*File, error)

    xlsx.OpenReaderAt takes in an io.ReaderAt interface and multipart.File also implements io.ReaderAt.

    So you can directly pass it to xlsx.OpenReaderAt

    var (
    file multipart.File
    size int64
    err error
    )
    
    file, _,err = req.FormFile("key")
    // size = // Calculate size
    xlsx.OpenReaderAt(file,size)
    
    点赞 打赏 评论
  • xurime 2020-10-12 17:06

    推荐一个功能丰富、兼容性良好、高性能的 Excel 文档基础库 Excelize:https://github.com/xuri/excelize

    点赞 打赏 评论

相关推荐