輸入要讀取的資料夾路徑
在A1欄位輸入完整的資料夾路徑,最後記得要帶一個星號*
,例如D:\ExcelListFileExample\*

定義名稱
到公式
頁籤,點擊定義名稱

新增名稱
開啟新增對話視窗後
名稱填入FileNameList
(想填其它的也可以,自己記住就好)
範圍選擇活頁簿
參照到填入 =FILES(工作表1!$A$1)
結果如下圖所示

帶入公式
在A3欄位貼上以下公式=IFERROR(INDEX(FileNameList,ROW()-2),"")
接著往下拖曳即可列出資料夾內的所有檔案名稱

補充
過濾附檔名
如果只想列出指定的附檔名,例如pdf
,修改位於A1的資料夾路徑,將D:\ExcelListFileExample\*
改成 D:\ExcelListFileExample\*pdf*
即可

/blog/list-of-file-names-from-a-folder-in-excel/Example.xlsm 範例檔案下載
使用VBA?
Sub ListFiles()
Dim MyPath As String
Dim MyFile As String
Dim i As Integer
MyPath = "C:\Users\使用者名稱\Documents\" '將路徑替換為您想要列出檔案的目錄路徑
MyFile = Dir(MyPath & "*.*")
i = 1
Do While MyFile <> ""
Cells(i, 1) = MyFile
i = i + 1
MyFile = Dir
Loop
End Sub
請將 MyPath
替換為您要列出檔案的目錄路徑。此巨集會在活頁簿的第一個工作表中的第一列中列出指定目錄下的所有檔案的檔案名稱。請注意,此代碼僅列出目錄中的檔案,不包括任何子目錄中的檔案。
請按下 Alt + F11 開啟 VBA 編輯器,然後選擇插入模組。將上面的程式碼複製貼上到模組中,然後保存文件並關閉 VBA 編輯器。然後返回 Excel,選擇開發人員標籤,點擊巨集後,找到巨集名稱ListFiles
並選擇後,點一下執行即可。

如果找不到開發人員標籤,請到檔案→選項,開啟Excel 選項後,選擇自訂功能區,然後開啟開發人員
的標籤,如下圖

使用Powershell?
Get-ChildItem "C:\Users\使用者名稱\Documents\" | Where-Object {$_.Attributes -ne "Directory"} | Select-Object Name | Export-Csv -Path "C:\Users\使用者名稱\Documents\檔案清單.csv" -NoTypeInformation
如果想要列出檔案,可以參考這篇文章 Excel列出所有目錄