Veri Biçimlendirme
Cells’de Verileri Biçimlendirme Yaklaşımları
Çalışma sayfası hücreleri düzgün biçimlendirilirse, kullanıcıların hücrenin içeriğini (verilerini) okumasının 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, Tasarımcı Elektronik Tablosu oluştururken WYSIWYG ortamında Microsoft Excel kullanarak hücreleri biçimlendirmektir. Tasarımcı e-tablosu oluşturulduktan sonra, e-tablo ile kaydedilen tüm biçim ayarlarını koruyarak Aspose.Cells’i kullanarak elektronik tabloyu açabilirsiniz. Hücreleri ve içeriklerini biçimlendirmenin başka bir yolu da Aspose.Cells API’i kullanmaktır. Bu konuda, Aspose.Cells API kullanarak hücreleri ve içeriklerini biçimlendirmek için iki yaklaşım açıklayacağız.
biçimlendirme Cells
Geliştiriciler, Aspose.Cells’in esnek API’ini kullanarak hücreleri ve içeriklerini biçimlendirebilir. Aspose.Cells, bir sınıf sağlar,Çalışma kitabı , bu bir Microsoft Excel dosyasını temsil eder. buÇalışma kitabı sınıf bir içerirÇalışma Sayfası Koleksiyonu Bu, bir Excel dosyasındaki her çalışma sayfasına erişim sağlar. Bir çalışma sayfası şununla temsil edilir:Çalışma kağıdı sınıf. buÇalışma kağıdı class bir Cells koleksiyonu sağlar. İçindeki her öğeCellskoleksiyon bir nesneyi temsil ederCell sınıf.
Aspose.Cells şunları sağlar:stil mülkiyetCell sınıf, bir hücrenin biçimlendirme stilini ayarlamak için kullanılır. Ayrıca, Aspose.Cells ayrıca birstil Aynı amaca hizmet etmek için kullanılan sınıf. Arka plan veya ön plan renklerini, kenarlıkları, yazı tiplerini, yatay ve dikey hizalamaları, girinti düzeyini, metin yönünü, döndürme açısını ve çok daha fazlasını ayarlamak için hücrelere farklı biçimlendirme stilleri uygulayın.
setStyle Yöntemini Kullanma
Farklı hücrelere farklı biçimlendirme stilleri uygularken setStyle yöntemini kullanmak daha iyidir.Cell sınıf. Bir hücrede çeşitli biçimlendirme ayarlarını uygulamak için setStyle yönteminin kullanımını gösteren bir örnek aşağıda 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 Nesnesini Kullanma
Aynı biçimlendirme stilini farklı hücrelere uygularken,stil nesne.
- Eklestil Styles koleksiyonuna nesneÇalışma kitabı Workbook sınıfının createStyle yöntemini çağırarak sınıfı.
- Yeni eklenen Style nesnesine Styles koleksiyonundan erişin.
- İstenen biçimlendirme ayarlarını uygulamak için Style nesnesinin istenen özelliklerini ayarlayın.
- Yapılandırılan Style nesnesini istenen herhangi bir hücrenin Style özelliğine atayın.
Bu yaklaşım, uygulamalarınızın verimliliğini büyük ölçüde artırabilir ve bellek tasarrufu da 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"); |
Degrade Dolgu Efektlerini Uygulama
İstediğiniz Degrade Dolgu Efektlerini hücreye uygulamak için, buna göre Style nesnesinin setTwoColorGradient yöntemini kullanın.
Kod Örneği
Aşağıdaki çıktı aşağıdaki kod çalıştırılarak elde edilir.
Degrade Dolgu Efektlerini Uygulama
// 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ı Yapılandırma
Hücreleri biçimlendirmek için Microsoft Excel’i kullanan herkes, Microsoft Excel’deki hizalama ayarlarına aşina olacaktır.
Microsoft Excel’deki hizalama ayarları
Yukarıdaki şekilde görebileceğiniz gibi, farklı hizalama seçenekleri vardır:
- Metin hizalama (yatay dikey)
- Girinti.
- Oryantasyon.
- Metin kontrolü.
- Metin yönü.
Bu hizalama ayarlarının tümü Aspose.Cells tarafından tam olarak desteklenmektedir ve aşağıda daha ayrıntılı olarak ele alınmıştır.
Hizalama Ayarlarını Yapılandırma
Aspose.Cells bir sınıf sağlar,Çalışma kitabı , bu bir Excel dosyasını temsil eder. Workbook sınıfı, Excel dosyasındaki her çalışma sayfasına erişim sağlayan bir WorksheetCollection içerir. Bir çalışma sayfası şununla temsil edilir:Çalışma kağıdı sınıf.
Worksheet sınıfı bir Cells koleksiyonu sağlar. Cells koleksiyonundaki her öğe, bir nesneyi temsil eder.Cell sınıf.
Aspose.Cells, setStyle yöntemini sağlar.Cell bir hücrenin biçimlendirmesi için kullanılan sınıf. bustil class, yazı tipi ayarlarını yapılandırmak için yararlı özellikler sağlar.
TextAlignmentType numaralandırmasını kullanarak herhangi bir metin hizalama türünü seçin. TextAlignmentType numaralandırmasındaki önceden tanımlanmış metin hizalama türleri şunlardır:
Metin Hizalama Türleri | Açıklama |
---|---|
Alt kısım | Alt metin hizalamasını temsil eder |
merkez | Merkez metin hizalamasını temsil eder |
Merkez Boyunca | Metin hizalaması boyunca merkezi temsil eder |
dağıtılmış | Dağıtılmış metin hizalamasını temsil eder |
Doldurmak | Dolgu metni hizalamasını temsil eder |
Genel | Genel metin hizalamasını temsil eder |
Savunmak | Yaslanmış metin hizalamasını temsil eder |
Sol | Sola metin hizalamasını temsil eder |
Doğru | Doğru metin hizalamasını temsil eder |
Tepe | Üst metin hizalamasını temsil eder |
Justify dağıtılmış ayarını Style.setJustifyDistributed() yöntemini kullanarak da uygulayabilirsiniz.
|
Yatay hizalama
Kullanstil Metni yatay olarak hizalamak için nesnenin setHorizontalAlignment yöntemi.
Aşağıdaki çıktı, aşağıdaki örnek kodu çalıştırarak elde edilir:
Metni yatay olarak hizalama
// 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
Kullanstil Metni dikey olarak hizalamak için nesnenin setVerticalAlignment yöntemi.
VerticalAlignment merkeze ayarlandığında aşağıdaki çıktı elde edilir.
Metni dikey olarak hizalama
// 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
Kullanarak bir hücredeki metnin girinti düzeyini ayarlamak mümkündür.stil nesnenin setIndentLevel yöntemi.
IndentLevel 2 olarak ayarlandığında aşağıdaki çıktı elde edilir.
Girinti seviyesi 2’ye ayarlandı
// 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"); |
Oryantasyon
ile bir hücredeki metnin yönünü (döndürme) ayarlayın.stil nesnenin setRotationAngle yöntemi.
Dönme açısı 25 olarak ayarlandığında aşağıdaki çıkış elde edilir.
Dönüş açısı 25 olarak ayarlandı
// 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ümde, metin kaydırma, sığdırmak için küçültme ve diğer biçimlendirme seçeneklerini ayarlayarak metnin nasıl kontrol edileceği anlatılmaktadır.
Metni Kaydırma
Metni bir hücreye kaydırmak okumayı kolaylaştırır: Hücrenin yüksekliği, onu kesmek veya bitişik hücrelere dökmek yerine tüm metne sığacak şekilde ayarlanır.
ile metin kaydırmayı açık veya kapalı olarak ayarlayın.stil nesnenin setTextWrapped yöntemi.
Metin kaydırma etkinleştirildiğinde aşağıdaki çıktı elde edilir.
Hücrenin içine sarılmış metin
// 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ırmak İçin Küçültmek
Metni bir alana kaydırma seçeneği, metin boyutunu bir hücrenin boyutlarına sığacak şekilde küçültmektir. Bu ayarlanarak yapılır.stil nesnenin IsTextWrapped özelliğidoğru.
Metin hücreye sığacak şekilde küçültüldüğünde aşağıdaki çıktı elde edilir.
Metin, hücrenin sınırları içine sığacak şekilde küçültüldü
// 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"); |
Birleştirme Cells
Microsoft Excel gibi, Aspose.Cells de birkaç hücrenin tek hücrede birleştirilmesini destekler.
İlk satırdaki üç hücre birleştirilerek büyük bir tek hücre oluşturulursa aşağıdaki çıktı elde edilir.
Büyük bir hücre oluşturmak için üç hücre birleştirildi
KullanCells koleksiyonun Hücreleri birleştirmek için Merge yöntemi. Merge yöntemi aşağıdaki parametreleri alır:
- İlk sıra, birleştirmeye nereden başlayacağınız ilk sıra.
- İlk sütun, birleştirmeye nereden başlayacağınız 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ücrelerdeki metnin okuma sırasını ayarlamak mümkündür. Okuma sırası, karakterlerin, kelimelerin vb. görüntülendiği görsel sıradır. Örneğin, İngilizce soldan sağa bir dilken, Arapça sağdan sola bir dildir.
Okuma sırası ile ayarlanırstil nesnenin TextDirection özelliği. Aspose.Cells, TextDirectionType numaralandırmasında önceden tanımlanmış metin yönü türleri sağlar.
Metin Yönü Türleri | Açıklama |
---|---|
Bağlam | İlk girilen karakterin diliyle tutarlı okuma sırası |
Soldan sağa | Soldan sağa okuma sırası |
Sağdan sola | 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 doğru ayarlanırsa aşağıdaki çıktı elde edilir.
Metin okuma sırasını sağdan sola ayarlama
Cell’de Seçili Karakterleri Biçimlendirme
Yazı Tipi Ayarlarıyla Başa Çıkmahücrelerin nasıl biçimlendirileceğini, ancak yalnızca tüm hücrelerin içeriğinin nasıl biçimlendirileceğini açıkladı. Yalnızca seçili karakterleri biçimlendirmek isterseniz ne olur?
Aspose.Cells bu özelliği destekler. Bu konuda, bu özelliğin nasıl kullanılacağı açıklanmaktadır.
Seçilen Karakterleri Biçimlendirme
Aspose.Cells bir sınıf sağlar,Çalışma kitabı , bu bir Microsoft Excel dosyasını temsil eder. Workbook sınıfı, Excel dosyasındaki her çalışma sayfasına erişim sağlayan bir Worksheets koleksiyonu içerir. Bir çalışma sayfası şununla temsil edilir:Çalışma kağıdı sınıf. Worksheet sınıfı bir Cells koleksiyonu sağlar. Cells koleksiyonundaki her öğe, bir nesneyi temsil eder.Cell sınıf.
Cell sınıfı, bir hücrede bir dizi karakter seçmek için aşağıdaki parametreleri alan karakter yöntemini sağlar:
- Dizini başlat, seçimin başlatılacağı karakterin dizini.
- Karakter sayısı, seçilecek karakter sayısı.
Çıktı dosyasında, A1" hücresinde, ‘Ziyaret’ sözcüğü varsayılan yazı tipiyle biçimlendirilmiştir ancak ‘Aspose!’ kalın ve mavidir.
Seçilen karakterleri biçimlendirme
// 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"); |
Sayfaları Etkinleştirme ve Cell Etkinleştirme veya Çalışma Sayfasında Cells Aralık Seçme
Bazen, birisi dosyayı Microsoft Excel’de açtığında ilk görüntülenen sayfa olması için belirli bir çalışma sayfasını etkinleştirmeniz gerekebilir. Belirli bir hücreyi, kaydırma çubukları etkin hücreye kaydırılarak açıkça görülebilecek şekilde etkinleştirmeniz de gerekebilir. Aspose.Cells yukarıda sayılan tüm görevleri yapabilecek kapasitededir.
Etkin sayfa, çalışma kitabında üzerinde çalıştığınız sayfadır. Etkin sayfanın sekmesindeki ad varsayılan olarak kalındır. Bu arada etkin hücre, siz yazmaya başladığınızda seçilen ve içine verilerin girildiği hücredir. Aynı anda yalnızca bir hücre etkindir. Aktif hücre, diğer hücrelere karşı görünmesini sağlamak için kalın bir sınırla çevrilidir. Aspose.Cells, çalışma sayfasında bir hücre aralığı seçmenize de olanak tanır.
Bir Sayfayı Etkinleştirme ve Cell’i Etkinleştirme
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ında etkin bir hücreyi ayarlamak ve almak için kullanılır.
Dosya Microsoft Excel’de açıldığında seçilen verilerin iyi bir görünümünü vermek için yatay ve dikey kaydırma çubuklarının satır ve sütun dizini konumuna kaydırılmasını istiyorsanız, Worksheet.setFirstVisibleRow ve Worksheet.setFirstVisibleColumn özelliklerini kullanın.
Aşağıdaki örnek, bir çalışma sayfasının nasıl etkinleştirileceğini ve içindeki bir hücrenin nasıl etkinleştirileceğini gösterir. Kaydırma çubukları, 2. satırı ve 2. sütunu ilk görünür satır ve sütunları yapmak için kaydırılır.
B2 hücresini aktif hücre olarak ayarlama
// 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 Cells Aralığı Seçme
Aspose.Cells, Worksheet.selectRange(int startRow, int startColumn, int totalRows, int totalColumns, bool removeOthers) yöntemini sağlar. Son parametre - removeOthers - true olarak kullanıldığında, sayfadaki diğer hücre veya hücre aralığı seçimleri kaldırılır.
Aşağıdaki örnek, etkin çalışma sayfasında bir hücre aralığının nasıl seçileceğini 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ırları ve Sütunları Biçimlendirme
Rapora bir görünüm vermek için bir elektronik tablodaki satırları ve sütunları biçimlendirmek, muhtemelen Excel uygulamasının en yaygın kullanılan özelliğidir. Aspose.Cells API’ler ayrıca, yazı tipi ve öznitelikleri, metnin hizalanması, arka plan/ön plan renkleri, kenarlıklar, sayılar ve tarih değişmezleri için görüntüleme formatı vb. . Aspose.Cells API’lerinin sağladığı diğer bir yararlı sınıf, Style nesnesinin yeniden kullanılabilirliğini sağlayan StyleFlag’dır.
Bu yazımızda satır ve sütunlara biçimlendirme uygulamak için Aspose.Cells for Java API nasıl kullanılacağını açıklamaya çalışacağız.
Satırları ve Sütunları Biçimlendirme
Aspose.Cells bir sınıf sağlar,Çalışma kitabı bu bir Microsoft Excel dosyasını temsil eder. buÇalışma kitabı 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. buÇalışma kağıdı class Cells koleksiyonunu sağlar. Cells koleksiyonu bir Rows koleksiyonu sağlar.
Satır Biçimlendirme
Rows koleksiyonundaki her öğe bir Row nesnesini temsil eder. Row 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 nesnesini kullanın:
- CreateStyle yöntemini çağırarak Workbook sınıfına bir Style nesnesi ekleyin.
- Biçimlendirme ayarlarını uygulamak için Stil nesnesi özelliklerini ayarlayın.
- Yapılandırılan Style nesnesini bir Row 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, bir Sütun koleksiyonu sağlar. Columns koleksiyonundaki her öğe bir Column nesnesini temsil eder. Row nesnesine benzer şekilde Column nesnesi, sütun biçimlendirmesini ayarlamak için kullanılan ApplyStyle yöntemini sunar. Bir sütunu satırla aynı şekilde biçimlendirmek için Column nesnesinin ApplyStyle yöntemini kullanı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(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 Numbers ve Tarihlerin Görüntüleme Biçimini Ayarlama
Gereksinim, tam bir satır veya sütun için sayıların ve tarihlerin görüntülenme biçimini ayarlamaksa, süreç aşağı yukarı yukarıda tartışılanla aynıdır, ancak metin içerikleri için parametreler ayarlamak yerine, sayılar için biçimlendirmeyi ayarlıyor olacaksınız. ve Style.Number veya Style.Custom kullanarak tarihler. Lütfen unutmayın, Style.Number özelliği tamsayı türündedir ve yerleşik sayı ve tarih biçimlerini ifade eder, oysa Style.Custom özelliği dize türündedir ve geçerli kalıpları 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"); |