---
title: File.Contents – อ่านข้อมูลจากไฟล์
url: https://www.thepexcel.com/functions/power-query/accessing-data-functions/file-contents/
type: function-explainer
program: Power Query
syntax: "= File.Contents(path as text, optional options as nullable record) as binary"
date: 2025-12-12
updated: 2025-12-23
scores:
  popularity: 7
  difficulty: 2
  usefulness: 7
---

# File.Contents – อ่านข้อมูลจากไฟล์

> File.Contents ใช้อ่านข้อมูล binary ของไฟล์จากพาธที่ระบุ เพื่อนำเข้าข้อมูลจากระบบไฟล์ลงในโปรแกรม

## คำอธิบาย

File.Contents ใช้อ่านข้อมูล binary ของไฟล์จากพาธที่ระบุ เพื่อนำเข้าข้อมูลจากระบบไฟล์ลงในโปรแกรม

## Syntax

```excel
= File.Contents(path as text, optional options as nullable record) as binary
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| path | Yes | text |  | พาธไฟล์ที่ต้องการอ่าน ต้องเป็น absolute path เช่น 'C:\\Users\\Name\\Data.xlsx' |
| options | No | nullable record | null | ตัวเลือกเพิ่มเติม (สำหรับการใช้งานภายในเท่านั้น) |

## ตัวอย่าง

### 1. อ่านไฟล์ Excel พื้นฐาน

```excel
= let
    FilePath = "C:\\Users\\John\\Desktop\\Sales.xlsx",
    FileData = File.Contents(FilePath),
    ExcelFile = Excel.Workbook(FileData)
  in
    ExcelFile
```

**ผลลัพธ์:** `ไฟล์ Excel ที่แปลงเป็น binary data พร้อมสำหรับการประมวลผลเพิ่มเติม`

ใช้ File.Contents อ่านไฟล์ Excel แล้ว Excel.Workbook จะแปลงเป็นตารางที่อ่านได้

### 2. อ่านไฟล์ CSV พร้อมแปลงเป็นตาราง

```excel
= let
    FilePath = "C:\\Data\\Employees.csv",
    FileData = File.Contents(FilePath),
    CSVContent = Csv.Document(FileData),
    Headers = Table.PromoteHeaders(CSVContent)
  in
    Headers
```

**ผลลัพธ์:** `ตารางข้อมูล CSV พร้อมใช้งาน โดยแถวแรกเป็น headers`

File.Contents อ่านไฟล์ CSV เป็น binary จากนั้น Csv.Document แปลงเป็นตาราง และ PromoteHeaders ยกแถวแรกขึ้นมาเป็นชื่อคอลัมน์

### 3. อ่านไฟล์ JSON และแปลงเป็นตาราง

```excel
= let
    FilePath = "C:\\Data\\config.json",
    FileData = File.Contents(FilePath),
    TextData = Text.FromBinary(FileData, TextEncoding.Utf8),
    JSONData = Json.Document(TextData)
  in
    JSONData
```

**ผลลัพธ์:** `ข้อมูล JSON ที่เปลี่ยนจาก binary เป็น text แล้วแปลงเป็นรูปแบบ JSON`

ต้องแปลง binary เป็น text ก่อนใช้ Json.Document เพื่อให้ JSON.Document อ่านได้

### 4. ใช้กับ Folder.Contents เพื่ออ่านไฟล์หลายแฟ้ม

```excel
= let
    FolderPath = "C:\\Data\\Reports",
    FolderFiles = Folder.Contents(FolderPath),
    ExcelFiles = Table.SelectRows(FolderFiles, each Text.EndsWith([Name], ".xlsx")),
    FilesWithContent = Table.AddColumn(
      ExcelFiles, 
      "FileContent", 
      each File.Contents([Folder Path] & [Name])
    )
  in
    FilesWithContent
```

**ผลลัพธ์:** `ตารางที่มีคอลัมน์ 'FileContent' ประกอบด้วยข้อมูล binary ของไฟล์ Excel ทั้งหมด`

รวม Folder.Contents และ File.Contents เพื่ออ่านไฟล์ Excel ทั้งหมดจากโฟลเดอร์ จากนั้นเพิ่มคอลัมน์ใหม่ประกอบด้วยข้อมูลของแต่ละไฟล์

## หมายเหตุเพิ่มเติม

- ใช้ File.Contents แล้วแปลงตามรูปแบบไฟล์: Excel.Workbook, Csv.Document, Json.Document, Xml.Tables

- รวมกับ Folder.Contents เพื่ออ่านไฟล์หลายแฟ้มจากโฟลเดอร์เดียว

- หากไฟล์ต้องเปลี่ยนพาธ (เช่น develop vs production) ให้เก็บพาธไว้ในตัวแปร let เพื่อง่ายต่อการแก้ไข

- File.Contents อาจ trigger permission errors ถ้าไฟล์ถูก lock โดยโปรแกรมอื่น ให้ปิดไฟล์ก่อน

- หากไฟล์มีขนาดใหญ่มากๆ ระวังปัญหาเรื่องหน่วยความจำ ลองทำ chunk processing หรือใช้ Power BI Desktop แทน Excel

## คำถามที่พบบ่อย

**Q: File.Contents ต้องใช้ absolute path ใช่ไหม?**

ใช่ครับ ต้องเป็น absolute path เช่น 'C:\\Users\\Name\\file.xlsx' เสมอ ไม่สามารถใช้ relative path ได้

**Q: หากอ่านไฟล์จากแหล่งที่ไม่ใช่ local drive ได้หรือไม่?**

File.Contents ออกแบบมาสำหรับไฟล์ local เท่านั้น หากต้องอ่านจากเนตเวิร์ก (SharePoint, OneDrive) ให้ใช้ SharePoint.Contents หรือ Web.Contents แทน

**Q: ส่วนต่างระหว่าง File.Contents กับ Excel.Workbook คืออะไร?**

File.Contents อ่านไฟล์เป็น binary data เท่านั้น ส่วน Excel.Workbook ใช้ประมวลผล binary data เพื่อแปลงเป็นตาราง ต้องใช้ File.Contents ก่อนจึงใช้ Excel.Workbook ได้

**Q: จะเลือกได้ว่าจะโหลดอักขระ (encoding) ไหนหรือไม่?**

File.Contents เองไม่มีพารามิเตอร์ encoding แต่เมื่อแปลง binary เป็น text ก็ใช้ Text.FromBinary(data, TextEncoding.Utf8) ระบุได้

## แหล่งข้อมูลเพิ่มเติม

- [File.Contents - Microsoft Learn](https://learn.microsoft.com/en-us/powerquery-m/file-contents) _(official)_
- [Power Query M Function Reference](https://learn.microsoft.com/en-us/powerquery-m/power-query-m-function-reference) _(official)_
- [File.Contents Examples - PowerQuery.how](https://powerquery.how/) _(article)_
- [Understanding Power Query M Functions](https://learn.microsoft.com/en-us/powerquery-m/understanding-power-query-m-functions) _(official)_

---

_Source: [https://www.thepexcel.com/functions/power-query/accessing-data-functions/file-contents/](https://www.thepexcel.com/functions/power-query/accessing-data-functions/file-contents/)_
