วิธีแก้ไขไฟล์ Excel ใน C++

ในตัวอย่างนี้ เราจะศึกษาวิธี แก้ไข Excel ไฟล์ใน C+ คุณสามารถแก้ไขไฟล์ Excel โดยทางโปรแกรมใน C++ โดยใช้อินเทอร์เฟซ API อย่างง่ายที่ใช้ได้ทั้งใน Microsoft Windows และ Linux เป็นต้น

ขั้นตอนการแก้ไขไฟล์ Excel ใน C++

  1. เพิ่ม Aspose.Cells.Cpp ด้วยเครื่องมือ NuGet package Manager
  2. เพิ่มการอ้างอิงไปยังเนมสเปซ Aspose::Cells
  3. สร้างอินสแตนซ์ของวัตถุ Workbook เพื่อโหลดไฟล์ Excel เพื่อแก้ไข
  4. เข้าถึงเซลล์ A1 และ C1 ภายในแผ่นงานและตั้งค่าข้อมูลและสตริงสูตรตามลำดับ
  5. คำนวณสูตรสำหรับสมุดงาน
  6. บันทึกสมุดงานเอาต์พุตพร้อมสูตรเป็น XLSX ใน C ++

ตัวอย่างต่อไปนี้ใช้เพื่อเข้าถึงสมุดงานและ อัปเดตไฟล์ Excel ใน C++ อย่างรวดเร็วและง่ายดายโดยใช้การเรียก API เพียงไม่กี่ครั้ง เมื่อใช้ C++ คุณสามารถตั้งค่าข้อมูลเนื้อหาใดๆ ไปยังเซลล์ Excel โดยทางโปรแกรม เช่น วันที่ เปอร์เซ็นต์ ตัวเลข หรือค่าอื่นๆ

รหัสเพื่อแก้ไขไฟล์ Excel ใน C ++

#pragma once
#include "Aspose.Cells.h"
class ExcelFileEditing
{
void UpdateCellInExcel()
{
// Add Aspose.Cells for C++ API License
intrusive_ptr<License> CellRenderingLicense = new License();
CellRenderingLicense->SetLicense(new String("CPP.Aspose.Total.lic"));
// Instantiate the Workbook class object to load Excel file for editing
intrusive_ptr<IWorkbook> WbWithDataAndFormula = Factory::CreateIWorkbook();
// Access cell A1 from a first worksheet to set data
intrusive_ptr <ICell> DataCell = WbWithDataAndFormula->GetIWorksheets()->GetObjectByIndex(0)->
GetICells()->GetObjectByIndex(new String("A1"));
// Set some value in cell
DataCell->PutValue(100);
// Access cell C1 from first worksheet to update formula
intrusive_ptr <ICell> ForumulCell = WbWithDataAndFormula->GetIWorksheets()->GetObjectByIndex(0)->
GetICells()->GetObjectByIndex(new String("C1"));
// Update the formula for selected cell
ForumulCell->SetFormula(new String("=Sum(A1,A20)"));
// Calculate the workbook after formula is updated
WbWithDataAndFormula->CalculateFormula();
// Save the output workbook with formula to XLSX
WbWithDataAndFormula->Save(new String("WorkbookWithFormula.xlsx"));
}
};

กระบวนการทั้งหมดของ การแก้ไขไฟล์ Excel ใน C++ ทำได้โดยไม่ต้องพึ่งพา Microsoft Office หรือ Interop ณ จุดนี้ หากคุณต้องการบันทึกไฟล์ Excel เป็น PDF คุณสามารถสำรวจตัวอย่าง วิธีแปลง Excel เป็น PDF โดยใช้ C++

 ไทย