Share C++ Đọc File Excel: Đọc Dữ Liệu Từ File Excel Trong C++

doanpaintball

New member
## 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.Nó thường được sử dụng để tạo báo cáo tài chính, theo dõi số liệu bán hàng và quản lý hàng tồn kho.C ++ là một ngôn ngữ lập trình mạnh mẽ có thể được sử dụng để tạo ra nhiều ứng dụng.Đây cũng là một lựa chọn tốt để đọc các tệp Excel vì nó cung cấp một số chức năng tích hợp có thể được sử dụng để phân tích và trích xuất dữ liệu từ bảng tính Excel.

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách đọc tệp Excel trong C ++.Chúng tôi sẽ sử dụng [excel :: reader] (https://github.com/cpp-excel/cpp-excel), cung cấp API đơn giản và dễ sử dụng để đọc các tệp Excel.

### 1. Bắt đầu

Để bắt đầu, bạn sẽ cần cài đặt thư viện Excel :: Reader.Bạn có thể làm điều này bằng cách làm theo các hướng dẫn trên trang web [Excel :: Reader] (https://github.com/cpp-excel/cpp-excel).

Khi bạn đã cài đặt thư viện, bạn có thể tạo một dự án C ++ mới và thêm tệp tiêu đề Excel :: Reader vào dự án của bạn.

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

Để đọc một tệp excel, bạn có thể sử dụng hàm `excel :: reader :: open ()`.Hàm này đưa đường dẫn đến tệp excel làm đối số duy nhất của nó.

`` `C ++
Excel :: Reader Reader ("myfile.xlsx");
`` `

`Excel :: Reader :: Open ()` Hàm sẽ trả về `std :: shared_ptr <excel :: workbook>` Object.Đối tượng này đại diện cho sổ làm việc excel mà bạn đang đọc.

### 3. Truy cập bảng tính

Một cuốn sách bài tập Excel có thể chứa nhiều bảng tính.Để truy cập một bảng tính, bạn có thể sử dụng hàm `excel :: workbook :: getworksheet ()`.Hàm này lấy tên của bảng tính làm đối số duy nhất của nó.

`` `C ++
Excel :: bảng tính* bảng tính = workbook-> getworkSheet ("sheet1");
`` `

`Excel :: Bảng tính :: GetWorkSheet ()` Hàm sẽ trả về `std :: shared_ptr <excel :: bảng tính>` Đối tượng.Đối tượng này đại diện cho bảng tính mà bạn đang truy cập.

### 4. Đọc dữ liệu từ các ô

Để đọc dữ liệu từ một ô, bạn có thể sử dụng hàm `excel :: cell :: getValue ()`.Hàm này lấy chỉ số cột và hàng của ô làm đối số của nó.

`` `C ++
int value = bảng tính-> getCell (0, 0)-> getValue <int> ();
`` `

`Excel :: cell :: getValue ()` Hàm sẽ trả về giá trị của ô dưới dạng `std :: any bất kỳ đối tượng.Sau đó, bạn có thể đúc đối tượng này vào loại mong muốn.

### 5. Đóng sổ làm việc

Khi bạn đọc xong một tệp Excel, bạn nên đóng sổ làm việc.Bạn có thể làm điều này bằng cách gọi chức năng `excel :: workbook :: close ()`.

`` `C ++
sổ làm việc-> đóng ();
`` `

### Mã ví dụ

Mã sau đây hiển thị một ví dụ về cách đọc tệp Excel trong C ++.

`` `C ++
#include <Istream>
#include <excel/reader.h>

int main ()
{
// Tạo một excel mới :: Đối tượng Reader.
Excel :: Reader Reader ("myfile.xlsx");

// Nhận đối tượng sổ làm việc.
Excel :: Sổ làm việc* Sổ làm việc = Reader.open ();

// Nhận đối tượng bảng tính.
Excel :: bảng tính* bảng tính = workbook-> getworkSheet ("sheet1");

// Đọc giá trị của ô đầu tiên.
int value = bảng tính-> getCell (0, 0)-> getValue <int> ();

// In giá trị vào bảng điều khiển.
std :: cout << "Giá trị của ô đầu tiên là" << value << std :: endl;

// Đóng sổ làm việc.
sổ làm việc-> đóng ();

trả lại 0;
}
`` `

### hashtags

* #C ++
* #Excel
* #SpreadSheet
* #Phân tích dữ liệu
* #Programming
=======================================
## How to Read Excel File in C++

Excel is a popular spreadsheet program that is used to store and organize data. It is often used to create financial reports, track sales figures, and manage inventory. C++ is a powerful programming language that can be used to create a variety of applications. It is also a good choice for reading Excel files because it provides a number of built-in functions that can be used to parse and extract data from Excel spreadsheets.

In this tutorial, we will show you how to read an Excel file in C++. We will use the [Excel::Reader](https://github.com/cpp-excel/cpp-excel) library, which provides a simple and easy-to-use API for reading Excel files.

### 1. Getting Started

To get started, you will need to install the Excel::Reader library. You can do this by following the instructions on the [Excel::Reader website](https://github.com/cpp-excel/cpp-excel).

Once you have installed the library, you can create a new C++ project and add the Excel::Reader header file to your project.

### 2. Reading an Excel File

To read an Excel file, you can use the `Excel::Reader::eek:pen()` function. This function takes the path to the Excel file as its only argument.

```c++
Excel::Reader reader("myfile.xlsx");
```

The `Excel::Reader::eek:pen()` function will return a `std::shared_ptr<Excel::Workbook>` object. This object represents the Excel workbook that you are reading.

### 3. Accessing Worksheets

An Excel workbook can contain multiple worksheets. To access a worksheet, you can use the `Excel::Workbook::getWorksheet()` function. This function takes the name of the worksheet as its only argument.

```c++
Excel::Worksheet* worksheet = workbook->getWorksheet("Sheet1");
```

The `Excel::Worksheet::getWorksheet()` function will return a `std::shared_ptr<Excel::Worksheet>` object. This object represents the worksheet that you are accessing.

### 4. Reading Data from Cells

To read data from a cell, you can use the `Excel::Cell::getValue()` function. This function takes the column and row index of the cell as its arguments.

```c++
int value = worksheet->getCell(0, 0)->getValue<int>();
```

The `Excel::Cell::getValue()` function will return the value of the cell as a `std::any` object. You can then cast this object to the desired type.

### 5. Closing the Workbook

When you are finished reading an Excel file, you should close the workbook. You can do this by calling the `Excel::Workbook::close()` function.

```c++
workbook->close();
```

### Example Code

The following code shows an example of how to read an Excel file in C++.

```c++
#include <iostream>
#include <Excel/Reader.h>

int main()
{
// Create a new Excel::Reader object.
Excel::Reader reader("myfile.xlsx");

// Get the workbook object.
Excel::Workbook* workbook = reader.open();

// Get the worksheet object.
Excel::Worksheet* worksheet = workbook->getWorksheet("Sheet1");

// Read the value of the first cell.
int value = worksheet->getCell(0, 0)->getValue<int>();

// Print the value to the console.
std::cout << "The value of the first cell is " << value << std::endl;

// Close the workbook.
workbook->close();

return 0;
}
```

### Hashtags

* #C++
* #Excel
* #SpreadSheet
* #DataAnalysis
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top