Share c# read excel file

truongmet2002

New member
#C # #Đọc tệp Excel #Excel #CSV #data

## 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 để lưu trữ và sắp xếp dữ liệu.C# là ngôn ngữ lập trình có thể được sử dụng để đọc và ghi dữ liệu từ các tệp Excel.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#.

### 1. Bắt đầu

Để bắt đầu, bạn sẽ cần cài đặt Microsoft .NET Framework.Bạn có thể tải xuống .NET Framework từ trang web Microsoft.

Khi bạn đã cài đặt .NET Framework, bạn sẽ cần tạo một dự án C# mới.Bạn có thể làm điều này bằng cách mở Visual Studio và chọn tệp **> mới> Dự án **.Trong hộp thoại ** mới **, chọn ** Visual C#> Windows> Ứng dụng bảng điều khiển **.Đặt tên cho dự án "Excelreader" và nhấp vào ** OK **.

### 2. Đọc một tệp excel

Để đọc một tệp excel, bạn sẽ cần sử dụng lớp `excelworkbook`.Lớp `Excelworkbook` đại diện cho một sổ làm việc excel.Để tạo đối tượng `excelworkbook`, bạn có thể sử dụng phương thức` workbooks.open () `.Phương thức `workbooks.open ()` lấy một chuỗi làm tham số.Chuỗi chỉ định đường dẫn đến tệp Excel mà bạn muốn mở.

Ví dụ: mã sau tạo một đối tượng `excelworkbook` và mở` "C: \ My Documents \ myExcelfile.xlsx" `Tệp:

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

Khi bạn đã tạo một đối tượng `excelworkbook`, bạn có thể truy cập các bảng tính trong sổ làm việc bằng thuộc tính` worksheets`.Thuộc tính `Worksheet` trả về một bộ sưu tập các đối tượng` excelworksheet`.Một đối tượng `excelworksheet` đại diện cho một bảng tính excel.

Để truy cập một bảng tính cụ thể trong sổ làm việc, bạn có thể sử dụng phương thức `Worksheet.item ()`.Phương thức `Bảng tính.Item ()` lấy số nguyên làm tham số.Số nguyên chỉ định chỉ mục của bảng tính mà bạn muốn truy cập.

Ví dụ: mã sau truy cập bảng tính đầu tiên trong sổ làm việc:

`` `C#
Bảng tính excelworksheet = workbook.workSheet.Item (0);
`` `

Khi bạn đã truy cập một bảng tính, bạn có thể truy cập các ô trong bảng tính bằng cách sử dụng thuộc tính `Cells`.Thuộc tính `ô` trả về một tập hợp các đối tượng` excelcell`.Một đối tượng `Excelcell` đại diện cho một ô Excel.

Để truy cập một ô cụ thể trong bảng tính, bạn có thể sử dụng phương thức `cells.item ()`.Phương thức `cells.Item ()` có hai tham số.Tham số đầu tiên chỉ định số hàng của ô mà bạn muốn truy cập.Tham số thứ hai chỉ định số cột của ô mà bạn muốn truy cập.

Ví dụ: mã sau truy cập ô trong hàng đầu tiên và cột đầu tiên của bảng tính:

`` `C#
Excelcell di động = bảng tính.cells.item (0, 0);
`` `

Đối tượng `excelcell` có một số thuộc tính mà bạn có thể sử dụng để truy cập dữ liệu trong ô.Thuộc tính `value` trả về giá trị của ô.Thuộc tính `công thức` trả về công thức của ô.Thuộc tính `text` trả về văn bản của ô.

Ví dụ: mã sau in giá trị của ô trong hàng đầu tiên và cột đầu tiên của bảng tính:

`` `C#
Console.WriteLine (Cell.Value);
`` `

### 3. 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 những điều cơ bản của việc đọc một tệp excel, bao gồm cách tạo đối tượng `excelworkbook`, cách truy cập bảng tính trong sổ làm việc và cách truy cập các ô trong bảng tính.

Để biết thêm thông tin về việc đọc các tệp Excel trong C#, bạn có thể tham khảo các tài nguyên sau:

* [Tài liệu phát triển Microsoft Excel] (https://docs.microsoft.com/en-us/office/dev/api/excel/overview)
* [Stack Overflow] (Newest 'excel-c%2' Questions
=======================================
#C# #Read Excel File #Excel #CSV #data

## How to Read an Excel File in C#

Excel is a popular spreadsheet program that is used to store and organize data. C# is a programming language that can be used to read and write data from Excel files. In this tutorial, we will show you how to read an Excel file in C#.

### 1. Getting Started

To get started, you will need to install the Microsoft .NET Framework. You can download the .NET Framework from the Microsoft website.

Once you have installed the .NET Framework, you will need to create a new C# project. You can do this by opening Visual Studio and selecting **File > New > Project**. In the **New Project** dialog box, select **Visual C# > Windows > Console Application**. Name the project "ExcelReader" and click **OK**.

### 2. Reading an Excel File

To read an Excel file, you will need to use the `ExcelWorkbook` class. The `ExcelWorkbook` class represents an Excel workbook. To create an `ExcelWorkbook` object, you can use the `Workbooks.Open()` method. The `Workbooks.Open()` method takes a string as a parameter. The string specifies the path to the Excel file that you want to open.

For example, the following code creates an `ExcelWorkbook` object and opens the `"C:\My Documents\MyExcelFile.xlsx"` file:

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

Once you have created an `ExcelWorkbook` object, you can access the worksheets in the workbook using the `Worksheets` property. The `Worksheets` property returns a collection of `ExcelWorksheet` objects. An `ExcelWorksheet` object represents an Excel worksheet.

To access a specific worksheet in the workbook, you can use the `Worksheets.Item()` method. The `Worksheets.Item()` method takes an integer as a parameter. The integer specifies the index of the worksheet that you want to access.

For example, the following code accesses the first worksheet in the workbook:

```c#
ExcelWorksheet worksheet = workbook.Worksheets.Item(0);
```

Once you have accessed a worksheet, you can access the cells in the worksheet using the `Cells` property. The `Cells` property returns a collection of `ExcelCell` objects. An `ExcelCell` object represents an Excel cell.

To access a specific cell in the worksheet, you can use the `Cells.Item()` method. The `Cells.Item()` method takes two parameters. The first parameter specifies the row number of the cell that you want to access. The second parameter specifies the column number of the cell that you want to access.

For example, the following code accesses the cell in the first row and first column of the worksheet:

```c#
ExcelCell cell = worksheet.Cells.Item(0, 0);
```

The `ExcelCell` object has a number of properties that you can use to access the data in the cell. The `Value` property returns the value of the cell. The `Formula` property returns the formula of the cell. The `Text` property returns the text of the cell.

For example, the following code prints the value of the cell in the first row and first column of the worksheet:

```c#
Console.WriteLine(cell.Value);
```

### 3. Conclusion

In this tutorial, we showed you how to read an Excel file in C#. We covered the basics of reading an Excel file, including how to create an `ExcelWorkbook` object, how to access worksheets in a workbook, and how to access cells in a worksheet.

For more information on reading Excel files in C#, you can refer to the following resources:

* [Microsoft Excel Developer Documentation](https://docs.microsoft.com/en-us/office/dev/api/excel/overview)
* [Stack Overflow](https://stackoverflow.com/questions/tagged/excel-c%2
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top