Dalam tutorial ini, kita akan belajar cara membuat file Excel menggunakan C++. Anda dapat membuat file XLSX atau XLS menggunakan C++ di MS Windows atau Linux secara terprogram di C++.
Langkah-langkah Membuat File Excel menggunakan C++
- Konfigurasikan paket Aspose.Cells.Cpp dengan alat pengelola paket NuGet
- Tambahkan referensi ke Aspose::Cells namespace
- Inisialisasi objek Kelas Workbook untuk membuat instance buku kerja Excel kosong
- Masukkan nilai sampel ke dalam sel file Excel
- Simpan file output Excel setelah memasukkan data menggunakan C++
Dalam contoh berikut, Anda akan menjelajahi cara membuat file Excel menggunakan C++. Inisialisasi buku kerja kosong dan masukkan nilai sampel ke dalam sel hanya dengan mengikuti beberapa langkah.
Kode untuk Membuat File Excel di C++
#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")); | |
} | |
}; | |
Anda dapat menghasilkan file Excel menggunakan C++ dengan membuat buku kerja kosong dan memasukkan data sampel dan nilai ke dalam sel. Anda tidak perlu menginstal MS Excel atau aplikasi lain untuk menghasilkan file excel dengan potongan kode ini. Pada contoh sebelumnya, kita mempelajari Cara Menambahkan Filter di File XLSX menggunakan C++ yang menjelaskan penambahan filter dalam file XLSX.