Adlandırılmış Aralıklar
Bir Adlandırılmış Aralık Oluşturma
Microsoft Excel Kullanımı
Aşağıdaki adımlar, Microsoft Excel ile hücre veya hücre aralığına ad verme işlemini tanımlar. Bu yöntem, Microsoft Office Excel 2003, Microsoft Excel 97, 2000 ve 2002 için geçerlidir.
- Adlandırmak istediğiniz hücreyi veya hücre aralığını seçin.
- Formül çubuğunun sol ucundaki Ad Kutusu’na tıklayın.
- Hücrelerin adını yazın.
- ENTER tuşuna basın.
Aspose.Cells Kullanımı
Burada görevi yapmak için Aspose.Cells API’sını kullanıyoruz.
Aspose.Cells, bir Microsoft Excel dosyasını temsil eden Workbook adlı bir sınıf sağlar. Workbook sınıfı, bir Excel dosyasındaki her çalışma sayfasına erişim sağlayan WorksheetCollection içerir. Bir çalışma sayfası Worksheet sınıfı tarafından temsil edilir. Worksheet sınıfı, Cells koleksiyonunu sağlar.
Belirli bir liste aralığını ve AutoFitterOptions örneğini kabul eden createRange metodunu aşırı yükleyerek isimlendirilmiş bir aralık oluşturabilirsiniz. createRange yönteminin tipik bir versiyonu aşağıdaki parametreleri alır:
- Sol üst hücrenin adı, aralıktaki sol üst hücrenin adı.
- Sağ alt hücrenin adı, aralıktaki sağ alt hücrenin adı.
createRange yöntemi çağrıldığında, yeni oluşturulan isimlendirilmiş aralık, Range sınıfının bir örneği olarak döner.
Aşağıdaki örnek, B4:G14’ten genişleyen hücrelerin adlandırılmış bir aralık oluşturmayı gösterir.
// 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"); |
Çalışma Kitabındaki Tüm Adlandırılmış Aralıklara Erişme
Bir elektronik tabloda tüm adlandırılmış alanları almak için WorksheetCollection koleksiyonunun getNamedRanges metodunu çağırın. getNamedRanges metodu, WorksheetCollection içindeki tüm adlandırılmış alanların dizisini döndürür.
Aşağıdaki örnek, bir çalışma kitabındaki tüm adlandırılmış aralıklara erişmeyi gösterir.
// 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); |
Belirli Bir Adlandırılmış Aralığa Erişme
Belirli bir adı olan aralığı almak için WorksheetCollection koleksiyonunun getRangeByName metodunu çağırın. Tipik getRangeByName metodu, adlandırılmış alanın adını alır ve belirli adlandırılmış alanı Range sınıfı örneği olarak döndürür.
Aşağıdaki örnek, adı verilen bir aralığa erişmeyi gösterir.
// 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()); |
Adlandırılmış Bir Aralıktaki Hücreleri Belirleme
Aspose.Cells kullanarak, bir aralıktaki bireysel hücrelere veri ekleyebilirsiniz. Varsayalım ki, A1:C4 gibi adlandırılmış bir hücre aralığınız var. Bu durumda, matris 4 * 3 = 12 hücre oluşturacak ve bireysel hücreler sıralı bir şekilde düzenlenecektir. Aspose.Cells size, aralıktaki bireysel hücrelere erişmek için Range sınıfının bazı kullanışlı özelliklerini sağlar. Aşağıdaki yöntemleri kullanarak, aralıktaki hücreleri belirleyebilirsiniz:
- getFirstRow, adlandırılmış aralıktaki ilk satırın dizinini döndürür.
- getFirstColumn, adlandırılmış aralıktaki ilk sütunun dizinini döndürür.
Aşağıdaki örnek, belirtilen bir aralıktaki hücrelere bazı değerler girmeyi gösterir.
// 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()); |
Adı Verilen Aralıktaki Hücrelere Veri Girişi
Aspose.Cells kullanarak, bir aralıktaki bireysel hücrelere veri ekleyebilirsiniz. Varsayalım ki, H1:J4 gibi adlandırılmış bir hücre aralığınız var. Bu durumda, matris 4 * 3 = 12 hücre oluşturacak ve bireysel hücreler sıralı bir şekilde düzenlenecektir. Aspose.Cells size, aralıktaki bireysel hücrelere erişmek için Range sınıfının bazı kullanışlı özelliklerini sağlar. Aşağıdaki özellikleri kullanarak, aralıktaki hücreleri belirleyebilirsiniz:
- getFirstRow, adlandırılmış aralıktaki ilk satırın dizinini döndürür.
- getFirstColumn, adlandırılmış aralıktaki ilk sütunun dizinini döndürür.
Aşağıdaki örnek, belirtilen bir aralıktaki hücrelere bazı değerler girmeyi gösterir.
// 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"); |
Aralıklara Biçim Verme…Arka Plan Rengi ve Yazı Tipi Özniteliklerini Belirleme
Biçimlendirme uygulamak için Range nesnesine stil ayarlarını belirlemek için Style nesnesini tanımlayın ve onu Range nesnesine uygulayın.
Aşağıdaki örnek, belirli bir aralığa katı dolgu rengi (gölgeleme rengi) ve yazı tipi ayarları belirlemeyi gösterir.
// 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"); |
Aralıklara Biçim Verme…Adlandırılmış Bir Aralığa Kenarlık Eklenmesi
Bir hücre aralığına sadece tek bir hücre yerine sınırlar eklemek mümkündür. Range nesnesi, aşağıdaki parametreleri alan setOutlineBorders metodunu sağlar ve bu metod, hücre aralığına sınır eklemek içindir:
- borderStyle: kenarlık türü, CellBorderType numaralandırmasından seçilir.
- borderColor: kenarlığın çizgi rengi, Color numaralandırmasından seçilir.
Aşağıdaki örnek, bir aralığa kenarlık eklemeyi gösterir.
// 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"); |
Yukarıdaki kodun çalıştırılmasından sonra aşağıdaki çıktı üretilecektir:
Bir Aralıktaki Hücrelere Stil Uygula
Bazen, bir Range içindeki hücrelere bir stil uygulamak istersiniz. Bunun için, aralıktaki hücreler üzerinde yineleyebilir ve Cell.setStyle metodunu kullanarak stili hücreye uygulayabilirsiniz.
Aşağıdaki örnek, bir Aralıktaki hücrelere stil uygulamanın nasıl yapıldığını göstermektedir.
// 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"); |
Adlandırılmış Bir Aralığı Kaldır
Aspose.Cells, NameCollection.RemoveAt() metodunu sağlar, bu metod aralık adını silmek için kullanılır. Aralık içeriğini temizlemek için Cells.ClearRange() metodunu kullanın. Aşağıdaki örnek, bir adlandırılmış aralığı içeriği ile birlikte kaldırmanın nasıl yapıldığını göstermektedir.
// 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