Veri Biçimlendirme

Hücre Verilerini Biçimlendirme Yaklaşımları

Çalışma sayfası hücreleri uygun şekilde biçimlendirildiğinde, hücre içeriğini okuyan kullanıcılar için işlerin daha kolay hale geldiği yaygın bir gerçektir. Hücreleri ve içeriklerini biçimlendirmenin birçok yolu vardır. En basit yol, bir Tasarımcı Elektronik Tablo oluştururken Microsoft Excel’i kullanarak hücreleri biçimlendirmektir. Tasarımcı elektronik tablo oluşturulduktan sonra Aspose.Cells kullanarak bu elektronik tabloyu açabilir ve tüm biçim ayarlarını kaydedebilirsiniz. Hücreleri ve içeriklerini biçimlendirmenin diğer bir yolu ise Aspose.Cells API’sini kullanmaktır. Bu konuda, Aspose.Cells API’nin kullanımıyla hücreleri ve içeriklerini biçimlendirmenin iki yaklaşımını açıklayacağız.

Hücreleri Biçimlendirme

Geliştiriciler, Aspose.Cells’in esnek API’sini kullanarak hücreleri ve içeriklerini biçimlendirebilir. Aspose.Cells, bir Microsoft Excel dosyasını temsil eden Workbook sınıfını 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ı bir Hücreler koleksiyonu sağlar. Cells koleksiyonundaki her öğe, Cell sınıfının bir nesnesini temsil eder.

Aspose.Cells, Cell sınıfında, bir hücrenin biçimlendirme stili ayarlamak için kullanılan Style özelliğini sağlar. Ayrıca, aynı amaca hizmet eden bir Style sınıfı da sağlar. Hücrelere farklı türde biçimlendirme stilleri uygulayarak arka plan veya ön plan renklerini, kenarlıkları, yazı tiplerini, yatay ve dikey hizalamaları, girinti seviyesini, metin yönünü, döndürme açısını ve çok daha fazlasını ayarlayabilirsiniz.

setStyle Yöntemi Kullanma

Farklı hücrelere farklı biçimlendirme stilleri uygularken, Cell sınıfının setStyle yöntemini kullanmak daha iyidir. Aşağıda, bir hücreye çeşitli biçimlendirme ayarları uygulamak için setStyle yönteminin nasıl kullanılacağını gösteren bir örnek verilmiştir.

// 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.getDataDir(FormattingCellsUsingsetStyleMethod.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Accessing the "A1" cell from the worksheet
Cell cell = cells.get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello Aspose!");
Style style = cell.getStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the "A1" cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Setting the cell to shrink according to the text contained in it
style.setShrinkToFit(true);
// Setting the bottom border
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "output.xls");

Stil Nesnesi Kullanma

Aynı biçimlendirme stiline farklı hücrelere uygularken, Style nesnesini kullanın.

  1. Workbook sınıfının Styles koleksiyonuna, Workbook sınıfının createStyle metodunu çağırarak Style nesnesi ekleyin.
  2. Styles koleksiyonundan yeni eklenen Style nesnesine erişin.
  3. Style nesnesinin istenen özelliklerini ayarlayarak istenen biçimlendirme ayarlarını uygulayın.
  4. Konfigure edilmiş Style nesnesini istenen hücrenin Style özelliğine atayın.

Bu yaklaşım uygulamalarınızın verimliliğini büyük ölçüde artırabilir ve aynı zamanda bellek tasarrufu sağlayabilir.

// 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(FormattingCellsUsingStyleObject.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Accessing the "A1" cell from the worksheet
Cell cell = cells.get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello Aspose!");
// Adding a new Style to the styles collection of the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the "A1" cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Setting the cell to shrink according to the text contained in it
style.setShrinkToFit(true);
// Setting the bottom border
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "FCUsingStyleObject_out.xls");

Gradyan Dolgu Efektleri Uygulama

Hücreye istenen Gradyan Dolgu Efektlerini uygulamak için, Style nesnesinin setTwoColorGradient metodu uygun şekilde kullanılır.

Kod Örneği

Aşağıdaki çıktı, aşağıdaki kodu çalıştırarak elde edilir.

Gradyan Dolgu Efektlerinin Uygulanması

