إدارة أوراق العمل
Contents
[
Hide
]
يمكن للمطورين إنشاء أوراق العمل وإدارتها بسهولة في ملفات Excel Microsoft برمجيًا باستخدام Aspose.Cells مرن API. يصف هذا الموضوع طرق إضافة أوراق العمل وإزالتها في ملفات Excel Microsoft.
Aspose.Cells يوفر فئةIWorkbook يمثل ملف Excel. الIWorkbook فئة تحتوي علىأوراق عملمجموعة تسمح بالوصول إلى كل ورقة عمل في ملف Excel.
يتم تمثيل ورقة العمل بواسطةIWorksheet صف دراسي. الIWorksheetيوفر class مجموعة واسعة من الأساليب لإدارة أوراق العمل.
إضافة أوراق عمل إلى ملف Excel جديد
لإنشاء ملف Excel جديد برمجيًا:
- قم بإنشاء كائن منIWorksheetصف دراسي.
- اتصل بيضيف طريقةIWorksheetCollection مجموعة. تتم إضافة ورقة عمل فارغة إلى ملف Excel تلقائيًا. يمكن الرجوع إليه عن طريق تمرير فهرس ورقة ورقة العمل الجديدة إلى ملفIWorksheetCollectionمجموعة.
- الحصول على مرجع ورقة العمل.
- أداء العمل على أوراق العمل.
- احفظ ملف Excel الجديد بأوراق عمل جديدة عن طريق استدعاء ملفIWorkbook صف دراسييحفظطريقة.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Output directory path | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
//Path of output excel file | |
StringPtr outputManageWorksheets = outDir->StringAppend(new String("outputManageWorksheets.xlsx")); | |
//Create workbook | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(); | |
// Adding a new worksheet to the Workbook object | |
int i = workbook->GetIWorksheets()->Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(i); | |
// Setting the name of the newly added worksheet | |
worksheet->SetName(new String("My Worksheet")); | |
// Save the Excel file. | |
workbook->Save(outputManageWorksheets); | |
StringPtr msg = new String("New worksheet added successfully with in a workbook!"); | |
Console::WriteLine(msg); |
الوصول إلى أوراق العمل باستخدام فهرس الورقة
يوضح نموذج التعليمات البرمجية التالي كيفية الوصول إلى أي ورقة عمل أو الحصول عليها عن طريق تحديد الفهرس الخاص بها.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Source directory path | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
//Path of input excel file | |
StringPtr sampleManageWorksheets = srcDir->StringAppend(new String("sampleManageWorksheets.xlsx")); | |
//Load the sample Excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleManageWorksheets); | |
//Accessing a worksheet using its index | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Access the cell by its name. | |
intrusive_ptr<ICell> cell = worksheet->GetICells()->GetObjectByIndex(new String("F7")); | |
//Print the value of cell F7 | |
StringPtr val = cell->GetStringValue(); | |
//Print the value on console. | |
Console::Write(new String("Value of cell F7: ")); | |
Console::WriteLine(val); |
إزالة أوراق العمل باستخدام فهرس الورقة
تعمل إزالة أوراق العمل حسب الاسم بشكل جيد عندما يكون اسم ورقة العمل معروفًا. إذا كنت لا تعرف اسم ورقة العمل ، فاستخدم إصدارًا محملاً بشكل زائد من ملفRemoveAtالطريقة التي تأخذ فهرس ورقة العمل بدلاً من اسم الورقة الخاص بها.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Source directory path | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
//Output directory path | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
//Path of input excel file | |
StringPtr sampleManageWorksheets = srcDir->StringAppend(new String("sampleManageWorksheets.xlsx")); | |
//Path of output excel file | |
StringPtr outputManageWorksheets = outDir->StringAppend(new String("outputManageWorksheets.xlsx")); | |
//Load the sample Excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleManageWorksheets); | |
//Removing a worksheet using its sheet index | |
workbook->GetIWorksheets()->RemoveAt(0); | |
//Save the Excel file. | |
workbook->Save(outputManageWorksheets); |