Hücreleri biçimlendir

Giriş

GetStyle ve SetStyle Yöntemlerini kullanarak Cells’i biçimlendirin

Arka plan veya ön plan renkleri, kenarlıklar, yazı tipleri, yatay ve dikey hizalamalar, girinti düzeyi, metin yönü, döndürme açısı ve çok daha fazlasını ayarlamak için hücrelere farklı biçimlendirme stilleri uygulayın.

GetStyle ve SetStyle Yöntemlerini Kullanma

Geliştiricilerin farklı hücrelere farklı biçimlendirme stilleri uygulamaları gerekiyorsa,stil kullanarak hücreninCell.GetStyle yöntemi, stil özniteliklerini belirtin ve ardından kullanarak biçimlendirmeyi uygulayın.Cell.SetStyleyöntem. Bir hücreye çeşitli biçimlendirmeler uygulamak için bu yaklaşımı göstermek üzere aşağıda bir örnek verilmiştir.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Defining a Style object
Aspose.Cells.Style style;
// Get the style from A1 cell
style = cell.GetStyle();
// Setting the vertical alignment
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text
style.Font.Color = Color.Green;
// Setting to shrink according to the text contained in it
style.ShrinkToFit = true;
// Setting the bottom border color to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Applying the style to A1 cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Farklı Biçimlendirmek İçin Stil Nesnesini Kullanma Cells

Geliştiricilerin aynı biçimlendirme stilini farklı hücrelere uygulamaları gerekiyorsa, kullanabilirlerstil nesne. kullanmak için lütfen aşağıdaki adımları izleyin.stilnesne:

  1. Eklestil çağırarak nesneStil Oluştur yöntemiÇalışma kitabısınıf
  2. Yeni eklenenlere erişinstilnesne
  3. İstenen özellikleri/öznitelikleri ayarlayın.stilistenen biçimlendirme ayarlarını uygulamak için nesne
  4. Yapılandırılanı atastilistediğiniz hücrelere itiraz edin

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-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the first worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Adding a new Style
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Assigning the Style object to the "A1" cell
cell.SetStyle(style);
// Apply the same style to some other cells
worksheet.Cells["B1"].SetStyle(style);
worksheet.Cells["C1"].SetStyle(style);
worksheet.Cells["D1"].SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Microsoft Excel 2007 Önceden Tanımlanmış Stilleri Kullanma

Microsoft Excel 2007 için farklı biçimlendirme stilleri uygulamanız gerekirse, Aspose.Cells API’i kullanarak stilleri uygulayın. Hücreye önceden tanımlanmış bir stil uygulamak için bu yaklaşımı 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-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Create a style object .
Style style = workbook.CreateStyle();
// Input a value to A1 cell.
workbook.Worksheets[0].Cells["A1"].PutValue("Test");
// Apply the style to the cell.
workbook.Worksheets[0].Cells["A1"].SetStyle(style);
// Save the Excel 2007 file.
workbook.Save(dataDir + "book1.out.xlsx");

Cell’de Seçili Karakterleri Biçimlendirme

Yazı Tipi Ayarları ile ilgilenmek, hücrelerdeki metnin nasıl biçimlendirileceğini açıklar, ancak yalnızca tüm hücre içeriğinin nasıl biçimlendirileceğini açıklar. Yalnızca seçili karakterleri biçimlendirmek isterseniz ne olur?

Aspose.Cells bu özelliği de desteklemektedir. Bu konuda, bu özelliği nasıl etkili bir şekilde kullanacağımız 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. buÇalışma kitabı sınıf içerirçalışma sayfaları bir Excel dosyasındaki her çalışma sayfasına erişim sağlayan koleksiyon. Bir çalışma sayfası şununla temsil edilir:Çalışma kağıdı sınıf. buÇalışma kağıdı sınıf bir sağlarCells Toplamak. İçindeki her öğeCells koleksiyon bir nesneyi temsil ederCellsınıf.

buCell sınıf sağlarKarakterlerbir hücre içindeki bir karakter aralığını seçmek için aşağıdaki parametreleri alan yöntem:

  • Dizini başlat, seçimin başladığı karakterin dizini.
  • Karakter sayısı, seçilecek karakter sayısı.

buKarakterler yöntemi örneğini döndürürYazı Tipi Ayarıgeliştiricilerin karakterleri, aşağıda kod örneğinde gösterildiği gibi bir hücrede olduğu gibi biçimlendirmelerine izin veren sınıf. Çıktı dosyasında, A1 hücresinde, ‘Ziyaret’ sözcüğü varsayılan yazı tipiyle biçimlendirilecek ancak ‘Aspose!’ kalın ve mavidir.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first(default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Visit Aspose!");
// Setting the font of selected characters to bold
cell.Characters(6, 7).Font.IsBold = true;
// Setting the font color of selected characters to blue
cell.Characters(6, 7).Font.Color = Color.Blue;
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

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

Bazen, geliştiricilerin aynı biçimlendirmeyi satırlara veya sütunlara uygulamaları gerekir. Hücrelere tek tek biçimlendirme uygulamak genellikle daha uzun sürer ve iyi bir çözüm değildir. Bu sorunu çözmek için Aspose.Cells, bu makalede ayrıntılı olarak açıklanan basit ve hızlı bir yol sağlar.

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 bir içerirçalışma sayfaları Excel dosyasındaki her çalışma sayfasına erişim sağlayan koleksiyon. Bir çalışma sayfası şununla temsil edilir:Çalışma kağıdı sınıf. buÇalışma kağıdı sınıf bir sağlarCells Toplamak. buCellstoplama sağlarSatırlarToplamak.

Satır Biçimlendirme

İçindeki her öğeSatırlar koleksiyon temsil ederSıra nesne. buSıranesne şunları sunar:Stili Uygula satırın biçimlendirmesini ayarlamak için kullanılan yöntem. Aynı biçimlendirmeyi bir satıra uygulamak için,stilnesne. Aşağıdaki adımlar nasıl kullanılacağını göstermektedir.

  1. Eklestil itiraz etmekÇalışma kitabı sınıfını çağırarakStil Oluşturyöntem.
  2. Yı kurstilbiçimlendirme ayarlarını uygulamak için nesnenin özellikleri.
  3. için ilgili öznitelikleri AÇIK duruma getirin.Stil Bayrağınesne.
  4. Yapılandırılanı atastil itiraz etmekSıranesne.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Adding a new Style to the styles
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.HorizontalAlignment = true;
styleFlag.VerticalAlignment = true;
styleFlag.ShrinkToFit = true;
styleFlag.Borders = true;
styleFlag.FontColor = true;
// Accessing a row from the Rows collection
Row row = worksheet.Cells.Rows[0];
// Assigning the Style object to the Style property of the row
row.ApplyStyle(style, styleFlag);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Bir Sütunu Biçimlendirme

buCells toplama ayrıca birSütunlar Toplamak. İçindeki her öğeSütunlar koleksiyon temsil ederKolon nesne. benzer birSıra nesne,Kolon nesne ayrıca şunları sunar:Stili UygulaBir sütunu biçimlendirme yöntemi.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Adding a new Style to the styles
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.HorizontalAlignment = true;
styleFlag.VerticalAlignment = true;
styleFlag.ShrinkToFit = true;
styleFlag.Borders = true;
styleFlag.FontColor = true;
// Accessing a column from the Columns collection
Column column = worksheet.Cells.Columns[0];
// Applying the style to the column
column.ApplyStyle(style, styleFlag);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

ileri konular