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

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

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

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

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

  1. قم بإنشاء كائن مندفتر العملصف دراسي.
  2. اتصل بيضيف طريقةورقة العمل صف دراسي. تتم إضافة ورقة عمل فارغة إلى ملف Excel تلقائيًا. يمكن الرجوع إليه عن طريق تمرير فهرس ورقة ورقة العمل الجديدة إلى ملفأوراق عمل مجموعة.
  3. الحصول على مرجع ورقة العمل.
  4. أداء العمل على أوراق العمل.
  5. احفظ ملف Excel الجديد بأوراق عمل جديدة عن طريق استدعاء ملفدفتر العمل صف دراسي'يحفظطريقة.
// 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");

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

عملية إضافة أوراق العمل إلى جدول بيانات المصمم مماثلة لعملية إضافة ورقة عمل جديدة ، باستثناء أن ملف Excel موجود بالفعل ، لذا يجب فتحه قبل إضافة أوراق العمل. يمكن فتح جدول بيانات المصمم بواسطةدفتر العملصف دراسي.

// 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 طريقةورقة العمل صف دراسي. قم بتمرير اسم الورقة إلى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");

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

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

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

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

تنشيط الأوراق وتنشيط Cell

يوفر 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");

موضوعات مسبقة