النطاقات المسماة

إنشاء نطاق مسمى

استخدام Microsoft Excel

تصف الخطوات التالية كيفية تسمية خلية أو نطاق من الخلايا باستخدام Microsoft Excel. هذه الطريقة تنطبق على برنامج Microsoft Office Excel 2003، و Microsoft Excel 97، 2000 و 2002.

  1. حدد الخلية أو نطاق الخلايا الذي تريد تسميته.
  2. انقر فوق مربع الاسم في الطرف الأيسر من شريط الصيغة.
  3. اكتب اسم الخلايا.
  4. اضغط على ENTER.

استخدام Aspose.Cells

هنا، نستخدم واجهة برمجة التطبيقات Aspose.Cells للقيام بالمهمة.

توفر Aspose.Cells فئة، Workbook، التي تمثل ملف Microsoft Excel. تحتوي فئة Workbook على WorksheetCollection التي تسمح بالوصول إلى كل ورقة عمل في ملف Excel. يُمثل ورقة العمل بواسطة فئة Worksheet. توفر فئة Worksheet مجموعة Cells.

من الممكن إنشاء نطاق مسمى عن طريق استدعاء طراز createRange المتحمل لمجموعة Cells. يأخذ الإصدار النمطي لطراز createRange المتحمل التالي المعلمات:

  • اسم الخلية العلوي الأيمن، اسم الخلية العلوي الأيسر في النطاق.
  • اسم الخلية السفلي الأيمن، اسم الخلية السفلي الأيمن في النطاق.

عند استدعاء طراز createRange، يُرجع النطاق المسمى الجديد كمثيل لفئة Range.

يوضح المثال التالي كيفية إنشاء نطاق مسمى من الخلايا التي تمتد من B4 إلى G14.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CreateNamedRangeofCells.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Creating a named range
Range namedRange = cells.createRange("B4", "G14");
namedRange.setName("TestRange");
// Saving the modified Excel file in default (that is Excel 2000) format
workbook.save(dataDir + "CNROfCells_out.xls");
// Print message
System.out.println("Process completed successfully");

الوصول إلى جميع النطاقات المسماة في ورق عمل

استدعاء طراز getNamedRanges لـ WorksheetCollection للحصول على جميع النطاقات المسماة في ورق عمل. يُرجع طراز getNamedRanges مصفوفة بجميع النطاقات المسماة في WorksheetCollection.

يوضح المثال التالي كيفية الوصول إلى جميع النطاقات المسماة في ورق عمل.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AccessAllNamedRanges.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Getting all named ranges
Range[] namedRanges = worksheets.getNamedRanges();
// Print message
System.out.println("Number of Named Ranges : " + namedRanges.length);

الوصول إلى نطاق مسمى محدد

اتصل بمجموعة WorksheetCollection باستخدام الطريقة getRangeByName للحصول على نطاق محدد بواسطة الاسم. تأخذ الطريقة النموذجية getRangeByName اسم النطاق المسمى وتعيد النطاق المسمى المحدد كمثيل لفئة Range.

يظهر المثال التالي كيفية الوصول إلى نطاق محدد بواسطة اسمه.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AccessSpecificNamedRange.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Getting the specified named range
Range namedRange = worksheets.getRangeByName("TestRange");
// Print message
System.out.println("Named Range : " + namedRange.getRefersTo());

تحديد الخلايا في نطاق مسمى

باستخدام Aspose.Cells, يمكنك إدخال البيانات في الخلايا الفردية في النطاق. فلنفترض أن لديك نطاقًا مسمى للخلايا.أي، A1:C4. لذلك تجعل المصفوفة 4 * 3 = 12 خلية وترتب الخلايا في النطاق المسمى بشكل متسلسل. يوفر Aspose.Cells بعض الخصائص المفيدة لفئة Range للوصول إلى الخلايا الفردية في النطاق. يمكنك استخدام الطرق التالية لتحديد الخلايا في النطاق:

  • getFirstRow تعيد فهرس الصف الأول في النطاق المسمى.
  • getFirstColumn تعيد فهرس العمود الأول في النطاق المسمى.

