في هذا المثال ، سوف نستكشف كيفية ** تحرير ملف Excel في C ++ **. يمكنك تحرير ملف Excel برمجيًا في C ++ باستخدام واجهة API بسيطة يمكن استخدامها في كل من Microsoft Windows و Linux وما إلى ذلك.
خطوات تحرير ملف Excel في C ++
- أضف Aspose.Cells.Cpp باستخدام أداة NuGet package Manager
- أضف مرجعًا إلى مساحة الاسم Aspose::Cells
- إنشاء مثيل لكائن Workbook لتحميل ملف Excel لتحريره
- قم بالوصول إلى الخلايا A1 و C1 داخل ورقة العمل وقم بتعيين البيانات وسلسلة الصيغة على التوالي
- احسب صيغة المصنف
- احفظ مصنف الإخراج مع الصيغة إلى 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 ++.