todo:image_alt_text

// 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(ApplyGradientFillEffects.class) + "data/";
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first worksheet (default) in the workbook
Worksheet worksheet = workbook.getWorksheets().get(0);
// Input a value into B3 cell
worksheet.getCells().get(2, 1).putValue("test");
// Get the Style of the cell
Style style = worksheet.getCells().get("B3").getStyle();
// Set Gradient pattern on
style.setGradient(true);
// Specify two color gradient fill effects
style.setTwoColorGradient(Color.fromArgb(255, 255, 255), Color.fromArgb(79, 129, 189),
GradientStyleType.HORIZONTAL, 1);
// Set the color of the text in the cell
style.getFont().setColor(Color.getRed());
// Specify horizontal and vertical alignment settings
style.setHorizontalAlignment(TextAlignmentType.CENTER);
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Apply the style to the cell
worksheet.getCells().get("B3").setStyle(style);
// Set the third row height in pixels
worksheet.getCells().setRowHeightPixel(2, 53);
// Merge the range of cells (B3:C3)
worksheet.getCells().merge(2, 1, 1, 2);
// Save the Excel file
workbook.save(dataDir + "ApplyGradientFillEffects_out.xlsx");

Hizalama Ayarlarının Yapılandırılması

Hücreleri biçimlendirmek için Microsoft Excel kullanan herkes, Microsoft Excel’deki hizalama ayarlarına aşinadır.

Microsoft Excel’deki Hizalama Ayarları

todo:image_alt_text

Yukarıdaki şekilden görebileceğiniz gibi, farklı türde hizalama seçenekleri bulunmaktadır:

Bu tüm hizalama ayarları, Aspose.Cells tarafından tamamen desteklenir ve aşağıda daha detaylı olarak tartışılmaktadır.

Hizalama Ayarlarının Yapılandırılması

Aspose.Cells, bir Excel dosyasını temsil eden Workbook adında bir sınıf sağlar. Workbook sınıfı, Excel dosyasındaki her bir çalışma sayfasına erişime izin veren bir WorksheetCollection’a sahiptir. Bir çalışma sayfası, Worksheet sınıfı tarafından temsil edilir.

Worksheet sınıfı bir Cells koleksiyonu sağlar. Cells koleksiyonundaki her öğe, Cell sınıfının bir nesnesini temsil eder.

Aspose.Cells, Cell sınıfındaki setStyle metodu sağlar. Bu metot, bir hücrenin biçimlendirilmesi için kullanılır. Style sınıfı, yazı tiplerini yapılandırmak için kullanışlı özellikler sağlar.

MetinAlignmentType numaralandırmasını kullanarak herhangi bir metin hizalama türünü seçin. MetinAlignmentType numaralandırmasındaki önceden tanımlanmış metin hizalama tipleri şunlardır:

Metin Hizalama Türleri Açıklama
Bottom , alt metin hizalamasını temsil eder
Center , merkez metin hizalamasını temsil eder
CenterAcross , metin hizalamasını çapraz merkezlemeyi temsil eder
Distributed , dağıtılmış metin hizalamasını temsil eder
Fill , doldurma metin hizalamasını temsil eder
General , genel metin hizalamasını temsil eder
Justify , düzgün metin hizalamasını temsil eder
Left , sol metin hizalamasını temsil eder
Right , sağ metin hizalamasını temsil eder
Top , üst metin hizalamasını temsil eder

Yatay Hizalama

Metni yatay olarak hizalamak için Style nesnesinin setHorizontalAlignment yöntemini kullanın.

Aşağıdaki çıktı, aşağıdaki örnek kodu çalıştırarak elde edilir:

Metni yatay olarak hizalamak

todo:image_alt_text

// 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(TextAlignmentHorizontal.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "TAHorizontal_out.xls");

Dikey Hizalama

Metni dikey olarak hizalamak için Style nesnesinin setVerticalAlignment yöntemini kullanın.

Dikey Hizalama merkeze ayarlandığında aşağıdaki çıktı elde edilir.

Metni dikey olarak hizalamak

todo:image_alt_text

// 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(TextAlignmentVertical.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the vertical alignment of the text in a cell
Style style1 = cell.getStyle();
style1.setVerticalAlignment(TextAlignmentType.CENTER);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "TAVertical_out.xls");