يظهر المثال التالي كيفية إدخال بعض القيم في الخلايا لنطاق معين.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(IdentifyCellsinNamedRange.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Getting the specified named range
Range range = worksheets.getRangeByName("TestRange");
// Identify range cells.
System.out.println("First Row : " + range.getFirstRow());
System.out.println("First Column : " + range.getFirstColumn());
System.out.println("Row Count : " + range.getRowCount());
System.out.println("Column Count : " + range.getColumnCount());

إدخال البيانات في الخلايا في النطاق المسمى

باستخدام Aspose.Cells, يمكنك إدخال البيانات في الخلايا الفردية في النطاق. فلنفترض أن لديك نطاقًا مسمى للخلايا أي، H1:J4. لذلك تجعل المصفوفة 4 * 3 = 12 خلية وترتب الخلايا في النطاق المسمى بشكل متسلسل. يوفر Aspose.Cells بعض الخصائص المفيدة لفئة Range للوصول إلى الخلايا الفردية في النطاق. يمكنك استخدام الخصائص التالية لتحديد الخلايا في النطاق:

  • getFirstRow تعيد فهرس الصف الأول في النطاق المسمى.
  • getFirstColumn تعيد فهرس العمود الأول في النطاق المسمى.

يظهر المثال التالي كيفية إدخال بعض القيم في الخلايا لنطاق معين.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(InputDataInCellsInRange.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Create a range of cells and specify its name based on H1:J4.
Range range = worksheet1.getCells().createRange("H1:J4");
range.setName("MyRange");
// Input some data into cells in the range.
range.get(0, 0).setValue("USA");
range.get(0, 1).setValue("SA");
range.get(0, 2).setValue("Israel");
range.get(1, 0).setValue("UK");
range.get(1, 1).setValue("AUS");
range.get(1, 2).setValue("Canada");
range.get(2, 0).setValue("France");
range.get(2, 1).setValue("India");
range.get(2, 2).setValue("Egypt");
range.get(3, 0).setValue("China");
range.get(3, 1).setValue("Philipine");
range.get(3, 2).setValue("Brazil");
// Save the excel file.
workbook.save(dataDir + "IDICInRange_out.xls");
// Print message
System.out.println("Process completed successfully");

تنسيق النطاقات…إعداد لون الخلفية وسمات الخط لنطاق مسمى

لتطبيق التنسيق، قم بتعريف Style لتحديد إعدادات النمط وتطبيقه على Range.

يظهر المثال التالي كيفية تعيين لون تعبئة صلب (لون الظل) مع إعدادات الخط إلى نطاق.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormatRanges1.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the book.
Worksheet WS = workbook.getWorksheets().get(0);
// Create a named range of cells.
com.aspose.cells.Range range = WS.getCells().createRange(1, 1, 1, 17);
range.setName("MyRange");
// Declare a style object.
Style stl;
// Create the style object with respect to the style of a cell.
stl = WS.getCells().get(1, 1).getStyle();
// Specify some Font settings.
stl.getFont().setName("Arial");
stl.getFont().setBold(true);
// Set the font text color
stl.getFont().setColor(Color.getRed());
// To Set the fill color of the range, you may use ForegroundColor with
// solid Pattern setting.
stl.setBackgroundColor(Color.getYellow());
stl.setPattern(BackgroundType.SOLID);
// Apply the style to the range.
for (int r = 1; r < 2; r++) {
for (int c = 1; c < 18; c++) {
WS.getCells().get(r, c).setStyle(stl);
}
}
// Save the excel file.
workbook.save(dataDir + "FormatRanges1_out.xls");
// Print message
System.out.println("Process completed successfully");

تنسيق النطاقات…إضافة حدود إلى نطاق مسمى

من الممكن إضافة حدود إلى مجموعة من الخلايا بدلاً من خلية واحدة فقط. توفر فئة Range طريقة setOutlineBorders التي تأخذ المعلمات التالية لإضافة حد لمجموعة الخلايا:

  • نمط الحد: نوع الحد، محدد من تعداد CellBorderType.
  • لون الحد: لون الخط للحد، محدد من تعداد Color.

يظهر المثال التالي كيفية تعيين حد للنطاق.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormatRanges2.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
// Obtaining the reference of the newly added worksheet
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello World From Aspose");
// Creating a range of cells starting from "A1" cell to 3rd column in a
// row
Range range = worksheet.getCells().createRange("A1:C1");
range.setName("MyRange");
// Adding a thick outline border with the blue line
range.setOutlineBorders(CellBorderType.THICK, Color.getBlue());
// Saving the Excel file
workbook.save(dataDir + "FormatRanges2_out.xls");
// Print message
System.out.println("Process completed successfully");

سيتم توليد الإخراج التالي بعد تنفيذ الشفرة أعلاه:

todo:image_alt_text

تطبيق النمط الى الخلايا في مجموعة

أحيانًا، ترغب في إنشاء تطبيق النمط على الخلايا في Range. لهذا الغرض، قد تكرر عبر الخلايا في النطاق وتستخدم طريقة Cell.setStyle لتطبيق النمط على الخلية.

المثال التالي يوضح كيفية تطبيق الأنماط على الخلايا في مجموعة.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ConvertCellsAddresstoRangeorCellArea.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the newly added worksheet
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello World!");
// Creating a range of cells based on cells Address.
Range range = worksheet.getCells().createRange("A1:F10");
// Specify a Style object for borders.
Style style = cell.getStyle();
// Setting the line style of the top border
style.setBorder(BorderType.TOP_BORDER, CellBorderType.THICK, Color.getBlack());
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.THICK, Color.getBlack());
style.setBorder(BorderType.LEFT_BORDER, CellBorderType.THICK, Color.getBlack());
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.THICK, Color.getBlack());
Iterator cellArray = range.iterator();
while (cellArray.hasNext()) {
Cell temp = (Cell) cellArray.next();
// Saving the modified style to the cell.
temp.setStyle(style);
}
// Saving the Excel file
workbook.save(dataDir + "CCAToROrCArea_out.xls");

