Share Đọc file Excel trong lập trình C#

greenfrog458

New member
** #C ##Excel #Read #file #lập trình **

## Cách đọc tệp Excel trong C#

Excel là một chương trình bảng tính phổ biến được sử dụng bởi các doanh nghiệp và cá nhân.Nó có khả năng lưu trữ và tổ chức dữ liệu theo nhiều cách khác nhau, làm cho nó trở thành một công cụ mạnh mẽ để phân tích dữ liệu và trực quan hóa.C# là ngôn ngữ lập trình hướng đối tượng hiện đại, rất phù hợp để phát triển các ứng dụng tương tác với hệ điều hành Windows.Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách đọc một tệp Excel trong C#.

### Điều kiện tiên quyết

Để làm theo với hướng dẫn này, bạn sẽ cần những điều sau đây:

* Một máy tính chạy Windows 10 trở lên
* Visual Studio 2019 trở lên
* Thời gian chạy Microsoft Excel 2019 trở lên

### Tạo một dự án mới

Để bắt đầu, mở Visual Studio và tạo một dự án mới.Trong hộp thoại mới của dự án, chọn ** Visual C#**> ** Windows Forms **> ** Ứng dụng Windows Forms **.Đặt tên cho dự án "Excelreader" và nhấp vào ** OK **.

### Thêm tham chiếu đến thư viện Excel

Bước tiếp theo là thêm một tham chiếu đến thư viện Excel.Để thực hiện việc này, nhấp chuột phải vào dự án trong Giải pháp Explorer và chọn ** Thêm tham chiếu **.Trong hộp thoại Trình quản lý tham chiếu, chọn tab **. Net Framework ** và mở rộng nút ** system.data **.Chọn ** Microsoft.office.Interop.excel ** Thư viện và nhấp vào ** OK **.

### Tạo một đối tượng sổ làm việc Excel

Bây giờ chúng tôi có một tham chiếu đến thư viện Excel, chúng tôi có thể tạo một đối tượng sổ làm việc Excel.Để làm điều này, chúng ta có thể sử dụng mã sau:

`` `C#
Excel.workbook workbook = new excel.workbook ();
`` `

Mã này sẽ tạo một đối tượng sổ làm việc Excel mới và gán nó cho biến `Sổ làm việc '.

### Mở tệp Excel

Khi chúng tôi đã tạo một đối tượng sổ làm việc Excel, chúng tôi có thể mở một tệp Excel.Để làm điều này, chúng ta có thể sử dụng mã sau:

`` `C#
workbook.open ("c: \ tài liệu của tôi \ myExcelfile.xlsx");
`` `

Mã này sẽ mở tệp Excel nằm ở đường dẫn `C: \ Tài liệu của tôi \ MyExcelfile.xlsx`.

### Đọc dữ liệu từ tệp Excel

Bây giờ chúng tôi đã mở một tệp Excel, chúng tôi có thể đọc dữ liệu từ nó.Để làm điều này, chúng ta có thể sử dụng mã sau:

`` `C#
// Nhận bảng tính đầu tiên trong sổ làm việc.
Excel.worksheet bảng tính = workbook.workSheets [0];

// Nhận ô đầu tiên trong hàng đầu tiên.
Excel.range di động = bảng tính.cells [1, 1];

// Đọc giá trị của ô.
Chuỗi value = cell.value2.ToString ();
`` `

Mã này sẽ nhận được giá trị của ô tại giao điểm của hàng đầu tiên và cột đầu tiên.

### đóng tệp excel

Khi chúng tôi hoàn thành làm việc với một tập tin Excel, chúng tôi nên đóng nó.Để làm điều này, chúng ta có thể sử dụng mã sau:

`` `C#
sổ làm việc.close ();
`` `

### Mã hoàn chỉnh

Mã hoàn chỉnh để đọc tệp Excel trong C# được hiển thị bên dưới:

`` `C#
// Tạo một đối tượng sổ làm việc Excel mới.
Excel.workbook workbook = new excel.workbook ();

