إدراج وحذف الصفوف والأعمدة من ملف إكسل
مقدمة
سواء كنت تقوم بإنشاء ورقة عمل جديدة من الصفر أو العمل في ورقة عمل موجودة، قد نحتاج إلى إضافة صفوف أو أعمدة إضافية لاستيعاب المزيد من البيانات. بالعكس، قد نحتاج أيضًا إلى حذف صفوف أو أعمدة من مواقع محددة في ورقة العمل. لتلبية هذه المتطلبات، توفر Aspose.Cells مجموعة بسيطة جدًا من الفئات والأساليب، والتي سنناقشها أدناه.
إدارة الصفوف والأعمدة
توفر Aspose.Cells فئة Workbook، التي تمثل ملف Excel من Microsoft. تحتوي الفئة Workbook على مجموعة Worksheets التي تسمح بالوصول إلى كل ورقة عمل في ملف Excel. تمثل ورقة العمل بفئة Worksheet. توفر الفئة Worksheet مجموعة Cells التي تمثل جميع الخلايا في ورقة العمل.
تقدم مجموعة الفئة Cells العديد من الأساليب لإدارة الصفوف والأعمدة في ورقة عمل. يتم مناقشة بعض هذه الأساليب أدناه.
إدراج الصفوف والأعمدة
كيفية إدراج صف
قم بإدراج صف في ورقة العمل في أي موقع عن طريق استدعاء الInsertRow من مجموعة الCells. يأخذ الInsertRow المؤشر للصف الذي سيتم إدراج الصف الجديد فيه.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Inserting a row into the worksheet at 3rd position | |
worksheet.Cells.InsertRow(2); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
كيفية إدراج عدة صفوف
لإدراج عدة صفوف في ورقة العمل، اتصل بالطريقة InsertRows في تجميعة Cells. طريقة InsertRows تأخذ محددتين:
- فهرس الصف، الفهرس للصف من حيث إن الصفوف الجديدة ستدرج.
- عدد الصفوف، إجمالي عدد الصفوف التي يجب إدراجها.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Inserting 10 rows into the worksheet starting from 3rd row | |
worksheet.Cells.InsertRows(2, 10); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
كيفية إدراج صف مع تنسيق
لإدراج صف بخيارات تنسيق، استخدم الحمولة InsertRows التي تأخذ InsertOptions كمعلمة. ثبّت خصية CopyFormatType لفئة InsertOptions بقيمة الترقيم CopyFormatType. الفئة CopyFormatType لها ثلاثة أعضاء كما هو مدرج أدناه.
- SameAsAbove: ينسق الصف نفسه كصف الذي في الأعلى.
- SameAsBelow: ينسق الصف نفسه كصف الذي في الأسفل.
- Clear: يُمسح التنسيق.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Setting Formatting options | |
InsertOptions insertOptions = new InsertOptions(); | |
insertOptions.CopyFormatType = CopyFormatType.SameAsAbove; | |
// Inserting a row into the worksheet at 3rd position | |
worksheet.Cells.InsertRows(2, 1, insertOptions); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "InsertingARowWithFormatting.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
كيفية إدراج عمود
يمكن للمطورين أيضًا إدراج عمود في ورقة العمل في أي موقع عن طريق استدعاء الطريقة InsertColumn في تجميعة Cells. الطريقة InsertColumn تأخذ مؤشر العمود حيث سيتم إدراج العمود الجديد.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Inserting a column into the worksheet at 2nd position | |
worksheet.Cells.InsertColumn(1); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
حذف الصفوف والأعمدة
كيفية حذف عدة صفوف
لحذف صفوف متعددة من ورقة العمل، اتصل بالطريقة DeleteRows في تجميعة Cells. الطريقة DeleteRows تأخذ محددتين:
- فهرس الصف، الفهرس للصف من حيث سيتم حذف الصفوف.
- عدد الصفوف، الإجمالي لعدد الصفوف التي يجب حذفها.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.OpenOrCreate); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Deleting 10 rows from the worksheet starting from 3rd row | |
worksheet.Cells.DeleteRows(2, 10); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
كيفية حذف عمود
لحذف عمود من ورقة العمل في أي موقع، اتصل بالطريقة DeleteColumn في تجميعة Cells. الطريقة DeleteColumn تأخذ مؤشر العمود للحذف.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Deleting a column from the worksheet at 5th position | |
worksheet.Cells.DeleteColumn(4); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |