You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Julia中如何无需创建磁盘文件直接读取Web端XLSX文件?

无需创建磁盘文件直接读取Web端XLSX文件(Julia)

当前可行方案(写入磁盘后读取)

目前通过将文件下载到本地磁盘再读取的方式可以正常工作:

tnlret = HTTP.get(tnlurl)
write("tnlxlsx.xlsx", tnlret.body)
tnlxl = XLSX.openxlsx("tnlxlsx.xlsx", mode="r")

直接用IOBuffer读取的报错情况

尝试直接通过IOBuffer读取时触发报错:

tnlret = HTTP.get(tnlurl)
io=IOBuffer()
write(io, tnlret.body)
tnlxl = XLSX.openxlsx(io, mode="r")
close(io)

报错信息:

ERROR: LoadError: input is not a valid XLSX file.
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:35
 [2] check_for_xlsx_file_format(source::IOBuffer, label::String)
   @ XLSX C:\Users\TGebbels\.julia\packages\XLSX\we7i6\src\read.jl:22
 [3] check_for_xlsx_file_format
   @ C:\Users\TGebbels\.julia\packages\XLSX\we7i6\src\read.jl:11 [inlined]
 [4] XLSX.XLSXFile(source::IOBuffer, use_cache::Bool, is_writable::Bool)
   @ XLSX C:\Users\TGebbels\.julia\packages\XLSX\we7i6\src\types.jl:289
 [5] open_or_read_xlsx(source::IOBuffer, read_files::Bool, enable_cache::Bool, read_as_template::Bool)
   @ XLSX C:\Users\TGebbels\.julia\packages\XLSX\we7i6\src\read.jl:196
 [6] openxlsx(source::IOBuffer; mode::String, enable_cache::Bool)
   @ XLSX C:\Users\TGebbels\.julia\packages\XLSX\we7i6\src\read.jl:172
 [7] top-level scope
   @ c:\Users\TGebbels\OneDrive - ...\Read360Gjson.jl:33
in expression starting at c:\Users\TGebbels\OneDrive - ...\Read360Gjson.jl:33

目标文件地址:https://www.tnlcommunityfund.org.uk/media/data/grants/2021_22_v5.xlsx?mtime=20220608130305&focal=none

解决方案(无需磁盘文件)

可以直接读取,问题出在IOBuffer写入后指针停在内容末尾,XLSX.jl需要从起始位置解析文件。有两种修正方式:

方式1:写入后重置IO指针

tnlret = HTTP.get(tnlurl)
io = IOBuffer()
write(io, tnlret.body)
seekstart(io)  # 将IO指针移到起始位置
tnlxl = XLSX.openxlsx(io, mode="r")
# 这里可以对tnlxl进行数据读取操作
close(io)

方式2:直接用响应体初始化IOBuffer

更简洁的写法是直接用tnlret.body初始化IOBuffer,此时指针默认在起始位置:

tnlret = HTTP.get(tnlurl)
io = IOBuffer(tnlret.body)
tnlxl = XLSX.openxlsx(io, mode="r")
# 处理数据...
close(io)

两种方式都可以实现无需创建磁盘文件,直接读取Web端XLSX文件的需求。

内容的提问来源于stack exchange,提问作者Tim Gebbels

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 11:25:54