إزالة نطاق مسمى

توفر Aspose.Cells الطريقة NameCollection.RemoveAt() لمسح اسم النطاق. لمسح محتويات النطاق، استخدم طريقة Cells.ClearRange() ميثود. المثال التالي يوضح كيفية إزالة نطاق مسمى مع محتوياته.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(RemoveANamedRange.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.getWorksheets();
// Get the first worksheet in the worksheets collection.
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create a range of cells.
Range range1 = worksheet.getCells().createRange("E12", "I12");
// Name the range.
range1.setName("MyRange");
// Set the outline border to the range.
range1.setOutlineBorder(BorderType.TOP_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
range1.setOutlineBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
range1.setOutlineBorder(BorderType.LEFT_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
range1.setOutlineBorder(BorderType.RIGHT_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
// Input some data with some formattings into
// a few cells in the range.
range1.get(0, 0).setValue("Test");
range1.get(0, 4).setValue("123");
// Create another range of cells.
Range range2 = worksheet.getCells().createRange("B3", "F3");
// Name the range.
range2.setName("testrange");
// Copy the first range into second range.
range2.copy(range1);
// Remove the previous named range (range1) with its contents.
worksheet.getCells().clearRange(11, 4, 11, 8);
worksheets.getNames().removeAt(0);
// Save the excel file.
workbook.save(dataDir + "RANRange_out.xls");
// Print message
System.out.println("Process completed successfully");

borderColors