如何使用Excelize在URL链接上打开Excel文件?

huangapple go评论83阅读模式
英文:

How to open an Excel file on Excelize using URL link?

问题

我尝试解析一个Excel文件并使用Excelize进行处理。我只有一个可以用来访问文件的URL链接。

对于我的情况,我使用了一个我上传到Dropbox的文件进行测试。Dropbox的URL链接是这样的www.dropbox.com/s/t537135761/filename.xlsx,但是当我尝试使用这个链接时,Excelize会抛出以下错误:

open: www.dropbox.com/s/t537135761/filename.xlsx: 没有该文件或目录

文件本身已经对公众开放。我可以在隐身模式下浏览它,所以这不是因为受限制的访问问题。URL链接中有文件扩展名,所以我认为URL链接指向了可以被Excelize解析的实际文件。

我不确定问题是在Dropbox本身还是在Excelize上,但这是我可以用来测试我的代码的云存储服务。

有人可以告诉我如何使用Excelize打开URL链接的文件吗?我尝试搜索其他函数,但找不到任何相关的内容。

这是我用于测试的代码(简化版):

package main

func main() {

var urlLink = "www.dropbox.com/s/t537135761/filename.xlsx"

exlz, err := excelize.OpenFile(urlLink)
if err != {
  fmt.Println(err)
  return
}

defer func() {
  if err = exlz.Close(); err != nil {
    fmt.Println(err)
  }
}

  return
}

英文:

I tried to parse an Excel file and process it using Excelize. I only have a URL link that can be used to access the file.

For my case, I tested using a file I uploaded to Dropbox. The url from dropbox is like this www.dropbox.com/s/t537135761/filename.xlsx, but somehow when I try to use this link, the Excelize throws off this error:

open: www.dropbox.com/s/t537135761/filename.xlsx: no such file or directory

The file itself is already open for public. I can browse it on incognito mode so it means it's not because of restricted access problem. It has the extension file on the URL so I assume the URL touches the actual file that can be parsed by Excelize

I'm not sure if the problem is on Dropbox itself or is on Excelize, but that's only the cloud storage I can use to test my code.

Anyone can tell me how to use Excelize to open file using URL link? I tried searching for another function but can't find anything.

This is roughly my code (simplified) for testing.

package main

func main() {

var urlLink = "www.dropbox.com/s/t537135761/filename.xlsx"

exlz, err := excelize.OpenFile(urlLink)
if err != {
  fmt.Println(err)
  return
}

defer func() {
  if err = exlz.Close(); err != nil {
    fmt.Println(err)
  }
}

  return
}

答案1

得分: 2

这是解决方案,下面是解释。

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"

	excelize "github.com/xuri/excelize/v2"
)

func main() {

    // 注意这里末尾的1,而不是你原始链接中的0
	openURL("https://www.dropbox.com/s/8ngoie6spyga8k1/for%20testing.xlsx?dl=1")
}

func openURL(urlLink string) {
	data, err := getData(urlLink)
	if err != nil {
		panic(err)
	}

	// 使用 Excelize 打开 ZIP 文件
	exlz, err := excelize.OpenReader(bytes.NewReader(data))
	if err != nil {
		fmt.Println("Reader", err)
		return
	}

	lst := exlz.GetSheetList()
	if len(lst) == 0 {
		fmt.Println("Empty document")
		return
	}

	fmt.Println("Sheet list:")
	for _, s := range lst {
		fmt.Println(s)
	}

	defer func() {
		if err = exlz.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	fmt.Println("Done")

}

func getData(url string) ([]byte, error) {

	r, err := http.Get(url)
	if err != nil {
		panic(err)
	}

	defer r.Body.Close()

	return ioutil.ReadAll(r.Body)
}

对于 URL,你需要先获取数据。

第二个问题是你使用的 Dropbox 链接并不返回实际的 Excel 文件,而是一个嵌入了 Excel 的 HTML 文件。

你需要将链接末尾的 dl=1 改为 dl=0。这样才能得到实际的 Excel 文件。

英文:

This is the solution, below the explanation.

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"

	excelize "github.com/xuri/excelize/v2"
)

func main() {

    // note here the 1 at the end instead of 0 in your original link
	openURL("https://www.dropbox.com/s/8ngoie6spyga8k1/for%20testing.xlsx?dl=1")
}

func openURL(urlLink string) {
	data, err := getData(urlLink)
	if err != nil {
		panic(err)
	}

	// Open the ZIP file with Excelize
	exlz, err := excelize.OpenReader(bytes.NewReader(data))
	if err != nil {
		fmt.Println("Reader", err)
		return
	}

	lst := exlz.GetSheetList()
	if len(lst) == 0 {
		fmt.Println("Empty document")
		return
	}

	fmt.Println("Sheet list:")
	for _, s := range lst {
		fmt.Println(s)
	}

	defer func() {
		if err = exlz.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	fmt.Println("Done")

}

func getData(url string) ([]byte, error) {

	r, err := http.Get(url)
	if err != nil {
		panic(err)
	}

	defer r.Body.Close()

	return ioutil.ReadAll(r.Body)
}

For URL's you need to actually fetch the data first.

The second problem is that the Dropbox link that you used does not return an actual Excel, but an HTML file with an Excel embedded in it.

You need to change dl=1 at the end. This will give you the actual Excel file.

huangapple
  • 本文由 发表于 2022年9月23日 19:56:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/73827326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定