Share c# đọc file excel

biglion472

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

# Excel
# C#
# Lập trình
# Hướng dẫn

** 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ó cho phép người dùng tạo và chỉnh sửa bảng tính, có thể được sử dụng để lưu trữ dữ liệu, thực hiện tính toán và tạo biểu đồ và đồ thị.C# là ngôn ngữ lập trình được sử dụng để phát triển các ứng dụng cho Windows, MacOS và Linux.Đó là một ngôn ngữ mạnh mẽ phù hợp để phát triển các ứng dụng phức tạp.

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#.Chúng tôi sẽ sử dụng [SDK OpenXML] (https://docs.microsoft.com/en-us/do...mats.spreadsheetml.xlsx?view=Net-6.0).OpenXML SDK là một tập hợp các lớp có thể được sử dụng để đọc và ghi các tài liệu văn phòng, bao gồm các tệp Excel.

### 1. Tạo dự án C# mới

Để bắt đầu, hãy tạo một dự án C# mới trong Visual Studio.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. Thêm SDK OpenXML vào dự án của bạn

Bước tiếp theo là thêm SDK OpenXML vào dự án của bạn.Để thực hiện việc này, nhấp chuột phải vào dự án của bạ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 ** system.io.packaging ** nút.Chọn định dạng ** OpenXML 2.8 ** Lắp ráp và nhấp vào ** Thêm **.

### 3. Tạo tệp Excel

Để kiểm tra mã của chúng tôi, chúng tôi cần tạo một tệp Excel.Bạn có thể làm điều này bằng cách mở Excel và tạo một bảng tính mới.Lưu bảng tính dưới dạng tệp .xlsx.

### 4. Viết mã để đọc tệp Excel

Bây giờ chúng tôi có một tệp Excel, chúng tôi có thể viết mã để đọc nó.Mã sau đây cho thấy cách đọc tệp Excel bằng SDK OpenXML:

`` `C#
sử dụng hệ thống;
sử dụng System.io;
sử dụng System.io.Packaging;
sử dụng System.xml.linq;

không gian tên Excelreader
{
Chương trình lớp học
{
static void main (String [] args)
{
// Tạo một đối tượng FileStream mới để đọc tệp Excel.
FileStream fileStream = new fileStream ("excelfile.xlsx", filemode.open, fileaccess.read);

// Tạo một đối tượng gói mới để đọc tệp Excel.
Gói gói = gói.open (filestream);

// Nhận sổ làm việc từ gói.
Workbookbook = pack.getPartById (tên partName mới ("xl/workbook.xml")).

// Nhận bảng tính từ sổ làm việc.
Bảng tính bảng tính = Workbook.WorkSheet;

// In tên của bảng tính.
foreach (bảng tính bảng tính trong bảng tính)
{
Console.WriteLine (bảng tính.name);
}

// Đóng luồng tệp.
fileStream.close ();
}
}
}
`` `

Khi bạn chạy mã này, nó sẽ in tên của bảng tính trong tệp Excel.

### 5. 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 đã sử dụng SDK OpenXML để đọc tệp Excel.OpenXML SDK là một công cụ mạnh mẽ có thể được sử dụng để đọc và viết tài liệu văn phòng.
=======================================
## C# read Excel file

# Excel
# C#
# Programming
# Tutorial

**How to read an Excel file in C#**

Excel is a popular spreadsheet program that is used by businesses and individuals alike. It allows users to create and edit spreadsheets, which can be used to store data, perform calculations, and create charts and graphs. C# is a programming language that is used to develop applications for Windows, macOS, and Linux. It is a powerful language that is well-suited for developing complex applications.

In this tutorial, we will show you how to read an Excel file in C#. We will use the [OpenXML SDK](https://docs.microsoft.com/en-us/dotnet/api/system.io.packaging.openxmlformats.spreadsheetml.xlsx?view=net-6.0) to read the Excel file. The OpenXML SDK is a set of classes that can be used to read and write Office documents, including Excel files.

### 1. Create a new C# project

To get started, create a new C# project in Visual Studio. 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. Add the OpenXML SDK to your project

The next step is to add the OpenXML SDK to your project. To do this, right-click on your project in the Solution Explorer and select **Add > Reference**. In the **Reference Manager** dialog box, select the **.NET Framework** tab and expand the **System.IO.Packaging** node. Select the **OpenXML Format 2.8** assembly and click **Add**.

### 3. Create an Excel file

To test our code, we need to create an Excel file. You can do this by opening Excel and creating a new spreadsheet. Save the spreadsheet as a .xlsx file.

### 4. Write code to read the Excel file

Now that we have an Excel file, we can write code to read it. The following code shows how to read an Excel file using the OpenXML SDK:

```c#
using System;
using System.IO;
using System.IO.Packaging;
using System.Xml.Linq;

namespace ExcelReader
{
class Program
{
static void Main(string[] args)
{
// Create a new FileStream object to read the Excel file.
FileStream fileStream = new FileStream("ExcelFile.xlsx", FileMode.Open, FileAccess.Read);

// Create a new Package object to read the Excel file.
Package package = Package.Open(fileStream);

// Get the workbook from the package.
Workbook workbook = package.GetPartById(new PartName("xl/workbook.xml")).GetXmlDocument().DocumentElement as Workbook;

// Get the worksheets from the workbook.
WorksheetCollection worksheets = workbook.Worksheets;

// Print the names of the worksheets.
foreach (Worksheet worksheet in worksheets)
{
Console.WriteLine(worksheet.Name);
}

// Close the file stream.
fileStream.Close();
}
}
}
```

When you run this code, it will print the names of the worksheets in the Excel file.

### 5. Conclusion

In this tutorial, we showed you how to read an Excel file in C#. We used the OpenXML SDK to read the Excel file. The OpenXML SDK is a powerful tool that can be used to read and write Office documents.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top