Girinti

Hücredeki metnin girinti düzeyini ayarlamak için Style nesnesinin setIndentLevel yöntemini kullanabilirsiniz.

Girinti Düzeyi 2 olarak ayarlandığında aşağıdaki çıktı elde edilir.

Girinti düzeyi 2’ye ayarlandı

todo:image_alt_text

// 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(Indentation.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the vertical alignment of the text in a cell
Style style1 = cell.getStyle();
style1.setIndentLevel(2);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "Indentation_out.xls");

Yönlendirme

Hücredeki metnin yönlendirmesini (dönüş) Style nesnesinin setRotationAngle yöntemiyle ayarlayın.

Yönlendirme açısı 25’e ayarlandığında aşağıdaki çıktı elde edilir.

Yönlendirme açısı 25’e ayarlandı

todo:image_alt_text

// 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(Orientation.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the rotation of the text (inside the cell) to 25
Style style1 = cell.getStyle();
style1.setRotationAngle(25);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "Orientation_out.xls");

Metin Kontrolü

Aşağıdaki bölüm metin kaydırma, sığdırmayı daraltma ve diğer biçimlendirme seçeneklerini ayarlayarak metni nasıl kontrol edeceğinizi tartışmaktadır.

Metni Kaydırma

Bir hücrede metni sarmak, metni okumayı kolaylaştırır: hücrenin yüksekliği, metnin tümünü sığdırmak için ayarlanır, onu kesmez veya bitişik hücrelere taşmaz.

Style nesnesinin setTextWrapped yöntemiyle metni sarmayı açın veya kapatın.

Metni sarma etkinleştirildiğinde aşağıdaki çıktı elde edilir.

Hücre içindeki metin

todo:image_alt_text

// 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(WrapText.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Enabling the text to be wrapped within the cell
Style style1 = cell.getStyle();
style1.setTextWrapped(true);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "WrapText_out.xls");

Sığdırmayı Daraltma

Bir alanı sarmak için bir seçenek, hücre boyutlarına sığdırmak için metin boyutunu küçültmektir. Bunun için Stil nesnesinin IsTextWrapped özelliği true olarak ayarlanır.

Metin hücreye sığdırılmak için aşağıdaki çıktıya ulaşılır.

Hücre sınırları içine sığdırılmış metin

todo:image_alt_text

// 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(ShrinkingToFit.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Shrinking the text to fit according to the dimensions of the cell
Style style1 = cell.getStyle();
style1.setShrinkToFit(true);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "ShrinkingToFit_out.xls");

Hücreleri Birleştirme

Microsoft Excel gibi, Aspose.Cells birçok hücreyi bir araya getirerek tek bir hücre oluşturmayı destekler.

İlk satırdaki üç hücrenin birleştirilmesiyle büyük tek bir hücre oluşturulursa aşağıdaki çıktı elde edilir.

Büyük bir hücre oluşturmak için üç hücre birleştirildi

todo:image_alt_text

Hücreleri birleştirmek için Cells koleksiyonunun Merge yöntemini kullanın. Birleştirme yöntemi aşağıdaki parametreleri alır:

  • İlk satır, birleştirmeye başlamak için ilk satır.
  • İlk sütun, birleştirmeye başlamak için ilk sütun.
  • Satır sayısı, birleştirilecek satır sayısı.
  • Sütun sayısı, birleştirilecek sütun sayısı.
// 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(MergingCellsInWorksheet.class) + "data/";
// Create a Workbook.
Workbook wbk = new Workbook();
// Create a Worksheet and get the first sheet.
Worksheet worksheet = wbk.getWorksheets().get(0);
// Create a Cells object to fetch all the cells.
Cells cells = worksheet.getCells();
// Merge some Cells (C6:E7) into a single C6 Cell.
cells.merge(5, 2, 2, 3);
// Input data into C6 Cell.
worksheet.getCells().get(5, 2).setValue("This is my value");
// Create a Style object to fetch the Style of C6 Cell.
Style style = worksheet.getCells().get(5, 2).getStyle();
// Create a Font object
Font font = style.getFont();
// Set the name.
font.setName("Times New Roman");
// Set the font size.
font.setSize(18);
// Set the font color
font.setColor(Color.getBlue());
// Bold the text
font.setBold(true);
// Make it italic
font.setItalic(true);
// Set the backgrond color of C6 Cell to Red
style.setForegroundColor(Color.getRed());
style.setPattern(BackgroundType.SOLID);
// Apply the Style to C6 Cell.
cells.get(5, 2).setStyle(style);
// Save the Workbook.
wbk.save(dataDir + "mergingcells_out.xls");
wbk.save(dataDir + "mergingcells_out.xlsx");
wbk.save(dataDir + "mergingcells_out.ods");
// Print message
System.out.println("Process completed successfully");

