이 자습서에서는 C++를 사용하여 Excel 파일을 만드는 방법을 배웁니다. C++에서 프로그래밍 방식으로 MS Windows 또는 Linux에서 C++를 사용하여 XLSX 또는 XLS 파일을 만들 수 있습니다.
C++를 사용하여 Excel 파일을 만드는 단계
- NuGet 패키지 관리자 도구로 Aspose.Cells.Cpp 패키지 구성
- Aspose::Cells 네임스페이스에 대한 참조 추가
- Workbook 클래스 개체를 초기화하여 빈 Excel 통합 문서를 인스턴스화합니다.
- Excel 파일의 셀에 샘플 값 삽입
- C++를 사용하여 데이터를 삽입한 후 출력 Excel 파일 저장
다음 예에서는 C++*를 사용하여 *Excel 파일을 만드는 방법을 살펴봅니다. 몇 단계만 거치면 빈 통합 문서를 초기화하고 샘플 값을 셀에 삽입할 수 있습니다.
C++에서 Excel 파일을 만드는 코드
#pragma once | |
#include "Aspose.Cells.h" | |
class ExcelWorkbook | |
{ | |
void CreateExcelWorkbook() | |
{ | |
// Set the license for Aspose.Cells API for creating workbook | |
intrusive_ptr<License> CellCreateLicense = new License(); | |
CellCreateLicense->SetLicense(new String("Aspose.Total.lic")); | |
// Instantiate the Workbook object to create an empty XLSX file | |
intrusive_ptr<IWorkbook> CreateWorkbook = Factory::CreateIWorkbook(); | |
//Accessing a worksheet using its index for inserting data | |
intrusive_ptr<IWorksheet> CreateWorksheet = CreateWorkbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Adding sample data and values to cells for filtering | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A1"))->PutValue("Customers Report"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue("C_ID"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("B2"))->PutValue("C_Name"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A3"))->PutValue("C001"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("B3"))->PutValue("Customer1"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A4"))->PutValue("C002"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("B4"))->PutValue("Customer2"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A5"))->PutValue("C003"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("B5"))->PutValue("Customer3"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A6"))->PutValue("C004"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("B6"))->PutValue("Customer4"); | |
// Save the output Excel file with inserted data | |
CreateWorkbook->Save(new String ("WorkbookOutput.xlsx")); | |
} | |
}; | |
빈 통합 문서를 만들고 샘플 데이터와 값을 셀에 삽입하여 *C++*를 사용하여 Excel 파일을 생성할 수 있습니다. 이 코드 조각으로 Excel 파일을 생성하기 위해 MS Excel 또는 다른 응용 프로그램을 설치할 필요가 없습니다. 이전 예에서 XLSX 파일에 필터를 추가하는 방법을 설명하는 C++를 사용하여 XLSX 파일에 필터를 추가하는 방법에 대해 배웠습니다.