ในบทช่วยสอนนี้ เราจะเรียนรู้วิธี สร้างไฟล์ Excel โดยใช้ C+* คุณสามารถสร้างไฟล์ XLSX หรือ XLS โดยใช้ C++ บน MS Windows หรือ Linux โดยทางโปรแกรมใน C++
ขั้นตอนในการสร้างไฟล์ Excel ด้วย C++
- กำหนดค่าแพ็คเกจ Aspose.Cells.Cpp ด้วยเครื่องมือจัดการแพ็คเกจของ NuGet
- เพิ่มการอ้างอิงไปยังเนมสเปซ Aspose::Cells
- เริ่มต้นวัตถุคลาส Workbook เพื่อสร้างตัวอย่างสมุดงาน Excel เปล่า
- ใส่ค่าตัวอย่างลงในเซลล์ของไฟล์ Excel
- บันทึกไฟล์ Excel ที่ส่งออกหลังจากใส่ข้อมูลโดยใช้ C ++
ในตัวอย่างต่อไปนี้ คุณจะได้สำรวจวิธีการ *สร้างไฟล์ Excel โดยใช้ C++ เริ่มต้นสมุดงานเปล่าและใส่ค่าตัวอย่างลงในเซลล์ได้ง่ายๆ โดยทำตามขั้นตอนไม่กี่ขั้นตอน
รหัสเพื่อสร้างไฟล์ Excel ใน 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")); | |
} | |
}; | |
คุณสามารถ *สร้างไฟล์ Excel โดยใช้ C++ โดยสร้างสมุดงานเปล่าและใส่ข้อมูลตัวอย่างและค่าลงในเซลล์ คุณไม่จำเป็นต้องติดตั้ง MS Excel หรือแอปพลิเคชันอื่นใดเพื่อสร้างไฟล์ excel ด้วยข้อมูลโค้ดนี้ ในตัวอย่างก่อนหน้านี้ เราได้เรียนรู้ วิธีเพิ่มตัวกรองในไฟล์ XLSX โดยใช้ C ++ ที่อธิบายการเพิ่มตัวกรองในไฟล์ XLSX