Metin Yönü

Hücrelerde metnin okuma sırasını ayarlamak mümkündür. Okuma sırası, karakterlerin, kelimelerin vb. gösterildiği görsel sıradır. Örneğin, İngilizce soldan sağa bir dil iken Arapça sağdan sola bir dildir.

Okuma sırası Style nesnesinin TextDirection özelliği ile ayarlanır. Aspose.Cells, TextDirectionType numaralandırmasında önceden tanımlanmış metin yönü tiplerini sağlar.

Metin Yönü Türleri Açıklama
Context Girilen ilk karakterin diline uygun okuma sırası
LeftToRight Soldan sağa okuma sırası
RightToLeft Sağdan sola okuma sırası
// 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(ChangeTextDirection.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the text direction from right to left
Style style1 = cell.getStyle();
style1.setTextDirection(TextDirectionType.RIGHT_TO_LEFT);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "ChangeTextDirection_out.xls");

Metnin okuma sırası sağdan sola olarak ayarlandığında aşağıdaki çıktı elde edilir.

Metin okuma sırası sağdan sola ayarlanıyor

todo:image_alt_text

Hücredeki Seçili Karakterleri Biçimlendirme

Yazı Tipi Ayarlarıyla İlgilenme sadece hücreleri nasıl biçimlendireceğini açıklar, ancak yalnızca seçili karakterleri biçimlendirmek istersen ne yapacaksın?

Aspose.Cells bu özelliği destekler. Bu konu bu özelliği nasıl kullanacağınızı açıklar.

Seçili Karakterleri Biçimlendirme

Aspose.Cells, bir Microsoft Excel dosyasını temsil eden Workbook adlı bir sınıf sağlar. Workbook sınıfı, Excel dosyasındaki her çalışma sayfasına erişim sağlayan Worksheets koleksiyonunu içerir. Bir çalışma sayfası Worksheet sınıfı tarafından temsil edilir. Worksheet sınıfı bir Cells koleksiyonu sağlar. Hücreler koleksiyonundaki her bir öğe, Cell sınıfının bir nesnesini temsil eder.

Cell sınıfı, bir hücredeki karakterleri seçmek için aşağıdaki parametreleri alan characters yöntemi sağlar:

  • Başlangıç Dizini, seçimin başlayacağı karakterin dizini.
  • Karakter Sayısı, seçilecek karakterlerin sayısı.

Çıktı dosyasında, A1 hücresindeki ‘Visit’ kelimesi varsayılan yazı tipi ile biçimlendirilir ancak ‘Aspose!’ kalın ve mavi renklidir.

