إدراج وحذف الصفوف والأعمدة من ملف إكسل

مقدمة

سواء كنت تقوم بإنشاء ورقة عمل جديدة من الصفر أو العمل في ورقة عمل موجودة، قد نحتاج إلى إضافة صفوف أو أعمدة إضافية لاستيعاب المزيد من البيانات. بالعكس، قد نحتاج أيضًا إلى حذف صفوف أو أعمدة من مواقع محددة في ورقة العمل. لتلبية هذه المتطلبات ، توفر Aspose.Cells for Python via .NET مجموعة بسيطة جدًا من الصفوف والأساليب ، التي سيتم مناقشتها أدناه.

إدارة الصفوف والأعمدة

تقدم Aspose.Cells for Python via .NET فئة Workbook ، التي تمثل ملف Microsoft Excel. تحتوي الفئة Workbook على مجموعة worksheets التي تسمح بالوصول إلى كل ورقة عمل في ملف Excel. تمثل ورقة العمل بواسطة فئة Worksheet. توفر الفئة Worksheet مجموعة cells التي تمثل جميع الخلايا في ورقة العمل.

تقدم مجموعة الفئة cells العديد من الأساليب لإدارة الصفوف والأعمدة في ورقة عمل. يتم مناقشة بعض هذه الأساليب أدناه.

إدراج الصفوف والأعمدة

كيفية إدراج صف

قم بإدراج صف في ورقة العمل في أي موقع عن طريق استدعاء الinsert_row من مجموعة الcells. يأخذ الinsert_row المؤشر للصف الذي سيتم إدراج الصف الجديد فيه.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Inserting a row into the worksheet at 3rd position
worksheet.cells.insert_row(2)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

كيفية إدراج عدة صفوف

لإدراج عدة صفوف في ورقة العمل، اتصل بالطريقة insert_rows في تجميعة cells. طريقة insert_rows تأخذ محددتين:

  • فهرس الصف، الفهرس للصف من حيث إن الصفوف الجديدة ستدرج.
  • عدد الصفوف، إجمالي عدد الصفوف التي يجب إدراجها.
from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Inserting 10 rows into the worksheet starting from 3rd row
worksheet.cells.insert_rows(2, 10)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

كيفية إدراج صف مع تنسيق

لإدراج صف بخيارات تنسيق، استخدم الحمولة insert_rows التي تأخذ InsertOptions كمعلمة. ثبّت خصية copy_format_type لفئة InsertOptions بقيمة الترقيم CopyFormatType. الفئة CopyFormatType لها ثلاثة أعضاء كما هو مدرج أدناه.

  • SAME_AS_ABOVE: ينسق الصف نفس الصف أعلاه.
  • SAME_AS_BELOW: ينسق الصف نفس الصف أدناه.
  • CLEAR: يُمسح التنسيق.
from aspose.cells import CopyFormatType, InsertOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting Formatting options
insertOptions = InsertOptions()
insertOptions.copy_format_type = CopyFormatType.SAME_AS_ABOVE
# Inserting a row into the worksheet at 3rd position
worksheet.cells.insert_rows(2, 1, insertOptions)
# Saving the modified Excel file
workbook.save(dataDir + "InsertingARowWithFormatting.out.xls")
# Closing the file stream to free all resources
fstream.close()

كيفية إدراج عمود

يمكن للمطورين أيضًا إدراج عمود في ورقة العمل في أي موقع عن طريق استدعاء الطريقة insert_column في تجميعة cells. الطريقة insert_column تأخذ مؤشر العمود حيث سيتم إدراج العمود الجديد.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Inserting a column into the worksheet at 2nd position
worksheet.cells.insert_column(1)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

حذف الصفوف والأعمدة

كيفية حذف عدة صفوف

لحذف صفوف متعددة من ورقة العمل، اتصل بالطريقة delete_rows في تجميعة cells. الطريقة delete_rows تأخذ محددتين:

  • فهرس الصف، الفهرس للصف من حيث سيتم حذف الصفوف.
  • عدد الصفوف، الإجمالي لعدد الصفوف التي يجب حذفها.
from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "Book1.xlsx", "wb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Deleting 10 rows from the worksheet starting from 3rd row
worksheet.cells.delete_rows(2, 10)
# Saving the modified Excel file
workbook.save(dataDir + "output.xlsx")
# Closing the file stream to free all resources
fstream.close()

كيفية حذف عمود

لحذف عمود من ورقة العمل في أي موقع، اتصل بالطريقة delete_column في تجميعة Cells. الطريقة delete_column تأخذ مؤشر العمود للحذف.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "Book1.xlsx", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Deleting a column from the worksheet at 5th position
worksheet.cells.delete_column(4)
# Saving the modified Excel file
workbook.save(dataDir + "output.xlsx")
# Closing the file stream to free all resources
fstream.close()