إدراج وحذف الصفوف والأعمدة
مقدمة
سواء كنت تقوم بإنشاء ورقة عمل جديدة من الصفر أو العمل على ورقة عمل موجودة ، قد نحتاج أحيانًا إلى إضافة صفوف أو أعمدة إضافية لاستيعاب المزيد من البيانات. وبالمقابل ، قد نحتاج أيضًا إلى حذف الصفوف أو الأعمدة من المواقع المحددة في ورقة العمل. لتلبية هذه المتطلبات ، يوفر Aspose.Cells مجموعة بسيطة جدًا من الفئات والأساليب ، كما هو موضح أدناه.
إدارة الصفوف والأعمدة
توفر Aspose.Cells فئة ، Workbook, التي تمثل ملف Microsoft Excel. تحتوي فئة Workbook على مجموعة Worksheets تسمح بالوصول إلى كل ورقة عمل في ملف Excel. تمثل ورقة العمل بواسطة فئة Worksheet . توفر فئة Worksheet مجموعة Cells التي تمثل جميع الخلايا في ورقة العمل.
توفر مجموعة Cells العديد من الأساليب لإدارة الصفوف والأعمدة في ورقة العمل. يتم مناقشة بعض هذه الأساليب أدناه.
إدراج صف
قم بإدراج صف في ورقة العمل في أي موقع عن طريق استدعاء طريقة InsertRow من مجموعة Cells . تأخذ طريقة InsertRow مؤشر الصف حيث سيتم إدراج الصف الجديد.
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx"; | |
//Path of output excel file | |
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Inserting a column into the worksheet at 2nd position | |
worksheet.GetCells().InsertColumn(1); | |
//Save the Excel file. | |
workbook.Save(outputInsertingDeletingRowsAndColumns); | |
Aspose::Cells::Cleanup(); |
إدراج صفوف متعددة
لإدراج عدة صفوف في ورقة العمل ، قم باستدعاء طريقة InsertRows من مجموعة Cells . تأخذ طريقة InsertRows معها معها اثنين من المعلمات:
- فهرس الصف، الفهرس للصف من حيث إن الصفوف الجديدة ستدرج.
- عدد الصفوف، إجمالي عدد الصفوف التي يجب إدراجها.
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx"; | |
//Path of output excel file | |
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Inserting 10 rows into the worksheet starting from 3rd row | |
worksheet.GetCells().InsertRows(2, 10); | |
//Save the Excel file. | |
workbook.Save(outputInsertingDeletingRowsAndColumns); | |
Aspose::Cells::Cleanup(); |
حذف صفوف متعددة
لحذف عدة صفوف من ورقة العمل ، قم باستدعاء طريقة DeleteRows من مجموعة Cells . تأخذ طريقة DeleteRows معها اثنين من المعلمات:
- فهرس الصف، الفهرس للصف من حيث سيتم حذف الصفوف.
- عدد الصفوف، الإجمالي لعدد الصفوف التي يجب حذفها.
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx"; | |
//Path of output excel file | |
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Deleting 10 rows from the worksheet starting from 3rd row | |
worksheet.GetCells().DeleteRows(2, 10); | |
//Save the Excel file. | |
workbook.Save(outputInsertingDeletingRowsAndColumns); | |
Aspose::Cells::Cleanup(); |
إدراج عمود
يمكن للمطورين أيضًا إدراج عمود في ورقة العمل في أي موقع عن طريق استدعاء طريقة InsertColumn من مجموعة Cells . تأخذ طريقة InsertColumn مؤشر العمود حيث سيتم إدراج العمود الجديد.
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx"; | |
//Path of output excel file | |
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Inserting a column into the worksheet at 2nd position | |
worksheet.GetCells().InsertColumn(1); | |
//Save the Excel file. | |
workbook.Save(outputInsertingDeletingRowsAndColumns); | |
Aspose::Cells::Cleanup(); |
حذف عمود
لحذف عمود من ورقة العمل في أي موقع ، قم باستدعاء طريقة DeleteColumn من مجموعة Cells . تأخذ طريقة DeleteColumn مؤشر العمود الذي سيتم حذفه.
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleInsertingDeletingRowsAndColumns = dirPath + u"sampleInsertingDeletingRowsAndColumns.xlsx"; | |
//Path of output excel file | |
U16String outputInsertingDeletingRowsAndColumns = outPath + u"outputInsertingDeletingRowsAndColumns.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleInsertingDeletingRowsAndColumns); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Deleting a column from the worksheet at 2nd position | |
worksheet.GetCells().DeleteColumn(1); | |
//Save the Excel file. | |
workbook.Save(outputInsertingDeletingRowsAndColumns); | |
Aspose::Cells::Cleanup(); |