Power Query动态TXT文件路径报错:需提供有效绝对路径
解决Power Query动态TXT路径的绝对路径报错问题
排查与修复方案
修正路径拼接格式
检查FolderPath末尾是否带有反斜杠(\),如果缺失,拼接后的路径会出现格式错误(比如C:\Testfile.txt)。修改FullPath步骤,自动补全缺失的反斜杠:FullPath = FolderPath & if Text.EndsWith(FolderPath, "\") then "" else "\" & FilePath清理路径中的隐藏字符
从Excel表格读取的路径可能包含空格、换行或不可见字符,用文本清理函数去除干扰:FolderPath = Text.Clean(Text.Trim(Source{0}[FolderPath header])), FilePath = Text.Clean(Text.Trim(Sourcev2{0}[FilePath_Plant to Company header])), FullPath = FolderPath & if Text.EndsWith(FolderPath, "\") then "" else "\" & FilePath验证路径实际有效性
添加临时步骤检查路径是否真实存在,确认调试时的“正确路径”是否真的有效:IsPathValid = File.Exists(FullPath), // 运行后查看该步骤结果,若为false则说明路径仍有问题确认路径类型合规
File.Contents仅支持本地绝对路径或映射盘符的网络路径,若使用共享文件夹,需用\\服务器名\共享目录格式,而非相对路径或未映射的网络地址。
完整修复代码示例
let Source = Excel.CurrentWorkbook(){[Name="FolderPath"]}[Content], FolderPath = Text.Clean(Text.Trim(Source{0}[FolderPath header])), Sourcev2 = Excel.CurrentWorkbook(){[Name="FilePath_P2C"]}[Content], FilePath = Text.Clean(Text.Trim(Sourcev2{0}[FilePath_Plant to Company header])), FullPath = FolderPath & if Text.EndsWith(FolderPath, "\") then "" else "\" & FilePath, IsPathValid = File.Exists(FullPath), // 验证用步骤,可后续删除 fileContents = File.Contents(FullPath), lines = Lines.FromBinary(fileContents, null, null, 1252), table = Table.FromColumns({lines}) in table
内容的提问来源于stack exchange,提问作者Wei Ni
相关产品推荐
相关产品推荐