Seçili karakterleri biçimlendirme

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Path to source file
String dataDir = Utils.getSharedDataDir(FormattingSelectedCharacters.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Visit Aspose!");
Font font = cell.characters(6, 7).getFont();
// Setting the font of selected characters to bold
font.setBold(true);
// Setting the font color of selected characters to blue
font.setColor(Color.getBlue());
// Saving the Excel file
workbook.save(dataDir + "FSCharacters_out.xls");

Çalışma Kitabını Etkinleştirme ve Çalışma Sayfasındaki Etkin Bir Hücreyi veya Hücre Aralığını Seçme

Bazen, bir dosyayı Microsoft Excel’de birisi açtığında ilk olarak görünen sayfanın belirli bir çalışma sayfası olmasını isteyebilirsiniz. Ayrıca, belirli bir hücreyi etkinleştirmeniz ve bu hücreye girilmeye başlandığında kaydırma çubuklarının etkin hücreye kaydırılmasını isteyebilirsiniz. Aspose.Cells, yukarıda bahsedilen tüm görevleri yapabilir.

Etkin bir sayfa, bir çalışma kitabı içinde çalıştığınız sayfadır. Etkin sayfanın sekmesindeki adı varsayılan olarak kalındır. Etkin hücre ise seçili olan ve veri girilmeye başlandığında içine veri girilen hücredir. Aynı anda yalnızca bir hücre etkindir. Etkin hücre, diğer hücrelere karşı açıkça görünmesi için kalın bir kenarlıkla çevrilmiştir. Aspose.Cells ayrıca çalışma sayfasındaki bir hücre aralığını seçmenize de olanak tanır.

Bir Sayfayı Etkinleştirme ve Bir Hücreyi Etkin Hale Getirme

Aspose.Cells, bu görevler için belirli bir API sağlar. Örneğin, WorksheetCollection.setActiveSheetIndex yöntemi, etkin bir sayfa ayarlamak için kullanışlıdır. Benzer şekilde, Worksheet.setActiveCell yöntemi bir çalışma sayfasındaki etkin hücreyi ayarlamak ve almak için kullanılır.

Dosya Microsoft Excel’de açıldığında yatay ve dikey kaydırma çubuklarının seçilen veriyi güzel bir görüntü sağlamak için satır ve sütun dizin konumuna kaydırılmasını istiyorsanız, Worksheet.setFirstVisibleRow ve Worksheet.setFirstVisibleColumn özelliklerini kullanın.

Aşağıdaki örnek, bir çalışma sayfasını etkinleştirme ve içindeki bir hücreyi etkin hale getirme şeklini gösterir. Kaydırma çubukları, 2. satırı ve 2. sütunu ilk görünür satır ve sütun olarak kaydırılmıştır.

B2 hücresini etkin hücre olarak ayarlama

todo:image_alt_text

// 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(MakeCellActive.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Get the cells in the worksheet.
Cells cells = worksheet1.getCells();
// Input data into B2 cell.
cells.get(1, 1).setValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.getWorksheets().setActiveSheetIndex(0);
// Set B2 cell as an active cell in the worksheet.
worksheet1.setActiveCell("B2");
// Set the B column as the first visible column in the worksheet.
worksheet1.setFirstVisibleColumn(1);
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.setFirstVisibleRow(1);
// Save the Excel file.
workbook.save(dataDir + "MakeCellActive_out.xls");

Çalışma Sayfasında Bir Hücre Aralığı Seçme

Aspose.Cells, int startRow, int startColumn, int totalRows, int totalColumns, bool removeOthers parametrelerini kullanan Worksheet.selectRange(int startRow, int startColumn, int totalRows, int totalColumns, bool removeOthers) yöntemini sağlar. removeOthers parametresini true olarak kullanarak, çalışma sayfasındaki diğer hücre ya da hücre aralığı seçimleri kaldırılır.

Aşağıdaki örnek, etkin çalışma sayfasında bir hücre aralığı seçme şeklini 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(SelectRangeofCellsinWorksheet.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Get the cells in the worksheet.
Cells cells = worksheet1.getCells();
// Input data into B2 cell.
cells.get(1, 1).setValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.getWorksheets().setActiveSheetIndex(0);
// Select range of cells(A1:E10) in the worksheet.
worksheet1.selectRange(0, 0, 10, 5, true);
// Save the Excel file.
workbook.save(dataDir + "SROfCInWorksheet_out.xlsx");

Satır ve Sütunların Biçimlendirilmesi

Raporun görünümünü sağlamak için elektronik tablo içindeki satır ve sütunları biçimlendirmek, Excel uygulamasının muhtemelen en yaygın kullanılan özelliklerindendir. Aspose.Cells API’leri de veri modeli aracılığıyla bu işlevselliği sunar ve özellikle yazı tipi ve özellikleri, metnin hizalaması, arka plan/önrenkleri, kenarlıkları, sayıların ve tarihsel dizelerin görüntü biçimlendirmesini ele alan Style sınıfını açığa çıkarır. Aspose.Cells API’lerinin sağladığı diğer faydalı bir sınıf ise StyleFlag’dir ve Style nesnesinin tekrar kullanılmasını sağlar. 

Bu makalede, satır ve sütunlara biçimlendirme uygulamak için Aspose.Cells for Java API’sını nasıl kullanacağımızı açıklamaya çalışacağız. 

Satırları ve Sütunları Biçimlendirme

Aspose.Cells, bir Microsoft Excel dosyasını temsil eden Workbook sınıfını sağlar. Workbook sınıfı, Excel dosyasındaki her çalışma sayfasına erişim sağlayan bir WorksheetCollection içerir. Bir çalışma sayfası, Worksheet sınıfı tarafından temsil edilir. Worksheet sınıfı, Cells koleksiyonunu sağlar. Cells koleksiyonu, Row koleksiyonunu sağlar.

Bir Satırı Biçimlendirme

Row koleksiyonundaki her öğe, bir Satır nesnesini temsil eder. Satır nesnesi, bir satıra biçimlendirme uygulamak için kullanılan applyStyle yöntemini sunar.

Aynı biçimlendirmeyi bir satıra uygulamak için Style objesini kullanın:

  1. createStyle yöntemini çağırarak Style objesini Workbook sınıfına ekleyin.
  2. Biçimlendirme ayarlarını uygulamak için Style objesi özelliklerini ayarlayın.
  3. Yapılandırılmış Style objesini Satır nesnesinin applyStyle yöntemine atayın.
// 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(FormattingARow.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Adding a new Style to the styles collection of the Excel object Accessing the newly added Style to the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border of the cell
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBottomBorder(true);
styleFlag.setFontColor(true);
// Accessing a row from the Rows collection
Row row = cells.getRows().get(0);
// Assigning the Style object to the Style property of the row
row.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(dataDir + "FormattingARow_out.xls");

Bir Sütunu Biçimlendirme

Cells koleksiyonu, Columns koleksiyonunu sağlar. Columns koleksiyonundaki her öğe, bir Sütun nesnesini temsil eder. Satır nesnesi gibi, Sütun nesnesi de sütun biçimlendirmesi yapmak için applyStyle yöntemini sunar. Sütun nesnesinin applyStyle yöntemini kullanarak bir sütunu satır biçimleriyle aynı şekilde biçimlendirebilirsiniz.

// 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(FormattingAColumn.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Adding a new Style to the styles collection of the Excel object Accessing the newly added Style to the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border of the cell
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBottomBorder(true);
styleFlag.setFontColor(true);
// Accessing a column from the Columns collection
Column column = cells.getColumns().get(0);
// Applying the style to the column
column.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(dataDir + "FormattingAColumn_out.xls");

Satırlar ve Sütunlar için Sayı ve Tarihlerin Görüntü Biçimini Ayarlama

Eğer tam bir satır veya sütunun ekran biçimini sayı ve tarihler için ayarlamanız gerekiyorsa, süreç yukarıda tartışıldığı gibi daha veya daha az aynıdır, ancak metin içeriği için parametreleri ayarlamak yerine, Style.Number veya Style.Custom’ı kullanarak sayılar ve tarihler için biçimlendirme ayarlayacaksınız. Lütfen dikkat edin, Style.Number özelliği tamsayı türündedir ve yerleşik sayı ve tarih biçimlerine işaret ederken, Style.Custom özelliği dize türündedir ve geçerli desenleri kabul eder.

// 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(SettingDisplayFormat.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(0);
// Adding a new Style to the styles collection of the Workbook object
Style style = workbook.createStyle();
// Setting the Number property to 4 which corresponds to the pattern #,##0.00
style.setNumber(4);
// Creating an object of StyleFlag
StyleFlag flag = new StyleFlag();
// Setting NumberFormat property to true so that only this aspect takes effect from Style object
flag.setNumberFormat(true);
// Applying style to the first row of the worksheet
worksheet.getCells().getRows().get(0).applyStyle(style, flag);
// Re-initializing the Style object
style = workbook.createStyle();
// Setting the Custom property to the pattern d-mmm-yy
style.setCustom("d-mmm-yy");
// Applying style to the first column of the worksheet
worksheet.getCells().getColumns().get(0).applyStyle(style, flag);
// Saving spreadsheet on disc
workbook.save(dataDir + "SDisplayFormat_out.xlsx");