إدارة أوراق العمل في ملفات Microsoft Excel.

توفر Aspose.Cells فئة، Workbook، تمثل ملف إكسل. تحتوي الفئة Workbook على مجموعة Worksheets تتيح الوصول إلى كل ورقة عمل في ملف إكسل.

تمثل ورقة العمل بفئة Worksheet. توفر الفئة Worksheet مجموعة واسعة من الخصائص والأساليب لإدارة أوراق العمل.

إضافة ورقات العمل إلى ملف Excel جديد

لإنشاء ملف Excel جديد برمجياً:

  1. إنشاء كائن من الفئة Workbook.
  2. استدعاء الطريقة Add من الفئة WorksheetCollection. يتم إضافة ورقة عمل فارغة تلقائياً إلى ملف إكسل. يمكن الإشارة إليها عن طريق تمرير فهرس الورقة الجديدة إلى مجموعة Worksheets.
  3. الحصول على مرجع ورقة العمل.
  4. القيام بالعمل على أوراق العمل.
  5. حفظ ملف إكسل الجديد مع أوراق جديدة بتوجيه طريقة Save لفئة 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.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Setting the name of the newly added worksheet
worksheet.Name = "My Worksheet";
// Saving the Excel file
workbook.Save(dataDir + "output.out.xls");

إضافة ورقات عمل إلى جدول التصميم

عملية إضافة أوراق العمل إلى جدول إكسل للمصمم هي نفس عملية إضافة ورقة عمل جديدة، باستثناء أن ملف إكسل موجود بالفعل ويجب فتحه قبل إضافة الأوراق. يمكن فتح جدول إكسل للمصمم بواسطة فئة 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.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string InputPath = dataDir + "book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Setting the name of the newly added worksheet
worksheet.Name = "My Worksheet";
// Saving the Excel file
workbook.Save(dataDir + "output.xlsx");

الوصول إلى الأوراق العمل باستخدام اسم الورقة

الوصول إلى أي ورقة عمل عن طريق تحديد اسمها أو فهرسها.

// 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);
string InputPath = dataDir + "book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing a worksheet using its sheet name
Worksheet worksheet = workbook.Worksheets["Sheet1"];
Cell cell = worksheet.Cells["A1"];
Console.WriteLine(cell.Value);

إزالة الأوراق العمل باستخدام اسم الورقة

لإزالة أوراق العمل من ملف، ادع الطريقة RemoveAt من فئة WorksheetCollection. امرر اسم الورقة إلى الطريقة RemoveAt لإزالة ورقة عمل معينة.

// 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);
// Removing a worksheet using its sheet name
workbook.Worksheets.RemoveAt("Sheet1");
// Save workbook
workbook.Save(dataDir + "output.out.xls");

إزالة الأوراق العمل باستخدام فهرس الورقة

يعمل إزالة الأوراق باسم عندما يكون اسم ورقة العمل معروفًا. إذا لم تكن تعرف اسم ورقة العمل، استخدم الطراز الزائد للطريقة RemoveAt الذي يأخذ فهرس ورقة العمل بدلاً من اسمها.

// 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);
// Removing a worksheet using its sheet index
workbook.Worksheets.RemoveAt(0);
// Save workbook
workbook.Save(dataDir + "output.out.xls");

تنشيط الأوراق وجعل خلية نشطة في ورقة العمل

أحيانًا، تحتاج ورقة عمل معينة إلى أن تكون نشطة ومعروضة عندما يفتح المستخدم ملف إكسل في إكسل. بالمثل، قد ترغب في تنشيط خلية معينة وتعيين شريطي التمرير لعرض الخلية النشطة. تمتلك Aspose.Cells القدرة على القيام بكل هذه المهام.

ورقة العمل النشطة هي الورقة التي تعمل عليها: اسم الورقة النشطة على علامة التبويب يكون سميك افتراضيًا.

الخلية النشطة هي الخلية المحددة، الخلية التي يتم إدخال البيانات فيها عند بدء الكتابة. تكون خلية واحدة فقط نشطة في وقت واحد. يتم تمييز الخلية النشطة بحد ثقيل.

تفعيل الأوراق وجعل خلية نشطة

توفر Aspose.Cells استدعاءات API محددة لتفعيل ورقة وخلية. على سبيل المثال، تعد خاصية Aspose.Cells.WorksheetCollection.ActiveSheetIndex مفيدة لضبط الورقة النشطة في دفتر العمل. بالمثل، تستخدم الخاصية Aspose.Cells.Worksheet.ActiveCell لضبط والحصول على خلية نشطة في ورقة العمل.

للتأكد من أن شريطي التمرير الأفقي أو العمودي في موضع فهرس الصف والعمود الذي تريد عرض البيانات المحددة فيه، استخدم الخصائص Aspose.Cells.Worksheet.FirstVisibleRow و Aspose.Cells.Worksheet.FirstVisibleColumn.

تُظهر الشفرة المثالية التالية كيفية تفعيل ورقة عمل وجعل خلية نشطة فيها. في الناتج المولد، ستتم تمرير أشرطة التمرير لجعل الصف الثاني والعمود الثاني أول صف وعمود مرئيين لديها.

// 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);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.Worksheets[0];
// Get the cells in the worksheet.
Cells cells = worksheet1.Cells;
// Input data into B2 cell.
cells[1, 1].PutValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.Worksheets.ActiveSheetIndex = 0;
// Set B2 cell as an active cell in the worksheet.
worksheet1.ActiveCell = "B2";
// Set the B column as the first visible column in the worksheet.
worksheet1.FirstVisibleColumn = 1;
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.FirstVisibleRow = 1;
// Save the excel file.
workbook.Save(dataDir + "output.xls");

مواضيع متقدمة