// Mở tệp Excel.
workbook.open ("c: \ tài liệu của tôi \ myExcelfile.xlsx");

// Nhận bảng tính đầu tiên trong sổ làm việc.
Excel.worksheet bảng tính = workbook.workSheets [0];

// Nhận ô đầu tiên trong hàng đầu tiên.
Excel.range di động = bảng tính.cells [1, 1];

// Đọc giá trị của ô.
Chuỗi value = cell.value2.ToString ();

// Đóng tệp Excel.
sổ làm việc.close ();
`` `

### Phần kết luận

Trong hướng dẫn này, chúng tôi đã chỉ cho bạn cách đọc một tệp Excel trong C#.Chúng tôi đề cập đến các chủ đề sau:

* Tạo một dự án mới
* Thêm tham chiếu đến thư viện Excel
* Tạo một đối tượng sổ làm việc Excel
* Mở tệp Excel
*
=======================================
**#C# #Excel #Read #file #Programming**

## How to Read an Excel File in C#

Excel is a popular spreadsheet program that is used by businesses and individuals alike. It is capable of storing and organizing data in a variety of ways, making it a powerful tool for data analysis and visualization. C# is a modern object-oriented programming language that is well-suited for developing applications that interact with the Windows operating system. In this tutorial, we will show you how to read an Excel file in C#.

### Prerequisites

To follow along with this tutorial, you will need the following:

* A computer running Windows 10 or later
* Visual Studio 2019 or later
* The Microsoft Excel 2019 or later runtime

### Creating a New Project

To get started, open Visual Studio and create a new project. In the New Project dialog box, select **Visual C#** > **Windows Forms** > **Windows Forms Application**. Name the project "ExcelReader" and click **OK**.

### Adding a Reference to the Excel Library

The next step is to add a reference to the Excel library. To do this, right-click on the project in the Solution Explorer and select **Add Reference**. In the Reference Manager dialog box, select the **.NET Framework** tab and expand the **System.Data** node. Select the **Microsoft.Office.Interop.Excel** library and click **OK**.

### Creating an Excel Workbook Object

Now that we have a reference to the Excel library, we can create an Excel workbook object. To do this, we can use the following code:

```c#
Excel.Workbook workbook = new Excel.Workbook();
```

This code will create a new Excel workbook object and assign it to the variable `workbook`.

### Opening an Excel File

Once we have created an Excel workbook object, we can open an Excel file. To do this, we can use the following code:

```c#
workbook.Open("C:\My Documents\MyExcelFile.xlsx");
```

This code will open the Excel file located at the path `C:\My Documents\MyExcelFile.xlsx`.

### Reading Data from an Excel File

Now that we have opened an Excel file, we can read data from it. To do this, we can use the following code:

```c#
// Get the first worksheet in the workbook.
Excel.Worksheet worksheet = workbook.Worksheets[0];

// Get the first cell in the first row.
Excel.Range cell = worksheet.Cells[1, 1];

// Read the value of the cell.
string value = cell.Value2.ToString();
```

This code will get the value of the cell at the intersection of the first row and the first column.

### Closing an Excel File

When we are finished working with an Excel file, we should close it. To do this, we can use the following code:

```c#
workbook.Close();
```

### Complete Code

The complete code for reading an Excel file in C# is shown below:

```c#
// Create a new Excel workbook object.
Excel.Workbook workbook = new Excel.Workbook();

// Open the Excel file.
workbook.Open("C:\My Documents\MyExcelFile.xlsx");

// Get the first worksheet in the workbook.
Excel.Worksheet worksheet = workbook.Worksheets[0];

// Get the first cell in the first row.
Excel.Range cell = worksheet.Cells[1, 1];

// Read the value of the cell.
string value = cell.Value2.ToString();

// Close the Excel file.
workbook.Close();
```

### Conclusion

In this tutorial, we showed you how to read an Excel file in C#. We covered the following topics:

* Creating a new project
* Adding a reference to the Excel library
* Creating an Excel workbook object
* Opening an Excel file
*
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top